Welcome To Iterators.co.in

Design patterns

Design Patterns deal with comman reoccuring problems.

Creational Design Patterns

these design patterns deal with object creation.

Factory Pattern / Factory Method :-

  • this can be used when there are multiple sub classes of an interface or class or abstract class.
  • it can help to hide implementation class. so that changes made in implementation class would not affect to client code.
    eg. :- Currency(I) can be implemented with different Nation as implementation class.
    Then we can create a CurrencyFactory Class which will have method to return the logic to
    return Currecy implementaion object based on input for nation name.

Abstract Factory Pattern.

  • In general Factory is place where something is produced.
  • Abstract Factory is nothing but Factory of Factories.
  • AbstractFactory pattern could be implemented by interface with multiple abstract getFactoryTypes() methods.
  • like in FactoryPattern we used to get object of same type like currency or color etc. here we can provide abstraction over for multiple factories together.
    e.g. getShapeFactory() it could be rectangle or circle or getColorFactory() it can be red, green or blue.

Builder Design Pattern

  • This approach helps to build step by step approach to build complex object.
  • This can be implemented with help of interface which provides abstract methods to build some object.
  • consider example of Bike . if we want to build a bike then we can use BikeBuilder like interface in Which we can provide abstract methods like engine(-); headLights(-); seatType(-) etc. so implementing those methods would give us our dream Bike :).

Prototype Pattern in java

  • Prototype patterns is required, when object creation is time consuming, and costly operation,so we create object with existing object itself. One of the best available way to create object from existing objects are clone() method. e.g. getting database connection operation.
  • If you are using clone method, then it is upto you to decide whether go for shallow copy or deep copy based on your business need.
  • Deep Cloning is when we make copy of Has-A relationship type data members.
  • Shallow cloning is default type. in this type we are not making copy of Has-A relationship type data members. i.e. both orignal and cloned object refer same HAS-A relationship data-member.
  • Example implementation.
    1. create an Abstract class implementing Clonable interface. & providing prototypes of other methods and variables.
    2. implement all the subclasses for abstract class.
    3. create a prototype class which can create a store for objects of implementations like using map. that stores all implementations of object.
    4. in prototype class provide the getter(Key) method to get implementation class object with provided key.
    5. the getter method should invoke clone() method insted of returning orignal implementation object.
      public static Color getColor(String colorName){ return (Color) colorMap.get(colorName).clone(); }

Structural Design Patterns

Structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.

Adapter Pattern

  • An adapter pattern helps two incompatible interfaces to work together
  • e.g. lets take realtime example. there are USA charger sockets & there are UK type charger sockets.
  • to support both types we must have an adapter where we can plug our charger and then we can plugthe adapter in USA or UK type charging socket.
  • simmilerly we can implement this concept in java.
    e.g. Class A needs temprature in degree celsius. &
    Class B is there which provides temprature in Farenhite.
    so we can write an adapter to take input form Class B to get temprature in Faranhite &
    write convertion method to convert from Faranhite to Degree celsius and return value.

Composite Pattern

  • Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy.
  • Composite pattern can be implemented like one object should have a group or collection of it's own sub-type in hirarchy objects.
  • e.g. an employee can be implemented by CEO and CEO class can have SALES &
    MARKETING employees as its sub-ordinates and SALES & MARKETING employees can
    have list of their subordinates. inshort sales & marketing employees could be added under CEO
    & clerks could be added as subordinates of MARKETING employees.

Proxy Pattern

  • Proxy pattern provide a surrogate or placeholder for another object to control access to it.
  • for example suppose there is request for Internet object to visit useful website.
  • so insted of providing direct access to Internet object we can create a class to which is proxy of Internet this class will capture request and do validations and then it will return Internet object to access it's content.
    Request("www.ddffss.com")---> InternetProxy("www.ddffss.com")
    it will verify content reuqested and forward to --> Internet.

Flyweight Pattern

  • Flyweight pattern is primarily used to reduce the number of objects created and to decrease memory footprint and increase performance.
  • Flyweight pattern tries to reuse already existing similar kind objects by storing them and creates new object when no matching object is found.
  • it takes help of some property to check in it's store like map if object is already created. if object is already created then it will return existing object.
  • if object is not already present in the object store or map then it will go for creation of object.
  • it will reduce direct object creation.

Behavioural Design Patterns