Saturday 26 July 2014

What and When Design Pattern?



 

PatternWhat & When
Decorator

  • To provide a way of attaching new state and behavior to an object dynamically.
  • This pattern allows to add new functionality an existing object without altering its structure by creating wrapper class.
You have:
  • An existing component class that may be unavailable for subclassing
You wants:
  • Attach additional state and behavior to an object dynamically.
  • Make changes to some objects in a class without affecting others.
  • Avoid subclassing because too many classes could result.
******************
Proxy

  • It Supports objects that control the creation of and access to other objects
  • In Proxy pattern, a class represents functionality of another class.
You have:
  • Are expensive to create
  • Need access control
  • Access remote sites.
  • Need to perform some actions when they are accessed.
You wants:
  • Create objects when their operations are requested.
  • Perform checks on objects whenever accessed.
  • Have a local objects that will refers to a remote object.
  • Implement access rights on objects as their operation are requested.
******************
Bridge

 
  • It decouples an abstraction from its implementation, enabling them to vary independently.
You have:
  • Identify that there are operations that do not always need to be implemented in the same way.
You wants:
  • Completely hide implementation from client.
  • Combine different part of system at runtime.


******************
Composite


  • The Composite pattern arranges structured hierarchies so that single components and groups of components can be treated in the same way.
  • The beauty of this pattern is its ability to manipulate the different types of the elements equally.


You have:
  • An irregular structure of objects and composites of objects.
You wants:
  • Clients to ignore all but the essential differences between individual objects and composites of objects.
  • To treat all objects in a composite uniformly.
******************
Flyweight

  • The Flyweight pattern promotes an efficient way to share common information present in small objects that occurs in a system in large number.
  • It thus helps reduce storage requirement when many values are duplicated.
You have(There are):
  • Many objects to deal with in memory.
  • Different kind of state, which can be handled differently to achieve space saving.
  • Group of objects that share state.
  • Ways of computing some of the state at runtime.
You wants:
  • Implement a system despite server memory constraints
******************
Adapter

  • It is versatile pattern that joins together types that were not design to work each other.
  • It is useful pattern when dealing with legacy code.
  • It enables a system to use classes whose interfaces don't quite match its requirements.
You have:
  • A Domain specific Interface
  • A class to connect to with a mismatching interface.
You wants:
  • Create a reusable class to cooperate with yet-to-be-build classes.
  • Change the name of methods as called and as implemented.
  • Support different sets of methods for different purpose.
******************
Facade

  • To provide different high level views of subsystems whose details are hidden from users.
  • Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.
******************
Prototype

  • It creates new objects by cloning of a few stored prototypes.
  • It speeds up the instantiation of very large, dynamically loaded classes.
  • It refers to creating duplicate object while keeping performance in mind.
  • This pattern is used when creation of object directly is costly. For example, an object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.
You wants:
  • Hide concrete classes from the client.
  • Add and remove new classes (via prototypes) at runtime.
  • Keep the number of classes in the system to a minimum.
  • Adapt to changing structure of data at runtime.
Note: In C# cloning by deep copying is absolutely straightforward.
******************
Factory method

 
  • It is a way of creating objects, but letting subclasses decide exactly which class to instantiate.
You wants:
  • Flexibility is important.
  • Objects can be extended in subclasses
  • There is a specific reason why one subclass would be chosen over another-this logic forms part of the Factory method.
******************
Singleton

 
  • The purpose of the Singleton Pattern is to ensure that there is only one instance of a class, and there is a global access point to that object.
  • The pattern ensures that the class is instantiated only once and that all requests are directed to that one and only object.
  • Moreover, the object should not be created until it is really needed.
You wants:
  • You need to ensure that only one instance of a class.
  • Controlled access to that instance is essential.
  • You might need more than one instance at a larger stage.
Abstract Factory

 

No comments:

Post a Comment