phpdesign-patterns

Design pattern for activities


Let's say I have multiple activities with same events and these events having different parameters in each activity. The question is "Is there any design pattern that particularly cover this matter?"

If I use interfaces, then I have to have same parameters in event functions. What I need is just insuring of having all event functions with any parameters in each activity.

Example:

class FirstCampaign{
    public function onFirstPurchase($arg1,$arg2, User $user){
        //
    }
    public function onFirstTrade($price,$something, User $user,Model $model){
        //
    }
}

class SecondCampaign{
    public function onFirstPurchase(User $user){
        //
    }
    public function onFirstTrade(Model $model){
        //
    }
}

class ThirdCampaign{
    public function onFirstPurchase(User $user, Model $model, int $abc){
        //
    }
    public function onFirstTrade(){
        //
    }
}

PS. I have tried onFirstPurchase(...$arguments) but it's not exactly what I need and it's just too complicated to work with.

PS. I call the events in different part of my application and they are not running under same circumstances.


Solution

  • You can define an interface for your compaigns and define another interface which is sort of wrapper for your params.

    public interface CampaignInterface {
    
        public function onFirstPurchase(User $user, PurchaseContextInterface $context);
    
        public function onFirstTrade(TradeContextInterface $context);
    }
    

    public class FirstCampaign implements CampaignInterface {
    
        public function onFirstPurchase(User $user, PurchaseContextInterface $context) {
            // Implementation here
        }
    
        public function onFirstTrade(TradeContextInterface $context) {
            // Implementation here
        }
    }
    

    public class FirstCampaignPurchaseContext implements PurchaseContextInterface {
        // Implementation here
    }
    

    public class FirstCampaignTradeContext implements TradeContextInterface {
        // Implementation here
    }