Posts

Showing posts from December, 2014

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 UML. The d

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 Context Object : Conte