Skip to main content

Posts

Showing posts with the label design patterns

Creational Patterns : Singleton

Properties: -            Ensures that only one instance is created. -            Global access is provided to single instance. -            Conventionally has a  public static  method named  getInstance/getXYZ  ,where XYZ stands for class name, to return the class instance. -            Suitable for creating factory instances and system wide unique resources. Implementation: -            Constructor should be made  private/protected  to prevent arbitrary instance creation. -            Single instance is created and assigned to a  public static  member of the class. This class member is conventionally named as  instance ...

Creational Patterns : Simple Factory

Properties: -            Hides details of class instance creation and actual class implementation. This allows easy replacement of class implementation and gives flexibility to change the way new instance construction  is done. -            Creates and  abstraction  layer between client code and object needs to be created, thus facilitates  loose coupling . -            Client provides attributes of the object it needs to create and defers instantion to factory. It is not obligatory to supply any attributes though. -            Factory creates a suitable class instance according to attributes supplied.  -            The problem with Factory pattern is when a new implementation is added Factory class ...

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

Creational Patterns : Prototype

Properties: -            When cost of creating an object from scracth is high it might be good idea to create it by cloning an existing object. -            Identified by a super class declaring a clone method and implemented clone method inside class. Implementation: -            Class extends a super class which declares a clone method. -            Clone method is implemented to create a copy of the object. Java Standard Library Implementations: -            Any class implementing  java.lang.Cloneable Example Usage: // simply person clone is invoked Person newPerson  =   ( Person ) person . clone ();

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

Creational Patterns: Object Pool

Properties: -            When cost of creating an object from scratch is high it might be a good idea to reuse already created objects. -            Identified by a pool class that objects are retrieved from and then released back to. Implementation: -            Reused objects might be wrapped by another object. -            Pool class keeps created objects. -            Client requests a new object instance from pool when it needs a new object. After client is done with object instance it releases the object, so that the object can be reused again. Java Example Usage: -            com.mchange.v2.c3p0.ComboPooledDataSource Example Usage: // create connection ...