Posts

Showing posts with the label Behavioral Design Pattern

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

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

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

Design Pattern : Strategy Design Pattern

Image
Type Behavioral Design Pattern Summary It enables an algorithm's behavior to be selected at runtime.  Multiple Strategies exists and Client select one strategy at run time. The strategy pattern, defines a family of algorithms, encapsulates each algorithm, and makes the algorithms interchangeable within that family. Advantages The strategy pattern uses composition instead of inheritance. In the strategy pattern, behaviors are defined as separate interfaces and specific classes that implement these interfaces. It obeys  Open/Closed Principle which says Classes should be open for Extension but closed for modifications. Simple Use Case Consider the use case of online Advertisement i,e displaying of an advertise. We can consider different mediums of advertisement like TextAdd or video Adds or evan Image adds. Class Diagram Explanation  Sample Code Difference with State Pattern Both State Pattern and Strategy Pattern looks same in ...

Design Pattern : State Pattern

Image
Type : Behavioral Design Pattern Summary   The State Design Pattern is a behavioral design pattern. The essence of State pattern is that the behavior of an Object changes based on its state. For instance the behavior of Light changes based on it's state (turned On, turned Off etc) Advantages Reduces if/else, switch conditional statements. Context class gets its behavior by delegating to state object instances. Complex logic can be delegated to specific state objects. Details A good example of State pattern would be of Shuttle. Shuttle has different behavior like (speed, number of passengers, range etc.) based on its state. Car could have different states like running, parked etc. For instance when the Shuttle is busy in pickup it would have occupancy, range and since moving it will also have speed. But when the Shuttle is not busy and waiting for next trip, it will have speed 0 (since not moving) vacancy etc. Participants...

Command design pattern

Image
Type : Behavioral Design Pattern Summary Command pattern separates the client which invokes a method from the object which contains the method. Important points Command pattern decouples requestor (Invoker) of the action from the object that perform the action (Receiver). Command pattern can be used to do undo operations. Command pattern is useful when a history of requests is needed. Command pattern is useful when  \we need callback functionality Advantages it decouples the client from the actual class who performs the operation. We can create new commands to have new behaviour. Undo operations can be designed with command pattern. Disadvantages It increases the number of Classes. If the Receiver class changes we would need to change the Command Class. Details Different  Objects participating  in the pattern are, Command - declares an interface for executing an operation; ConcreteCommand - extends the Command interface, implementin...

Observer design pattern

Image
Type : Behavioral Design Pattern Summary In Observer design pattern, an object called subject maintains one to many relationship with Observers, so that once the state of the Subject changes it can automatically inform Observers registered with the subject. Advantage An Object(Subject) can notify other Objects(Observer) without being tightly coupled with Observer and depend on Observer Class. Different instances of the same Subject can register its own list of Observers. Disadvantage Due to the indirect communication between Subject and Observer's it can lead to performance issues. Important Points Multiple Subscribers for a Subject. Subjects know its Subscribers. Generally the Subject triggers the method that updates Observers. New Observers can be added to the System with minimal changes. Details Following are different participants in Observer Pattern Subject (Interface)  Keeps track of its observers  Provides an interface for attaching an...

Visitor Design Pattern

Image
Type   Behavioral Design Pattern Summary  Visitor Design pattern follows the open/closed principle and allows us to add new operations to be added to object structure. Open/Closed principle states that entities should be able to extend its behavior or add need new functionality without changing its own implementation. Important Points Visitor Design Pattern uses Double Dispatch to invoke a method at run time based on the object on which method is defined and its parameter. Visitor represents operations or functionality to be performed on different nodes of an object structure. Visitor Pattern  follows Open/Closed Principle. Useful in adding new behavior or functionality to a object tree, without modifying the original Objects code. Advantage  We can add new functionality to the Hierarchy of Classes without changing the classes. Disadvantage The arguments visiting methods have to be known in advance. Details The participants cl...