Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which design pattern to use (i need something like mediator pattern)?

I have a GUI application with a lot of controlles. The most part of them have events and controles are logically wired to each there. For example, i have 5 comboBoxes. If i choose something in cb1 and cb3 them cb2 must change a list of its available values. If i choose something in cb2 and ch4, them cb5 must load its list of its available values. All the articles i've found in the web says i should use mediator pattern in this case. But in all of these articles mediator - is something like a god class, which knows everything about all controlles and decides what should be done in case of some event. If i have 5-6 controlles - everything is ok. But what if i have something like 20 or 40 controlles? I guess mediator class will become a monster in this case. It'll be hard to do something with it (modify, or add new functionality).

Is there any other patterns, or good examples of mediator pathern, which will help to solve this situation?

PS: Sorry if there are already existing questions on this topic - there are so many questions with the same name, that it not so easy to find something i really need from all these topics.

like image 675
Victoria Agafonova Avatar asked Apr 16 '12 16:04

Victoria Agafonova


People also ask

What is the mediator pattern used for?

Mediator pattern is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loose coupling.

Is mediator a design pattern?

Mediator design pattern is one of the important and widely used behavioral design pattern. Mediator enables decoupling of objects by introducing a layer in between so that the interaction between objects happen via the layer.

What forms the mediator in the mediator design pattern?

The Mediator defines an object that controls how a set of objects interact. Loose coupling between colleague objects is achieved by having colleagues communicate with the Mediator, rather than with each other. The control tower at a controlled airport demonstrates this pattern very well.


2 Answers

Mediator is indeed the right choice here, however there is alternative option to use OBSERVER pattern which will allow you to keep logic separate. (cb2 observes cb1 and reacts approperly, cb3->cb2 etc).

With OBSERVER pattern you will have to link only 2 controls at a time, this is like chain.

like image 70
Alex Burtsev Avatar answered Sep 28 '22 07:09

Alex Burtsev


Use the different actions as strategies Strategy Pattern and invoke a certain strategy from the Mediator, so that you would only need to change few lines of code if you need to add more controllers tomorrow.

like image 41
Phani Avatar answered Sep 28 '22 07:09

Phani