Mediator design pattern is a pattern used to communicate between related object using mediator , the communication happens in such way that either party is not aware of the underlying communication mechanism. So as a classic example is our BSE and NSE stock exchange , the exchange facilitates with different stock offerings and they don't hold any stocks with them , it is broker which offers to sale or buy to NSE or BSE and which facilitates the buying or selling of the share . So here BSE and NSE are the mediator which provides hooks for different brokers zerodha, sharekhan... to register and perform the trade , one broker offers to sale certain shares to and other broker buys the same , and this task is performed by the mediator silently without bothering each other. Please follow the UML diagram for the same. Here we have Colleague object which is nothing but the abstract broker and then we have different implementation of broker's like sharekhan , zerodha which
We use this Pattern when we require don't want to give a direct access to object under some scenario and rather offer a surrogate object or proxy object which provides the functionality in place of the real object. There could be several scenario where we would want this kind of functionality. The real object has some sensitive operation which would be risky to expose. The real object creation is memory extensive so we want to control the behavior of its creation. The real object is remotely located and rather we want to interact through a local copy using proxy which is actually aware of it location. So lets look at the UML diagram of the same. So here we have Subject interface which is implemented by both the Proxy and RealObject and proxy object has reference to the real object , now the client will instantiate Proxy object to perform some action and , proxy object will delegate the call to RealSubject to perform the same.