Thursday, February 28, 2008

Design Patterns


I was asked to do a presentation about design patterns and after the presentation people had asked me to post it, so here is a copy of my PPT :

What are Design Patterns?



  • In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design.

  • Not a finished design that can be transformed directly into code

  • A description of how to solve a problem

  • Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems.

  • Show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.


Why Design Patterns?



  • Can speed up the development process.

  • Prevent issues that can cause major problems.

  • Improves code readability.

  • Provide general solutions.

  • Design reuse, which is more powerful than code reuse.

Classifications of Design Patterns:



  • Creational Patterns:
    Abstract Factory Creates an instance of several families of classes
    Builder Separates object construction from its representation
    Factory Method Creates an instance of several derived classes
    Prototype A fully initialized instance to be copied or cloned
    Singleton A class of which only a single instance can exist


  • Structural Patterns
    Adapter Match interfaces of different classes
    Bridge Separates an object’s interface from its implementation
    Composite A tree structure of simple and composite objects
    Decorator Add responsibilities to objects dynamically
    Facade A single class that represents an entire subsystem
    Flyweight A fine-grained instance used for efficient sharing
    Proxy An object representing another object


  • Behavioral Patterns
    Chain of Resp. A way of passing a request between a chain of objects
    Command Encapsulate a command request as an object
    Interpreter A way to include language elements in a program
    Iterator Sequentially access the elements of a collection
    Mediator Defines simplified communication between classes
    Memento Capture and restore an object's internal state
    Observer A way of notifying change to a number of classes
    State Alter an object's behavior when its state changes
    Strategy Encapsulates an algorithm inside a class
    Template Method Defer the exact steps of an algorithm to a subclass Visitor Defines a new operation to a class without change
A Sample of a Design Pattern Document:



  • Pattern Name and Classification:

  • Intent: goal

  • Also Known As: Other names for the pattern.

  • Motivation (Forces): A scenario in which this pattern can be used.

  • Applicability: Situations in which this pattern is usable; the context for the pattern.

  • Structure: A graphical representation of the pattern. (Class diagrams, Interaction diagrams)

  • Participants: A listing of the classes and objects used in the pattern.

  • Collaboration: A description of how classes and objects used in the pattern interact

  • Consequences: A description of the results, side effects, and trade offs.

  • Implementation: A description of an implementation

  • Sample Code:

  • Known Uses: Examples of real usages of the pattern. Related Patterns: Other patterns that have some relationship with the pattern; discussion of the differences between the pattern and similar patterns.
Example?



  • Pattern Name and Classification: Singleton, creational pattern

  • Intent: Ensure a class has only one instance and provide a global point of access to it.

  • Also Known As: Single Instance .

  • Motivation (Forces): Sometimes it's appropriate to have exactly one instance of a class: window managers.

  • Applicability: Singleton can be used in a data repository or data collection where creation of more than one object can be resource wastage. Hence each client is given a reference to a single shared object to operate on. While the second case Singleton can be used in locking mechanisms. Once a client has got the object, no other client can have an object..

  • Structure:

  • Participants: The classes and/or objects participating in this pattern are:

  • –Singleton:defines an Instance operation that lets clients access its unique instance. Instance is a class operation.

  • responsible for creating and maintaining its own unique instance.

  • Collaboration: A description of how classes and objects used in the pattern interact

  • Consequences: A description of the results, side effects, and trade offs.

  • Implementation: A description of an implementation

  • Sample Code:
    – class Singleton { private static Singleton instance; // Note: Constructor is 'protected' protected Singleton() { } public static Singleton Instance() { // Use 'Lazy initialization' if (instance == null) { instance = new Singleton(); } return instance; } }
    –// .NET Singleton
    –sealed class Singleton
    –{
    – private Singleton() {}
    – public static readonly Singleton Instance = new Singleton();
    –}



  • Known Uses: Connection Pooling

  • Related Patterns: Other patterns that have some relationship with the pattern; discussion of the differences between the pattern and similar patterns

  • References:


    No comments: