Plab Tirgul 12 Design Patterns - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Plab Tirgul 12 Design Patterns

Description:

Invented by Christopher Alexander, Architect. 'Each pattern describes a problem which ... Describes the elements that make up the design, their relationships, ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 20
Provided by: NirFri
Category:
Tags: design | patterns | plab | tirgul

less

Transcript and Presenter's Notes

Title: Plab Tirgul 12 Design Patterns


1
Plab Tirgul 12Design Patterns
2
What is a Design Pattern ?
  • Invented by Christopher Alexander, Architect.
  • 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, Christopher Alexander.
  • Originally referred to buildings and towns,
    applies to OOD as well.

3
What is a Design Pattern ?
  • A pattern has 4 elements
  • Name Naming a pattern immediately increases our
    design vocabulary.
  • Problem When to apply the pattern the problem
    and the context.
  • Solution Describes the elements that make up the
    design, their relationships, responsibilities,
    and collaborations. Not a specific
    implementation.
  • Consequence The results and trade-offs of
    applying the pattern. Allows evaluation between
    different patterns.

4
Why Design Patterns ?
  • Common terminology.
  • Solves practical problem.
  • Reuse of design and experience.
  • Improves the documentation and maintenance of
    existing systems.

5
Design Patterns The book
  • Written by the Gang Of four (Go4).

6
Design Patterns classification
  • Creational
  • Singleton
  • Abstract Factory
  • Factory Method
  • Structural
  • Composite
  • Behavioral
  • Strategy
  • Command
  • Iterator

7
Singleton
  • Ensures that at most a single instance exists,
    allows to access it.
  • Applications
  • Printer spooler.
  • An accounting system will be dedicated to serving
    one company.
  • Problem How do you keep track of number of
    instances ?

8
Singleton Structure
  • Note that the constructor is hidden (private)

Singleton static getInstance()method1()method2()
static uniqueInstance
return uniqueInstance
9
Decorator
  • Attaching and removing additional features to an
    object dynamically (run time) and independently.
  • Avoid complicating the API of the base class.
  • Examples
  • Word processor Create Bold, underline, or BOTH
    characters.
  • Windows system windows with scroll bar, border,
    header.

10
Notation
  • Part of the UML (Unified Modeling Language)

Inheritance
Association
Aggregation
Composite aggregation
11
Decorator Structure
Component operation()
ConcreteComponent operation()
Decoratoroperation()
component-gtoperation()
component
ConcreteDecoratorA operation() AddedBehavior()
ConcreteDecoratorB operation() AddedBehavior()
Decoratoroperation() AddedBehavior()
12
Strategy
  • Encapsulates a family of algorithms, making them
    interchangeable.
  • Strategy lets the algorithm vary independently
    from clients that use it.
  • Used when
  • many related classes differ only in their
    behavior. Strategies provide a way to configure a
    class with one of many behaviors.
  • you need different variants of an algorithm
    (space/time trade-off).
  • an algorithm uses data that clients shouldn't
    know about.
  • a class defines many behaviors, and these appear
    as multiple conditional statements in it's
    operations. Instead of many conditionals, move
    related conditional branches into their own
    Strategy class.
  • Examples
  • Comparator for data structure.
  • Modeling behavior of computer games characters.

13
Strategy Structure
StrategyAlgorithm()
strategy
Context
StrategyAAlgorithm()
StrategyBAlgorithm()
14
Abstract Factory
  • Provides an interface for creating objects
    without specifying their concrete classes.
  • When to use
  • a system should be independent of how its
    products are created, composed, and represented.
  • a system should be configured with one of
    multiple families of products.
  • a family of related product objects is designed
    to be used together, and you need to enforce this
    constraint.
  • you want to provide a class library of products,
    and you want to reveal just their interfaces, not
    their implementations
  • Applications
  • Implementation of different look and feel
    interface for windowing system.
  • Levels in computer games.
  • Improves portability across operating systems.

15
Abstract Factory Structure
AbstractFactory createA() createB()
AbstractProductA
Client
ProdA2
ProdA1
ConcreteFactory1 createA() createB()
ConcreteFactory2 createA() createB()
AbstractProductB
ProdB2
ProdB1
Creates
16
Consequences (Abstract Factory)
  • It isolates concrete classes client not even
    aware of concrete class name !
  • It makes exchanging product families easy.
  • It promotes consistency among products.
  • Supporting new kinds of products is difficult.

17
Factory Method
  • Define an interface for creating an object, but
    let subclasses decide which class to instantiate
  • Example
  • Used in Abstract Factory, but also can be used
    independently.

18
Factory Method Structure
Product
Creator method1()method2()FactoryMethod()
ConcreteCreator FactoryMethod()
Creates
ConcreteProduct
19
More on Design Patterns
  • Links
  • http//hillside.net/patterns/education.htm
  • http//mindview.net/Books/TIPatterns/
  • Object Oriented Design course (67615) very
    recommended !!
Write a Comment
User Comments (0)
About PowerShow.com