SlideShare a Scribd company logo
Design Patterns
  illustrated




        Herman Peeren          May 31st 2010
              (DP-illustrations: Nelleke Verhoeff)
Design Patterns
●●   recipes against common (OO-) programming problems
●●   code reuse: no need to reinvent the wheel
●●   common language
●●   GOF: 23 “classical” patterns



                     classic,
                     The Book




                           very nice!
The one constant in software development:
The one constant in software development:



CHANGE!
The one constant in software development:



CHANGE!

                     I knew it ...
Ideal: code as modular black boxes
Wish list and OOP-principles
●●   loose coupling: 1 change = ceteris paribus

●●   code reuse (is not the same as copy/paste)

●●   open for extension, closed for modification

●●   encapsulate what varies

●●   single responsibility principle

●●   program against an interface not against an imple-
     mentation. Dependency injection

●●   prefer composition over inheritance

(in fact this is all the same with different words)
Avoid: tight coupling!
It might get you into trouble...
Beware of:



Code
smells!
some code smells:
1.●   duplicate code: DRY
2.●   long method, huge class: SRP
3.●   combinatorial explosion (similar classes)
4.●   conditional complexity, switch statements
5.●   “exhibitionistic” classes
some code smells:
1.●   duplicate code: DRY
2.●   long method, huge class: SRP
3.●   combinatorial explosion (similar classes)
4.●   conditional complexity, switch statements
5.●   “exhibitionistic” classes
Classic pattern categories
creational, structural and behavioral patterns:

●●   creational: object instantiation
●●   structural: larger structures of classes or objects
●●   behavioral: interaction and distribution of responsibility
Creational design patterns
●● Factory Method: Allow subclasses to “decide”
   which class to instantiate.
●● Abstract Factory: Encapsulate a set of analo-
   gous factories that produce families of objects.
●● Builder: Encapsulate the construction of com-
   plex objects from their representation; so, the
   same building process can create various repre-
   sentations by specifying only type and content.
●● Singleton: Ensure that only a single instance of
   a class exists and provide a single method for
   gaining access to it.
●● Prototype: Create an initialized instance for
   cloning or copying.
Factory Method
    Provide an interface for the creation of objects.
    Allow subclasses to “decide” which class to instantiate.
c
Abstract Factory
    Povide an interface for creating families of related
    or dependent objects. A factory for factories.
c
Builder
    Seperate the construction process (how) of a complex object
    from the concrete representations (what).
c
Singleton
    Ensure a class only has one instance, and provide a global
    point of access to it.




                               Oh, I’m so
                                loooooooonly
c
Joomla!
●●   JFactory: a class with static methods to instantiate objects
     like JDatabase, JUser, JDocument, JTemplate, etc.
●●   most of those methods are singletons




Nooku
●●   KFactory: any class can be instantiated
●●   get() = singleton, tmp() = any instantiation
“Every
      advantage
        has its
   disadvantages”
(free to Johan Cruyff,
Dutch Football Pattern Designer
and Ajax-fan...)
Prototype
    Make variations on copies of a basic-object.




                              COPY-SERVICE
c
Structural design patterns
●● Adapter: Adapt an interface to an expected
   interface.
●● Bridge: Decouple an interface from its
   implementation.
●● Composite: Create a tree structure for
   part-whole hierarchies.
●● Decorator: Extend functionality dynamically.
●● Facade: Simplify usage by defining a high-level
   interface.
●● Flyweight: Support fine-grained objects
   efficiently by sharing.
●● Proxy: Represent an object with another object
   for access control.
Adapter (= Wrapper)
c   Adapt an interface to an expected interface.
Joomla!
●●   new in 1.6: JAdapter and JAdapterInstance

●●   JUpdateAdapter extends JAdapterInstance

     JUpdaterExtension & JUpdaterExtension
     extend JUpdateAdapter


wanted: documentation and examples!
what does it adapt?
Bridge
    Decouple an abstraction from its implementation.




                                                                COLA
    1 LITER                                                     1 LITER

                                                      COLA
              1 LITER                                 1 LITER
                                               COLA
                        MILK

                                             COLA
                               MILK                 COLA
                   MILK
c
Composite
    Create a tree structure for part-whole hierarchies. A node is also a
    (part of a) tree. Recursive:
c
Decorator
    Add extra functionallity (at runtime),
    while keeping the interface the same.
    Matroushka’s...
c
Decorator




Nooku
●●   KPatternDecorator: a general decorator
●●   e.g. extended by KDecoratorJoomlaApplication
Facade
    Provide a general (simpler) interface for a set of interfaces.




              looks
             simple
c
Flyweight
    Use one instance of a class to provide many
    “virtual” instances.
c
Proxy
    Provide a surrogate or placeholder for another object
    to control access to it.
c
Behavioral design patterns
●● Chain of Responsibility: Define a method of passing a
request among a chain of objects.
●● Command: Encapsulate a command request in an object.
●● Interpreter: Allow inclusion of language elements in an appli-
cation.
●● Iterator: Enable sequential access to collection elements.
●● Mediator: Define simplified communication between classes.
●● Memento: Save and restore the internal state of an object.
●● Observer: Define a scheme for notifying objects of changes to
another object.
●● State: Alter the behavior of an object when its state changes.
●● Strategy: Encapsulate an algorithm inside a class.
●● Template Method: Allow subclasses to redefine the steps of
an algorithm.
●● Visitor: Define a new operation on a class without changing it.
Command
    Encapsulate a command request in an object.




                      YOU,DO YOUR
                         TASK!




          TASK                                    TASK
          LIGHT                                   LIGHT
           ON                                      OFF
c
Chain of Responsibility
c
    Define a method of passing a request among a chain of objects.
Nooku
●●   KCommandChain
     + KCommandContext, KCommandEvent, KCommandHandler and
     KCommandInterface

●●    N.B.: executes a series of commands
     instead of passing a command to a series of handlers
     (like in Tapestry e.g.) ...correct me if I’m wrong....
Interpreter
    Domain -> (little) language -> grammar -> objects
    (DSL)



              HÉ!         he means:
                      do this, do that,
                    and after finishing it,
                          go there!
c
Iterator
    Enable sequential access to collection elements, without showing
    the underlying data-structures (array, list, records, etc)


           next




    next
c
Mediator
c   Layer in between: communication via one object.
Memento
    Save and restore the internal state of an object.




                                          ME
c
Observer
    Notify “subscribers” of changes.



                                            ME

                                                 NO   ME
                                       ME


                 WHO?
c
Joomla!
●●   JObserver and JObservable
●●   JObserver extended by JEvent and that by JPlugin




Nooku
●●   KPatternObserver and KPatternObservable
State
Let an object show other methods after a change of internal
state (as if it changes it’s class).



      in a.....hick......different state,
      ....hick....I behave differently....hick.....




c
Strategy
    When something can be done in several ways, make those
    ways interchangeable.




                       POSSI-
                       BILITIES
c
Design Patterns Illustrated
Template Method
    The skeleton of an algorithm is fixed, but parts can be filled in
    differently.
c
Visitor
    Make a kind of plugin-possibility for methods: in that way me-
    thods can be added in runtime.




          printservice!
c
extra: Mixin
●●   Used in Nooku (and a.o. in Ruby and Python)
●●   a kind of abstract class that is mixed into another object
●●   all methods of the mixin are now part of the object
●●   handle with care: beware of tight coupling....
Golden OO-principles

 ●●   encapsulate what varies, OCP

 ●●   1class, 1 responsibility, SRP

 ●●   program against an interface, not against an imple-
      mentation, DIP

 ●●   prefer composition over inheritance

 ●●   loose coupling, modular black boxes
Some books
GOF: 23 “classical” patterns:




                               very nice!


                    classic,
                    The Book




                               handy
                               examples
good start




       Fowler:
       extended
       e.g. with
       patterns
       for web



Fowler: also
known from
refactoring




        combi: re-
        factoring
        & patterns
Resign Patterns:
Ailments of Unsuitable Project-Disoriented Software
Joomla!
Could be improved by studying design patterns and applying
them.

                                           Loose coupling...
Questions?



Contact info:
Herman Peeren
herman@yepr.nl
© Yepr
The artwork in this presentation is provided under the
Creative Commons Public License for noncommercial use.
http://creativecommons.org/licenses/by-nc/3.0/legalcode

More Related Content

PDF
Java Course 11: Design Patterns
PPS
Java rmi
PDF
Java Thread Synchronization
PPTX
Packages in java
PPTX
Constructor in java
PPTX
Asp.NET Validation controls
PDF
JUnit & Mockito, first steps
PDF
Java Concurrency by Example
Java Course 11: Design Patterns
Java rmi
Java Thread Synchronization
Packages in java
Constructor in java
Asp.NET Validation controls
JUnit & Mockito, first steps
Java Concurrency by Example

What's hot (20)

PPT
Java Servlets
PPTX
Java constructors
PPTX
JAVA AWT
PPT
Design Patterns
PPTX
Introduction to Java
PDF
Introduction to Java 11
PPT
Packages in java
PPT
Shell programming
PPTX
Multithreading in java
PDF
JavaFX Overview
PDF
Quick Guide with Linux Command Line
PPTX
Multithreading and concurrency in android
PDF
Introduction to Design Pattern
PPTX
Polymorphism presentation in java
PPTX
JDBC ppt
PPTX
Clases en java
PPTX
Bash shell scripting
PDF
Shell scripting
PDF
Angular directives and pipes
Java Servlets
Java constructors
JAVA AWT
Design Patterns
Introduction to Java
Introduction to Java 11
Packages in java
Shell programming
Multithreading in java
JavaFX Overview
Quick Guide with Linux Command Line
Multithreading and concurrency in android
Introduction to Design Pattern
Polymorphism presentation in java
JDBC ppt
Clases en java
Bash shell scripting
Shell scripting
Angular directives and pipes
Ad

Viewers also liked (11)

PPTX
saleh nazzal
PPTX
عربي
PPT
Code Refactoring
PDF
Refactoring - An Introduction
PDF
The Decorator Pattern
PDF
Refactoring 101
PDF
Refactoring
PDF
Design patterns
PDF
Design Patterns這樣學就會了:入門班 Day1 教材
PPTX
Design Patterns - 01 Introduction and Decorator Pattern
PDF
SlideShare 101
saleh nazzal
عربي
Code Refactoring
Refactoring - An Introduction
The Decorator Pattern
Refactoring 101
Refactoring
Design patterns
Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns - 01 Introduction and Decorator Pattern
SlideShare 101
Ad

Similar to Design Patterns Illustrated (20)

PDF
Designpatterns illustrated
PDF
software engineering Design Patterns.pdf
PPT
Design Patterns
PDF
Design patterns illustrated-2015-03
PPT
Chapter 4_Introduction to Patterns.ppt
PPT
Chapter 4_Introduction to Patterns.ppt
PDF
Design Pattern in Software Engineering
PDF
Design Pattern Cheatsheet
PDF
Design patterns illustrated 010PHP
KEY
Design Patterns Course
PPT
Design patterns-sav
PDF
Basic design pattern interview questions
PPTX
Design patterns in Object oriented analysis and design
PPTX
Go f designpatterns 130116024923-phpapp02
PPTX
OOP design patterns
PPT
Design pattern
PPTX
Software Patterns
DOCX
Design patterns
PPT
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
PDF
Java-Design-Patterns.pdf
Designpatterns illustrated
software engineering Design Patterns.pdf
Design Patterns
Design patterns illustrated-2015-03
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
Design Pattern in Software Engineering
Design Pattern Cheatsheet
Design patterns illustrated 010PHP
Design Patterns Course
Design patterns-sav
Basic design pattern interview questions
Design patterns in Object oriented analysis and design
Go f designpatterns 130116024923-phpapp02
OOP design patterns
Design pattern
Software Patterns
Design patterns
Software Architecture and Project Management module III : PATTERN OF ENTERPRISE
Java-Design-Patterns.pdf

More from Herman Peeren (19)

PDF
ProjectionalForms-2023-11-14.pdf
PDF
ExtensionGenerator-JoomlaDagen2023-slides.pdf
PDF
Cut & Shave
PDF
Programmeren, talen en het begrijpen van de wereld
PDF
Dci in PHP
PDF
Improve our PHP code with ideas from Functional Programming
PDF
DCI DDD-BE April 2015
PDF
Event Sourcing
PDF
Next Generation Joomla!
PDF
Behat, Behavioral Driven Development (BDD) in PHP
PDF
Print, geen kunst aan
PDF
Jooctrine - Doctrine ORM in Joomla!
PDF
#jd12nl Joomla 2.5 extensies
PDF
#jd12nl Seblod 2
PDF
Jug010 120320-templates
PDF
Joomla2.0 architecture
PDF
Webservices: connecting Joomla! with other programs.
PDF
Commercial gpljoomla
PPS
Flash templates for Joomla!
ProjectionalForms-2023-11-14.pdf
ExtensionGenerator-JoomlaDagen2023-slides.pdf
Cut & Shave
Programmeren, talen en het begrijpen van de wereld
Dci in PHP
Improve our PHP code with ideas from Functional Programming
DCI DDD-BE April 2015
Event Sourcing
Next Generation Joomla!
Behat, Behavioral Driven Development (BDD) in PHP
Print, geen kunst aan
Jooctrine - Doctrine ORM in Joomla!
#jd12nl Joomla 2.5 extensies
#jd12nl Seblod 2
Jug010 120320-templates
Joomla2.0 architecture
Webservices: connecting Joomla! with other programs.
Commercial gpljoomla
Flash templates for Joomla!

Recently uploaded (20)

PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Getting Started with Data Integration: FME Form 101
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Heart disease approach using modified random forest and particle swarm optimi...
A comparative study of natural language inference in Swahili using monolingua...
Univ-Connecticut-ChatGPT-Presentaion.pdf
OMC Textile Division Presentation 2021.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
A comparative analysis of optical character recognition models for extracting...
Getting Started with Data Integration: FME Form 101
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding
Spectroscopy.pptx food analysis technology
Encapsulation_ Review paper, used for researhc scholars
MIND Revenue Release Quarter 2 2025 Press Release
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Design Patterns Illustrated

  • 1. Design Patterns illustrated Herman Peeren May 31st 2010 (DP-illustrations: Nelleke Verhoeff)
  • 2. Design Patterns ●● recipes against common (OO-) programming problems ●● code reuse: no need to reinvent the wheel ●● common language ●● GOF: 23 “classical” patterns classic, The Book very nice!
  • 3. The one constant in software development:
  • 4. The one constant in software development: CHANGE!
  • 5. The one constant in software development: CHANGE! I knew it ...
  • 6. Ideal: code as modular black boxes
  • 7. Wish list and OOP-principles ●● loose coupling: 1 change = ceteris paribus ●● code reuse (is not the same as copy/paste) ●● open for extension, closed for modification ●● encapsulate what varies ●● single responsibility principle ●● program against an interface not against an imple- mentation. Dependency injection ●● prefer composition over inheritance (in fact this is all the same with different words)
  • 9. It might get you into trouble...
  • 11. some code smells: 1.● duplicate code: DRY 2.● long method, huge class: SRP 3.● combinatorial explosion (similar classes) 4.● conditional complexity, switch statements 5.● “exhibitionistic” classes
  • 12. some code smells: 1.● duplicate code: DRY 2.● long method, huge class: SRP 3.● combinatorial explosion (similar classes) 4.● conditional complexity, switch statements 5.● “exhibitionistic” classes
  • 13. Classic pattern categories creational, structural and behavioral patterns: ●● creational: object instantiation ●● structural: larger structures of classes or objects ●● behavioral: interaction and distribution of responsibility
  • 14. Creational design patterns ●● Factory Method: Allow subclasses to “decide” which class to instantiate. ●● Abstract Factory: Encapsulate a set of analo- gous factories that produce families of objects. ●● Builder: Encapsulate the construction of com- plex objects from their representation; so, the same building process can create various repre- sentations by specifying only type and content. ●● Singleton: Ensure that only a single instance of a class exists and provide a single method for gaining access to it. ●● Prototype: Create an initialized instance for cloning or copying.
  • 15. Factory Method Provide an interface for the creation of objects. Allow subclasses to “decide” which class to instantiate. c
  • 16. Abstract Factory Povide an interface for creating families of related or dependent objects. A factory for factories. c
  • 17. Builder Seperate the construction process (how) of a complex object from the concrete representations (what). c
  • 18. Singleton Ensure a class only has one instance, and provide a global point of access to it. Oh, I’m so loooooooonly c
  • 19. Joomla! ●● JFactory: a class with static methods to instantiate objects like JDatabase, JUser, JDocument, JTemplate, etc. ●● most of those methods are singletons Nooku ●● KFactory: any class can be instantiated ●● get() = singleton, tmp() = any instantiation
  • 20. “Every advantage has its disadvantages” (free to Johan Cruyff, Dutch Football Pattern Designer and Ajax-fan...)
  • 21. Prototype Make variations on copies of a basic-object. COPY-SERVICE c
  • 22. Structural design patterns ●● Adapter: Adapt an interface to an expected interface. ●● Bridge: Decouple an interface from its implementation. ●● Composite: Create a tree structure for part-whole hierarchies. ●● Decorator: Extend functionality dynamically. ●● Facade: Simplify usage by defining a high-level interface. ●● Flyweight: Support fine-grained objects efficiently by sharing. ●● Proxy: Represent an object with another object for access control.
  • 23. Adapter (= Wrapper) c Adapt an interface to an expected interface.
  • 24. Joomla! ●● new in 1.6: JAdapter and JAdapterInstance ●● JUpdateAdapter extends JAdapterInstance JUpdaterExtension & JUpdaterExtension extend JUpdateAdapter wanted: documentation and examples! what does it adapt?
  • 25. Bridge Decouple an abstraction from its implementation. COLA 1 LITER 1 LITER COLA 1 LITER 1 LITER COLA MILK COLA MILK COLA MILK c
  • 26. Composite Create a tree structure for part-whole hierarchies. A node is also a (part of a) tree. Recursive: c
  • 27. Decorator Add extra functionallity (at runtime), while keeping the interface the same. Matroushka’s... c
  • 28. Decorator Nooku ●● KPatternDecorator: a general decorator ●● e.g. extended by KDecoratorJoomlaApplication
  • 29. Facade Provide a general (simpler) interface for a set of interfaces. looks simple c
  • 30. Flyweight Use one instance of a class to provide many “virtual” instances. c
  • 31. Proxy Provide a surrogate or placeholder for another object to control access to it. c
  • 32. Behavioral design patterns ●● Chain of Responsibility: Define a method of passing a request among a chain of objects. ●● Command: Encapsulate a command request in an object. ●● Interpreter: Allow inclusion of language elements in an appli- cation. ●● Iterator: Enable sequential access to collection elements. ●● Mediator: Define simplified communication between classes. ●● Memento: Save and restore the internal state of an object. ●● Observer: Define a scheme for notifying objects of changes to another object. ●● State: Alter the behavior of an object when its state changes. ●● Strategy: Encapsulate an algorithm inside a class. ●● Template Method: Allow subclasses to redefine the steps of an algorithm. ●● Visitor: Define a new operation on a class without changing it.
  • 33. Command Encapsulate a command request in an object. YOU,DO YOUR TASK! TASK TASK LIGHT LIGHT ON OFF c
  • 34. Chain of Responsibility c Define a method of passing a request among a chain of objects.
  • 35. Nooku ●● KCommandChain + KCommandContext, KCommandEvent, KCommandHandler and KCommandInterface ●● N.B.: executes a series of commands instead of passing a command to a series of handlers (like in Tapestry e.g.) ...correct me if I’m wrong....
  • 36. Interpreter Domain -> (little) language -> grammar -> objects (DSL) HÉ! he means: do this, do that, and after finishing it, go there! c
  • 37. Iterator Enable sequential access to collection elements, without showing the underlying data-structures (array, list, records, etc) next next c
  • 38. Mediator c Layer in between: communication via one object.
  • 39. Memento Save and restore the internal state of an object. ME c
  • 40. Observer Notify “subscribers” of changes. ME NO ME ME WHO? c
  • 41. Joomla! ●● JObserver and JObservable ●● JObserver extended by JEvent and that by JPlugin Nooku ●● KPatternObserver and KPatternObservable
  • 42. State Let an object show other methods after a change of internal state (as if it changes it’s class). in a.....hick......different state, ....hick....I behave differently....hick..... c
  • 43. Strategy When something can be done in several ways, make those ways interchangeable. POSSI- BILITIES c
  • 45. Template Method The skeleton of an algorithm is fixed, but parts can be filled in differently. c
  • 46. Visitor Make a kind of plugin-possibility for methods: in that way me- thods can be added in runtime. printservice! c
  • 47. extra: Mixin ●● Used in Nooku (and a.o. in Ruby and Python) ●● a kind of abstract class that is mixed into another object ●● all methods of the mixin are now part of the object ●● handle with care: beware of tight coupling....
  • 48. Golden OO-principles ●● encapsulate what varies, OCP ●● 1class, 1 responsibility, SRP ●● program against an interface, not against an imple- mentation, DIP ●● prefer composition over inheritance ●● loose coupling, modular black boxes
  • 49. Some books GOF: 23 “classical” patterns: very nice! classic, The Book handy examples
  • 50. good start Fowler: extended e.g. with patterns for web Fowler: also known from refactoring combi: re- factoring & patterns
  • 51. Resign Patterns: Ailments of Unsuitable Project-Disoriented Software
  • 52. Joomla! Could be improved by studying design patterns and applying them. Loose coupling...
  • 53. Questions? Contact info: Herman Peeren [email protected] © Yepr The artwork in this presentation is provided under the Creative Commons Public License for noncommercial use. http://creativecommons.org/licenses/by-nc/3.0/legalcode