1

I was studying Mediator Pattern and I noticed that to use this pattern you should register the Colleagues into Mediator from the Colleague concrete classes. for that we have to make an instance of Mediator inside Colleague concrete classes which violates IoC and you can not inject the Colleagues into Mediator (as far as I know! whether it is right or wrong)

Questions:

1- Am I right about the thing I said?

2- Shall we always use IoC at all or there are some times you can forget about it?

3- If we always have to use IoC, can we say Mediator is an anti-Pattern?

1

The mediator pattern helps in the communication between objects.
IoC is inversion of control i.e. instead of object getting a required resource one is provided to it.
These patterns are unrelated.

2
  • Will you please give me a simple example that uses Mediator and not violating IoC? – Mehrdad Kamelzadeh Jun 16 '13 at 18:21
  • 2
    @MehrdadKamelzadeh:But what I am saying is that these 2 patterns are completely unrelated.I am not sure what example you expect.Mediator helps in reducing the "communication links" between objects. IoC is a container where objects live and are provided the resources they need to do their work. E.g. a database connection, a tcp connection, a DAO, another object etc – Jim Jun 16 '13 at 18:23
1
  1. No - I only see registration mentioned in the examples it is not prescribed in the pattern itself.
  2. No - an IoC container is not prescribed in this pattern.
  3. No - Given the two first assertions are incorrect, the third does not follow.
1

I have used a Framework, RobotLegs, that automates dependency injection as part of how it works, and it very heavily depends on Mediator. Typically, the Mediator only knows in detail about one of the Colleagues (the View), and then it will get a generic reference to a communication channel that implements an Interface.

Both the reference to the View and the event bus are supplied through dependency injection. The default Mediator implementation declares an interest in the event bus, and so the IoC container supplies the one it has on file. Specific Mediator Classes are registered for use with specific Views, and the Mediator will be created when the View is put into use. It exposes a variable whose type matches the View and the IoC container will then provide the new View instance to the Mediator that was created.

How this works in your framework/language of choice will vary, but there is no reason that the Colleagues can't be provided through Dependency Injection. Under no circumstances should the Colleagues even be aware of, much less be responsible for creating, the Mediator.

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.