Design Patterns - PowerPoint PPT Presentation

About This Presentation
Title:

Design Patterns

Description:

... evaluate important reoccurring designs in object oriented ... Defined Design Patterns. Abstract Factory. Adapter. Bridge. Chain of Responsibility. Command ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 24
Provided by: ianm91
Learn more at: http://www.cs.albany.edu
Category:

less

Transcript and Presenter's Notes

Title: Design Patterns


1
Design Patterns
  • An Introduction
  • By Ian M. Mac Donald

2
History
  • Difficulties in designing reusable object
    oriented software
  • Must be specific to problem, yet general enough
    to handle future problems
  • Reoccurring patterns appear in classes and
    communicating objects in many O.O. systems.
  • You may get design déjá-vu

3
What Was Needed
  • We need to locate, explain and evaluate
    important reoccurring designs in object oriented
    system.
  • We need a generation of successful designs and
    architectures that can be reused.
  • We need a way to improve documentation and
    maintenance of existing systems.

4
Definition of a Design Pattern
  • In general, a design pattern describes a
    recurring problem and the core of its solution
    with the goal of creating reusable software.
  • A design pattern will identify the participating
    classes and instances, their roles and
    collaborations, and the distribution of
    responsibilities.

5
The Four Elements of a D.P.
  • Pattern Name handle for a design problem,
    solutions and consequences. It is a good idea to
    keep this name down to one or two words.

6
The Four Elements of a D.P.
  • The Problem When to apply the pattern.
    Explains problem and context, prerequisite
    conditions.

7
The Four Elements of a D.P.
  • The Solution Elements that make up design,
    relationships, responsibilities, and
    collaborations. Does not give concrete, rather
    abstract description of problem and how a general
    arrangement of classes/objects can solve it.

8
The Four Elements of a D.P.
  • The Consequences Results/trade-offs of applying
    the pattern.
  • Ex Space and time, language implementation,
    impact on existing system.

9
Attributes of a Design Pattern
  • Pattern Name and Classification
  • Intent
  • Also Known As
  • Motivation
  • Applicability
  • Structure
  • Participants
  • Collaborations
  • Consequences
  • Implementation
  • Sample Code
  • Known Users
  • Related Patterns

10
Defined Design Patterns
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • Prototype
  • Proxy
  • Singleton
  • State
  • Strategy
  • Template Method
  • Visitor
  • Abstract Factory
  • Adapter
  • Bridge
  • Chain of Responsibility
  • Command
  • Composite
  • Decorator
  • Façade
  • Factory Method
  • Flyweight

11
Design Pattern Classifications
  • Creational Patterns
  • Concerned with the process of object creation
  • Structural Patterns
  • Concerned with the composition of classes or
    objects
  • Behavioral Patterns
  • Concerned with the characterizing the ways in
    which classes or objects interact and distribute
    responsibility.

12
Organization of Design Patterns
  • We organize our catalog of defined design
    patterns based on two criteria
  • Purpose That is, whether the pattern has a
    creational, structural, or behavioral purpose.
  • Scope That is, whether the pattern applies
    primarily to objects or classes.

13
Design Pattern Classification
14
Deciding Which D.P. to Use
  • Consider how design patterns solve design
    problems
  • Scan the intent attributes of each design pattern
  • Study how patterns interrelate
  • Study patterns of like purpose
  • Examine a cause of redesign
  • Consider what should be variable in your design

15
How to Use a Design Pattern
  1. Read a pattern once through for an overview
  2. Study the structure
  3. Look at the sample code
  4. Choose names for pattern participants that are
    meaningful in the application context
  5. Define the classes

16
How to Use a Design Pattern
  1. Define application-specific names for operations
    in the pattern
  2. Implement the operations to carry out the
    responsibilities and collaborations in the pattern

17
Design Pattern Example
  • Lets look at the simplest design pattern in the
    catalog, the singleton.

18
Singleton
  • Intent
  • Ensure a class has only one instance, and
    provide a global point of access to it.
  • Motivation
  • It is important that some classes have only one
    instance.
  • Ex There can be many printers in a system, but
    there should only be one print spooler.
  • Make the class itself responsible for keeping
    track of its sole instance
  • Applicability
  • Use when there must be exactly one instance of a
    class, yet must be accessible to clients from a
    well known access point.

19
Singleton
  • Structure

20
Singleton
  • Benefits
  • Controlled access to sole instance.
  • Reduced name space better than using global
    variables.
  • Permits a variable number of instances if you
    change your mind and want to allow more than one
    instance of the Singleton class.
  • Implementation
  • Ensuring a unique instance.
  • The design pattern will give you a lot of some
    sample code to illustrate how to implement.

21
Singleton
  • Class Definition
  • class Singleton (
  • public
  • static Singleton Instance()
  • protected
  • Singleton()
  • private
  • static Singleton _instance
  • )
  • Corresponding Implementation
  • Singleton Singleton_instance 0
  • Singleton SingletonInstance()
  • if (_instance 0)
  • _instance new Singleton
  • return _instance

22
Singleton
  • Sample Code
  • In addition to the sample code given in the
    implementation section, sample code for a
    practical implementation is given here.
  • class MazeFactory (
  • public
  • static MazeFactory Instance()
  • // existing interface goes here
  • protected
  • MazeFactory()
  • private
  • static MazeFactory _instance
  • )
  • MazeFactory MazeFactory_instance 0
  • MazeFactory MazeFactoryInstance()
  • if (_instance 0)
  • _instance new MazeFactory
  • return _instance
  • The sample code section will also demonstrate
    example subclasses.

23
Singleton
  • Known Uses
  • Smalltalk-80Par90
  • InterViews user interface toolkitLCI02 used
    this design pattern to access the unique instance
    of its Session and WidgetKit classes.
  • Related Patterns
  • Many patterns can be implemented by using the
    Singleton pattern. Example Abstract Factory,
    Builder, and Prototype.
Write a Comment
User Comments (0)
About PowerShow.com