Posts

Showing posts from January, 2015

Interpreter Design Pattern

Type: Behavioral Design Pattern Summary : Interpreter design pattern is a behavioral design pattern. It is used to interpret or evaluate an expression(like sentence, query, mathematical expression etc).  In this pattern Expression is considered to be composition or Terminal Expression and Compound Expressions.  Example:  SQL Squery,  Search engine query processing are example for Interpreter design pattern.

Prototype Design Pattern

Image
Type : Creational Design Pattern Summary Prototype design pattern is useful when creating an Object is expensive. Prototype is based on the idea of having a blueprint object for creating other object instances by cloning it. In java Prototype Design pattern is implemented by using cloning an existing object. Details In Prototype design pattern, we create a new Object instance by copying (clone) existing object instance and changing required attributes. It is a good use case when Creating an object is complex and expensive. In those cases rather than creating a brand new instance though constructor it is much more efficient if we can clone an existing object. Participants In Prototype Design Pattern Client : It calls the prototype to get object instance. Prototype : It Provides the clone interface. Clones: The clones object that are being created. Registry or Cache: It holds the master created objects. Example Let's take example of building a Car object

Builder Design Pattern

Image
Type : Creational Design Pattern Summary Builder pattern is a Creational Design Pattern and solves the problem of creating complex objects or objects whose constructor needs many parameters. Details Considering a Class whose constructor has a number of parameters, we have to send all of them to instantiate an instance, which could be error prone and complex sometimes. Also if the constructor has some optional parameters or if we want to have some parameters to have fixed value, we might end up with a number or Constructors for the object. Builder pattern solves this problem by exposing a builder object which can have multiple methods based on different parameters. Example For our example we will consider a Class BlogObject which represents a Blog and has a number of fields. Some of the fields like, Tag, Summary etc are optional. Without Builder we might need to have a number of constructors for the Class. But with Builder we can create the instance of the Object easily a

Bridge Pattern

Image
Type : Structural Design Pattern Summary Decouple an abstraction from its implementation so that the two can vary independently. Abstraction and Implementation can have own hierarchies, which makes it easy to change the Implementation Classes without impacting the Client. Details It can be considered as double abstraction. Bridge pattern separates the implementation from abstraction in object oriented world. As a result of Bridge pattern two different object hierarchies can be assumed. The abstraction hierarchies and implementations hierarchies. The implementation hierarchies can take the complexity of implementations that very and not related to the other hierarchies. Bridge Pattern utilizes Composition and Inheritance. It's purpose is to make the Code more manageable and easy to refactor. Advantages: Bridge pattern bridges two hierarchies, it results in less number of Classes, since the implementations can be plugged in to the abstraction in run time. Bride

Memento Design Pattern

Image
Type : Behavioral Design Pattern Summary Memento design pattern is used when we want to save the state of an object so that we can restore later on. Advantages The memento pattern provides a way of recording the internal state of an object in a separate  object.  Memento design pattern removes need for multiple creation of the same object for the sole purpose of saving its state.  The memento simplifies the Originator since the responsibility of managing Memento storage is no longer centralized at the Originator but rather distributed among the Caretakers. Disadvantages As the Memento object stores the data of the Originator object, it generally copies the fields that represents state of the originator. Changes in the Originator object could force to change the memento object also. Details Memento design pattern is used when we want an option to to get previous state or restore previous version of objects. It has three key components Originator

Template Method Design Pattern

Image
Type : Behavioral Design Pattern Summary Template method pattern defines some steps or sequences of procedure and defers implementations of the procedures to the subclasses to implement. Details Template method uses in real life is very common. For instance we want to order some product online, first we have to select product and then add it to cart, then checkout. Another example would be when we go to a movie theater,  we first decide the show, buy the ticket and that go to the respective screen to enjoy the movie, and finally leave the theater. When the work can broken in to sub tasks and order of the tasks are important Template method helps in maintaining that order. Another example would be our day to day routine. We can break the day in several sequence of tasks that we want to carry out. DayRoutine : Abstract class which declares some tasks like wakeup(), doBreakFast() gotoBed() etc.                         To maintain the order it also has a final method

Mediator design pattern

Image
Type : Behavioral Design Pattern Summary Mediator design pattern provides a common platform for communication between different objects. It promotes loose coupling between interacting objects. Details In a complex software where number of classes interacting with each other are more, using Mediator design pattern is very useful. As mediator will reduce the complexity of objects interacting with each other to a centralized Mediator, which results in easy maintenance of code. Mediator is similar to Observer pattern as both promotes loose coupling. Mediator is a single object though which other objects communicates. In Observer pattern we can generally have multiple observer object. Whatsapp group, Messenger group can be considered as mediators. Users post messages to the group rather than directly to each other. Enterprise service bus (ESB) can be considered as mediator. Netflix app can be considered as a mediator. Participants in mediator design pattern Mediator Co