Wednesday, September 17, 2014

Design Patterns

General reusable solutions to common problems in software design.
Creational:  Create objects while hiding the creation logic, rather than instantiate objects directly using new operator
Structural: are concerned with how classes and objects are composed to form larger structures. This is all bout class and object composiiton.
Behavioral: interaction/communication between objects.
  • Creational
    1. Singleton:  This pattern ensures that a class has only one instance. 
    2. Factory Method: In Factory pattern, we create object without exposing the creation logic
    3. Abstract Factory patterns acts a super-factory which creates other factories. This pattern is also called as Factory of factories. In Abstract Factory pattern an interface is responsible for creating a set of related objects, or dependent objects without specifying their concrete classes.
    4. Builder pattern builds a complex object by using a step by step approach. Builder interface defines the steps to build the final object. This builder is independent from the objects creation process. A class that is known as Director, controls the object creation process.
      Moreover, builder pattern describes a way to separate an object from its construction. The same construction method can create different representation of the object.
    5. Prototype pattern is used to create a duplicate object or clone of the current object to enhance performance. This pattern is used when creation of object is costly or complex.
  • Structural
    1. Adapter :  convert the interface of a class into another interface client expect Related patterns:  Repository, Strategy, Facade,  Adapter pattern:  convert the interface of a class into another interface client expect
    2. Bridge:
    3.  Adapter starts from letter 'A' - map with After and Bridge starts from letter 'B' map with Before. 
    4.  Now when its Adapter its always to deal with the situation when some implementation is already in place and but some kind of hack 
    5. or patch or connector to put in place to make it work, like the real (electric) adapter.
    6.   Where in bridge - its before hand - design should be such that interface and implementation both are independent 
    7.  so abstraction is covered at interface level but actual implementation differs based on condition or need.
  • Behavioral
  1. TemplateMethod:  Define the skeleton of an algorithm in an operation, deferring some steps toSubclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the Algorithm's Structure. 
  2. Strategy:There are common situations when classes differ only in their behavior. For this cases is a good idea to isolate the algorithms in separate classes in order to have the ability to select different algorithms at runtime. 
  3. Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

No comments:

Post a Comment