Skip to main content

Posts

Showing posts with the label behavioral patterns

Behavioral Patterns : Visitor

Properties: -            Used when it is necessary to execute actions on members of an object structure (e.g. collections, hierarchical structures, composite structures). -            Separates algorithm of the actions from the object structure members. So that new algorithms can be modified easily. -            This pattern relies on polymorphic calls and helps to follow  open/closed principle . -            Another advantage is that visitor objects may be statefull. -            One disadvantages is, while it is easy to add new visitors, it is difficult to add new observable objects. Implementation: -            Visitor  interface declares visit methods. For...

Behavioral Patterns : Template Method

Properties: -            Abstract class defines steps to achieve some task. At some points it defers actions to subclasses.   -            Common steps are done within abstract class so code repetition is avoided. -            Subclasses defines subclass specific steps. Implementation: -            AbstractClass  defines template method in which generic algorithm is formed. At subclass specific points abstract mehods are called to defer specific tasks to subclasses. -            ConcreteClass  extends abstract class and defines abstract methods to implement tasks in a specific way to it. Java Standard Library Implementations: -            java....

Behavioral Patterns : Strategy

Properties: -            Separates implementation from algorithm (strategy).   -            The same class can be used with different algorithms. Implementation: -            Strategy  creates an abstraction for different algorithms. -            ConcreteStrategy  implements strategy interface for a specific algorithm. -            Context  object uses strategy object to do its job. Its behaviour depends on the concrete strategy implementation. Java Standard Library Implementations: -            java.util.Collections.html#sort(java.util.List, java.util.Comparator)  uses Comparator as strategy. Example Usage:     ...

Behavioral Patterns : Observer

Properties: -            When object state changes, lets interested parties know about the changes. -            As the name implies observers, observe the object in question for state changes. -            Observers need to register to the object for receiving notifications when state change occurs. Implementation: -            Observer  interface declares methods to get updates . -            Observable  interface declares methods to register, de-register for state changes. -            Concrete Observable  class implements the Observable interface and responsible for notifying oberservers about stage changes. Java Standard Library Implementations...

Behavioral Patterns : Memento

Properties: -            Can be used when object state needs to be saved between interactions. Implementation: -            Originator class is the class of which state needs to be saved. It should be able to save its state into Memento objects and restore its state from Memento objects. -            Memento objects keep state of originator object. -            Caretaker is the client code which saves and restores state of originator using Memento objects. Java Standard Library Implementations: -            javax.faces.component.   StateHolder implementations -            java.sql.Connection Example Usage: try {     //assume connection is acq...