Skip to main content

Posts

Showing posts with the label design pattern

Mediator Design Pattern

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...

Proxy Design Pattern

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 ...

Bridge Pattern

Bridge design pattern which is also a structural design pattern is a pattern where you can decouple the abstraction and implementation using a bridge so that both can vary without effecting each other. Let’s take the above class diagram as our use case where a circle can be drawn in different colors using the same abstract class method but different bridge implementer’s class. So here we have abstract class shape and it has method draw which can vary, so we have abstracted it out to an interface using aggregation to bridge interface "DrawAPI" and it is responsibility of its implementer’s to draw different color circle.   ** "Bridge Implementor" */ interface DrawingAPI {     public void drawCircle(final double x, final double y, final double radius); } /** "ConcreteImplementor"  1/2 */ class DrawingAPI1 implements DrawingAPI {     public void drawCircle(final double x, final double y, final double radius) {         System....