Thursday, July 7, 2011

Design Pattern

1. What is a Design Pattern?
Design Patterns represent solutions to problems what arise when developing software within a particular context or we can also write it as a solution to a recurring problem in a large OOP system. Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice”

2. Elements of Design Pattern:
In general, a pattern has four essential elements
  1. The pattern name is a handle we can use to describe a design problem, its solutions, and consequences in a word or two. Naming a pattern immediately increases our design  vocabulary. It lets us design at a higher level of abstraction. Having a vocabulary for patterns lets us talk about them with our colleagues, in our documentation, and even to ourselves. It makes it easier to think about designs and to communicate them and their trade-offs to others. Finding good names has been one of the hardest parts of developing our catalog. 
  2. The problem describes when to apply the pattern. It explains the problem and its  context. It might describe specific design problems such as how to represent algorithms as objects. It might describe class or object structures that are symptomatic of an inflexible design. Sometimes the problem will include a list of conditions that must be met before it makes sense to apply the pattern.
  3. The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations. The solution doesn't describe a particular concrete design or implementation, because a pattern is like a template that can be applied in many different situations. Instead, the pattern provides an abstract description of a design problem and how a general arrangement of elements (classes and objects in our case) solves it.
  4. The consequences are the results and trade-offs of applying the pattern. Though consequences are often unvoiced when we describe design decisions, they are critical for evaluating design alternatives and for understanding the costs and benefits of applying the pattern. The consequences for software often concern space and time trade-offs. They may address language and implementation issues as well. Since reuse is often a factor in object-oriented design, the consequences of a pattern include its impact on a system's flexibility, extensibility, or portability. Listing these consequences explicitly helps you understand and evaluate them.

A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. The design pattern identifies the participating classes and instances, their roles and collaborations, and the distribution of responsibilities. Each design pattern focuses on a particular object-oriented design problem or issue. It describes when it applies, whether it can be applied in view of other design constraints, and the consequences and trade-offs of its use. Since we must eventually implement our designs, a design pattern also provides sample C++ and (sometimes) Smalltalk code to illustrate an implementation.

3. Describing Design Patterns
How do we describe design patterns? Graphical notations, while important and useful, aren't sufficient. They simply capture the end product of the design process as relationships between classes and objects. To reuse the design, we must also record the decisions, alternatives, and trade-offs that led to it. Concrete examples are important too, because they help you see the design in action. We describe design patterns using a consistent format. Each pattern is divided into sections according to the following template. The template lends a uniform structure to the information, making design patterns easier to learn, compare, and use.

  4.   Pattern Name and Classification
a. Intent : What does the design pattern do? What particular design issue or problem does it address?
b.  Also Known As : Other well-known names for the pattern, if any.
c.  Motivation : how the class and object structures in the pattern solve the problem.
d.  Applicability : What are the situations in which the design pattern can be applied ?
e.  Structure : A graphical representation of the classes in the pattern using a notation based on the Object Modeling Technique (OMT).
f.  Participants : The classes and/or objects participating in the design pattern.
g.  Collaborations : How the participants collaborate to carry out their responsibilities.
h.  Consequences : How does the pattern support its objectives? What are the trade-offs and results of using the pattern?
i.  Implementation : What pitfalls, hints, or techniques should you be aware of when implementing the pattern?
j.  Sample Code : Code fragments that illustrate how you might implement the pattern.
k.  Known Uses : Examples of the pattern found in real systems.
l.  Related Patterns : What design patterns are closely related to this one?

5. Various types or catalog of the Design Patterns

There are a total of 23 design pattern. These are illustrated as follow:
  1.  Abstract Factory : Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
  2. Adapter : Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
  3. Bridge : Decouple an abstraction from its implementation so that the two can vary independently.
  4. Builder: Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  5. Chain of Responsibility : Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it  
  6. Command : Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
  7. Composite : Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. 
  8. Decorator : Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
  9. Façade : Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.  
  10. Factory Method : Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. 
  11. Flyweight : Use sharing to support large numbers of fine-grained objects efficiently. 
  12. Interpreter : Given a language, define a represention for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
  13. Iterator : Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 
  14. Mediator : Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
  15. Memento : Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.  
  16. Observer : Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  17. Prototype : Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.    
  18. Proxy : Provide a surrogate or placeholder for another object to control access to it.  
  19. Singleton : Ensure a class only has one instance, and provide a global point of access to it. 
  20. State : Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
  21. Strategy : Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
  22. Template Method : Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
  23. Visitor : Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
6. Organizing the Catalog  

Basically, there are three categories:
  1. Creational Patterns: deal with initializing and configuring classes and objects.
  2. Structural Patterns: deal with decoupling the interface and implementation of classes and objects.
  3. Behavioral Patterns: deal with dynamic interactions among societies of classes and objects.

No comments:

Post a Comment