2

I am looking for the best pattern to apply in my problem. I have an interface that defines my service class functionality

interface NegotiationInterface {
    abstract public function resetNegotiation(Negotiation $negotiantion);
} 

A main class implements it

public class NegotiationService implements NegotiationInterface {

    public function __construct(…Some Dependencies…)
    {
    ….        
    }

    public function resetNegotiation(Negotiation $negotiantion){
    …. //All business logic
    }
}

NegotiationService is registered under DI container(Symfony based) and used all over application by its service id.

$negotiationService = $this->container->get(“negotiation_service”);
$negotiationService->resetNegotiation($negotiation);

However some of our clients(negotiation contains the client info), require an additional step after calling resetNegotiation, for example our common business logic + calling a webservice. I reached at decorator pattern but I am not sure if it would be the best approach while using DI. If so how would I apply together with DI. I would like to have those extra steps loaded dynamically according to client.

  • 2
    How about a default implementation of NegotiationInterface for the clients whom do not have any post-resetNegotiation logic, and a specific implementation for each one that does have the extra logic? – Graham Nov 3 '17 at 20:50
  • @Graham Yes, that's the goal. How would I implement it and call just one method transparently from my container service? – dextervip Nov 3 '17 at 21:01
  • By providing a post-reset-negotiation method on your interface, and then implementing that method in your client-specific classes. If no -post-reset-negotiation logic is required, simply return from that method without providing any additional logic. – Robert Harvey Nov 3 '17 at 21:10
2

You should create 2 NegotiationService classes that implement the two different resetNegotiation() functionality. Then you add a NegotiationServiceFactory class that will provide you with the correct Service based on the client info. You should keep this business logic explicit and not hide into some technicalities.

In your code you will then call the NegFactory from DI container and ask it to provide you the correct NegService based on the client ID (or other parameters).

$negotiationServiceFactory = $this->container->get(“negotiation_service_factory”);
$negotiationService = $negotiationServiceFactory->getServiceByClientInfo($someClientInfo);
$negotiationService->resetNegotiation($negotiation);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.