Design Patterns and the Real World - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Design Patterns and the Real World

Description:

Many people say: 'A pattern is a proven solution to a problem in a context.' Pattern must: ... Intent: Provide a way to access the elements of a aggregate ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 36
Provided by: gerdschau
Category:
Tags: design | love | patterns | real | say | to | unique | ways | world | you

less

Transcript and Presenter's Notes

Title: Design Patterns and the Real World


1
Design Patterns and the Real World
  • PHP-Conference 2003
  • Mai 9th 2003, Amsterdam
  • Gerd Schaufelberger

2
Agenda
  • About the speaker
  • Software Engineering
  • Pattern Introduction
  • Exhausting Example
  • Using Patterns How-to
  • Object-Oriented Design and Patterns
  • Pattern Catalogues and examples
  • Further Reading

3
Gerd Schaufelberger
  • Web Application Developer at Metrix Internet
    Design GmbH in Karlsruhe/Germany
  • Publishing OS on http//www.php-tools.net
  • Contributor to the German PHP Magazine

4
Software Engineering
  • Goals
  • Increase productivity
  • Increase quality
  • Decrease costs
  • Methods
  • Methodical design
  • Reusability
  • Better maintainability

5
Engineering Problems
  • Developers and maintainers need to know and
    understand the system.
  • Classes are very small parts of a system
  • Reusing classes often does not increase
    productivity
  • (Class-)Libraries only support the reusability of
    code
  • Methods that ease understanding software and
    increases reusability are needed

6
What are Patterns?
  • Many people say "A pattern is a proven solution
    to a problem in a context.
  • Pattern must
  • solve a problem
  • be related to a context
  • be general to fit in many situations
  • impart knowledge (information how the pattern
    solves the problem)
  • have a unique name

7
Pattern Definition
  • As an element in the world, each pattern is a
    relationship between a certain context, a certain
    system of forces which occurs repeatedly in that
    context, and a certain spatial configuration
    which allows these forces to resolve themselves.
  • As an element of language, a pattern is an
    instruction which shows how this spatial
    configuration can be used, over and over again,
    to resolve the given system of forces, whenever
    the context makes it relevant.
  • Alexander, 1979, A Timeless Way of Building

8
Pattern Anatomy
  • Essential contents of patterns
  • Pattern name
  • Description of the problem
  • Description of the solution
  • Consequences
  • The quality of a pattern depends on the
  • quality of the document.

9
Pattern Prototype (1)
  • Name Prototype (Creational Pattern)
  • Intend Specify the kinds of objects to create
    using a prototypical instance, and create new
    objects by copying this prototype.
  • Diagram

10
Pattern Prototype (2)
  • Solution
  • Create an ancestor product class which defines an
    abstract or default clone method.
  • Create specific product classes by inheriting
    from the ancestor product class and implementing
    the clone method to create a copy of itself.
  • During system initialization, or other
    appropriate time, create prototype instances of
    each specific product.
  • Whenever a real instance of a particular product
    is needed, call the appropriate products clone
    method and use the object returned.

11
Pattern Prototype (3)
  • Applicability
  • Classes to instantiate are specified at run-time,
    or,
  • To avoid building a hierarchy of factories, or
  • It is more appropriate to clone objects with
    state information than to instantiate and
    initialize the class(es) manually.

12
Pattern Prototype (4)
  • Consequences
  • Products may be dynamically added and removed at
    run-time.
  • May reduce the number of object classes needed by
    varying object values.
  • Simplifies construction of complex object
    aggregations
  • Reduces or eliminates the need to implement a
    hierarchy of factories that parallels a hierarchy
    of product types

13
Extract Information
  • Extract essential information from the Pattern
    description
  • Name The Name (Prototype) identifies the
    Pattern. Add these names to your vocabulary!
  • Intent Tells what the Pattern is good for. Keep
    in mind!
  • Diagram It show how to implement the Pattern.
    Usually drawn as UML-Diagrams. It gives you an
    idea how the Pattern work. Forget it you know
    where you can find it.
  • Read this and you know whether it
  • is worth reading more.

14
More Information
  • You find the Prototype Pattern may help you? Read
    more
  • Solution Some more detailed view what the
    Pattern is good for.
  • Applicability Tells you in which environment the
    Pattern makes sense.
  • Consequences This very important part talks
    about disadvantages and pitfalls.
  • often there is additional information available.

15
Make Your Decision
  • Ask yourself Is this pattern useful in my
    case?
  • The description of the pattern can help you to
    make a decision You know
  • what this pattern intents to solve
  • how it can be implemented
  • the positive and the negative aspects
  • You know this in advance,
  • before implementation and testing.

16
No Magic!
  • The Prototype Pattern contains nothing magical
  • Easy to understand
  • Easy to implement
  • For sure you already used Patterns without
    knowing it
  • Design Patterns are nothing mystic
  • disappointed?

17
Object Oriented Design
  • Object-Oriented design and programming is widely
    spread and well known by programmers.
  • Thinking in objects and classes are good methods
    in software development.
  • PHP supports basic OO-features, but

18
Objects and PHP (PHP4)
  • Current version of PHP (PHP4)
  • Only some OO-features (classes, inheritance,
    etc.)
  • No destructors
  • Only some information hiding
  • No private attributes and methods
  • No protection from inheritance
  • PHP4 is not type-safe
  • Object-oriented programming with PHP4 needs
    discipline

19
Objects and PHP (PHP5)
  • PHP5 grows-up to an adult OO-programming language
  • Better encapsulation (private/protected
    attributes and member methods)
  • Type-Hinting allows better type-safe programming
  • New concept for constructors and destructors
  • and more
  • Object-oriented programming with PHP5 needs
  • less discipline

20
Patterns and Objects
  • The implementation of Patterns were usually
    described as class-diagrams.
  • Some Patterns (like Prototype) are about
    creating objects which only makes sense in an
    object-oriented environment.
  • OO-programming is not mandatory for Patterns.
    (Patterns describe a solution for specific
    problems.)

21
and with PHP
  • Even if PHP (PHP4) is no real OO-programming
    language, Patterns are useful.
  • Whether a Pattern can be implemented in PHP
    depends on the Pattern. Some Patterns dont make
    even sense for PHP-Programmes.
  • Design Patterns want solve Problems and not to
    force you OO-programming.

22
Pattern Catalogues (1)
  • Patterns (like Prototype) were collected in
    Catalogues.
  • In the Internet e.g. http//www.patterndigest.com
  • In books like Design Patterns
  • Catalogues contain complete Pattern-Description
    Name, Intent, Diagram, Solution, Consequences,
  • Catalogues usually contain references to related
    Patterns, related problems, related environments,

23
Pattern Catalogues (2)
  • Catalogues (usually) sort Design Patterns by
    categories
  • Structural Patterns
  • Creational Patterns
  • Behavioural Patterns
  • Relationship Patterns
  • Downcasting Patterns
  • Property Patterns

24
Pattern Catalogues (3)
  • Usage of catalogues
  • Browse through the catalogues for Patterns
    matching to your problem.
  • Study class-diagrams before implementing
  • Some catalogues also contain example code
    (usually in Java or C ?)

25
Pattern Catalogues (4)
  • These catalogues contain proven solutions for
    various problems. Finding a good solution within
    a catalogue is more easy than thinking about
    yourself.
  • Stop thinking, start reading.

26
Some Patterns
  • Some example Patterns
  • Iterator
  • Prototype
  • Command
  • shameless stolen from a pattern catalogue.

27
The Iterator (1)
  • Name Iterator (Behavioural Pattern)
  • Intent Provide a way to access the elements of a
    aggregate object without exposing its underlying
    representation.
  • Diagram

28
The Iterator (2)
  • The Client uses Iterator-Objects to walk through
    any aggregate.
  • Replaces loops ( for, while, etc.)
  • Easy to implement
  • Abstraction for accessing data
  • Simple interface for all kind of aggregates
  • Use more than one iterator at the same time

29
The Iterator (3)
  • agg new aggregate() //
    instantiate the aggregate
  • iter1 agg-gtgetIterator() //
    get a matching iterator
  • ele iter1-gtfirst()
    // get the first element
  • while( ele )
  • ele-gtdoSomething() //
    perform operation
  • ele iter1-gtnext()
    // get the next element
  • iter2 agg-gtgetIterator() //
    get independent iterator
  • ele iter2-gtlast()
    // get the last element

30
Prototype
  • Name Prototype (Creational Pattern)
  • This pattern was already shown.
  • The class itself is responsible for duplicating
    the client just calls the clone() method.

31
Command (1)
  • Name Command (Behavioural Pattern)
  • Intent Encapsulate a request as a parameterized
    object allow different requests, queue or log
    requests and support undoable operations
  • Diagram

32
Command (2)
  • Some benefits
  • Decouples objects from operations
  • Allows logging, do, undo
  • Easy to implement Macro-Commands

33
Summarize
  • Patterns names may ease communication
  • Patterns ease understanding software
  • Patterns are proven solutions
  • Patterns are well documented
  • Benefits and disadvantages are known in advance
  • Dogs love patterns
  • Design Patterns can ease your life
  • use them.

34
Further Reading
  • WWW
  • Pattern Homepage http//www.hillside.net
  • Pattern Digest http//www.patterndigest.com
  • Patterns and PHP http//www.phppatterns.com
  • Books
  • Graig Larman Applying UML and Patterns, 2001
  • Van Duyne, Hong, Landay Design of Sites, 2002
  • Gamma, Helm, Vlissides, Johnson Design Patterns,
    1994

35
The End
  • Thank you!
  • Get in contact
  • http//www.php-tools.net
  • gerd_at_php-tools.net
  • Thanks to
  • Sebastian Mordziol, Stephan Schmidt,
  • Metrix Internet Design GmbH
Write a Comment
User Comments (0)
About PowerShow.com