Your SlideShare is downloading. ×
0
Design Patterns
  illustrated




        Herman Peeren          May 31st 2010
              (DP-illustrations: Nelleke Ve...
Design Patterns
●●   recipes against common (OO-) programming problems
●●   code reuse: no need to reinvent the wheel
●●  ...
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/pas...
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 clas...
some code smells:
1.●   duplicate code: DRY
2.●   long method, huge class: SRP
3.●   combinatorial explosion (similar clas...
Classic pattern categories
creational, structural and behavioral patterns:

●●   creational: object instantiation
●●   str...
Creational design patterns
●● Factory Method: Allow subclasses to “decide”
   which class to instantiate.
●● Abstract Fact...
Factory Method
    Provide an interface for the creation of objects.
    Allow subclasses to “decide” which class to insta...
Abstract Factory
    Povide an interface for creating families of related
    or dependent objects. A factory for factorie...
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.




                  ...
Joomla!
●●   JFactory: a class with static methods to instantiate objects
     like JDatabase, JUser, JDocument, JTemplate...
“Every
      advantage
        has its
   disadvantages”
(free to Johan Cruyff,
Dutch Football Pattern Designer
and Ajax-f...
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 fr...
Adapter (= Wrapper)
c   Adapt an interface to an expected interface.
Joomla!
●●   new in 1.6: JAdapter and JAdapterInstance

●●   JUpdateAdapter extends JAdapterInstance

     JUpdaterExtensi...
Bridge
    Decouple an abstraction from its implementation.




                                                          ...
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.
●● C...
Command
    Encapsulate a command request in an object.




                      YOU,DO YOUR
                         TAS...
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.: ex...
Interpreter
    Domain -> (little) language -> grammar -> objects
    (DSL)



              HÉ!         he means:
       ...
Iterator
    Enable sequential access to collection elements, without showing
    the underlying data-structures (array, l...
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

                         ...
Joomla!
●●   JObserver and JObservable
●●   JObserver extended by JEvent and that by JPlugin




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



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




                       P...
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.




        ...
extra: Mixin
●●   Used in Nooku (and a.o. in Ruby and Python)
●●   a kind of abstract class that is mixed into another obj...
Golden OO-principles

 ●●   encapsulate what varies, OCP

 ●●   1class, 1 responsibility, SRP

 ●●   program against an in...
Some books
GOF: 23 “classical” patterns:




                               very nice!


                    classic,
    ...
good start




       Fowler:
       extended
       e.g. with
       patterns
       for web



Fowler: also
known from
r...
Resign Patterns:
Ailments of Unsuitable Project-Disoriented Software
Joomla!
Could be improved by studying design patterns and applying
them.

                                           Loose...
Questions?



Contact info:
Herman Peeren
herman@yepr.nl
© Yepr
The artwork in this presentation is provided under the
Cre...
Design Patterns Illustrated
Upcoming SlideShare
Loading in...5
×

Design Patterns Illustrated

17,621

Published on

Illustrations to show the essence of the 23 "classical" Gang Of Four design patterns. Plus some general info about object oriented progr

Published in: Technology
6 Comments
61 Likes
Statistics
Notes
  • That's a good idea to illustrate the patterns with real-life metaphors, though some are more difficult than others. Cheers
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • The video is also available now: http://jandbeyond.org/video/monday-may-31st-2010/item/229-design-patterns-illustrated-herman-peeren.html

    Unfortunately the video doesn't show the slides. You should see both at the same time.

    Before the talk I showed a picture of Saint Isidoro di Sevilla: http://sjsm.files.wordpress.com/2009/04/san-isidoro2-jpegggg.jpg . That is what I talk about in the beginning.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Thanks for the rectification’s Herman. One further remark, the coupling you describe with mixins is actually an advantage and it’s done by design :

    - If you mix in a method in a base class, you can later choose to override that method in an extended class. This give you the ability to re-use the mixin functionality and specialize it by extending the class.

    - If you mix in a method and you want to override that method you could also do so by mixing in a new mixin that specifies the same method. The mixins are mixed using a LIFO array.

    In both scenarios you are not coupling, but de-coupling.

    As a bonus, the mixin implementation in Nooku Framework is even more powerful then the one in Rails. We have added the concept of dynamic and selective mixing.

    - Dynamic mixing allows you to instantiate a mixin and mix it with multiple objects as in a one to many relationship. This offers a number of benefits and some interesting flexibility.

    - Selective mixing allows the mixin to make available a number of it’s public methods to the mixer based on a set of conditions, or it can choose to not make available any methods at all.

    Both the dynamic mixing and selective mixing bring Nooku Framework very close to the basic concepts of AOP (Aspect Oriented Programming).

    Hope this helps to further clarify.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Comment by @johanjanssens via Twitter: slide 47 : NFW mixes objects against their public interface mixing only public methods and properties.

    That is a comment on my remark 'beware of tight coupling'. I made that remark mainly because IMHO there could be a problem if mixing in a method with a name that allready exists in the 'native' or mixed methods of that KObject. You have to know those names in order to prevent conflicts. So that is tight coupling.

    A mixin is a very powerful and handy way to put the same functionality in several parts in the code. Nothing wrong with it. I think it is very useful if the number of mixins is limited and their methods are treated as 'reserved words'.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Correction for slide 35 (Chain of Command):

    The commands in a KCommandChain in Nooku are command-objects (see: Command-pattern). They are KCommandHandlers, that implement the KCommandInterface. So: they are objects that execute their task upon calling their execute()-method.

    When running the command-chain you run a task and look in the chain which command-object will handdle it. Just like here:
    https://www.ibm.com/developerworks/library/os-php-designptrns/#N101DE
    (the IComand in that code has the same purpose as the KCommandInterface in Nooku).
    The NFW chain of command == chain of responsibility + command pattern.
    Thank you, Johan, for showing this to me.

    So: I was wrong thinking the KCommandChain was just a kind of macro, meant for running a series of commands. The confusion is that the commandhandlers are called 'commands'. The commandhandlers are command-objects and they handle the request (the parameters of run()).
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
17,621
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
1,379
Comments
6
Likes
61
Embeds 0
No embeds

No notes for slide

Transcript of "Design Patterns Illustrated"

  1. 1. Design Patterns illustrated Herman Peeren May 31st 2010 (DP-illustrations: Nelleke Verhoeff)
  2. 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. 3. The one constant in software development:
  4. 4. The one constant in software development: CHANGE!
  5. 5. The one constant in software development: CHANGE! I knew it ...
  6. 6. Ideal: code as modular black boxes
  7. 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)
  8. 8. Avoid: tight coupling!
  9. 9. It might get you into trouble...
  10. 10. Beware of: Code smells!
  11. 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. 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. 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. 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. 15. Factory Method Provide an interface for the creation of objects. Allow subclasses to “decide” which class to instantiate. c
  16. 16. Abstract Factory Povide an interface for creating families of related or dependent objects. A factory for factories. c
  17. 17. Builder Seperate the construction process (how) of a complex object from the concrete representations (what). c
  18. 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. 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. 20. “Every advantage has its disadvantages” (free to Johan Cruyff, Dutch Football Pattern Designer and Ajax-fan...)
  21. 21. Prototype Make variations on copies of a basic-object. COPY-SERVICE c
  22. 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. 23. Adapter (= Wrapper) c Adapt an interface to an expected interface.
  24. 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. 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. 26. Composite Create a tree structure for part-whole hierarchies. A node is also a (part of a) tree. Recursive: c
  27. 27. Decorator Add extra functionallity (at runtime), while keeping the interface the same. Matroushka’s... c
  28. 28. Decorator Nooku ●● KPatternDecorator: a general decorator ●● e.g. extended by KDecoratorJoomlaApplication
  29. 29. Facade Provide a general (simpler) interface for a set of interfaces. looks simple c
  30. 30. Flyweight Use one instance of a class to provide many “virtual” instances. c
  31. 31. Proxy Provide a surrogate or placeholder for another object to control access to it. c
  32. 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. 33. Command Encapsulate a command request in an object. YOU,DO YOUR TASK! TASK TASK LIGHT LIGHT ON OFF c
  34. 34. Chain of Responsibility c Define a method of passing a request among a chain of objects.
  35. 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. 36. Interpreter Domain -> (little) language -> grammar -> objects (DSL) HÉ! he means: do this, do that, and after finishing it, go there! c
  37. 37. Iterator Enable sequential access to collection elements, without showing the underlying data-structures (array, list, records, etc) next next c
  38. 38. Mediator c Layer in between: communication via one object.
  39. 39. Memento Save and restore the internal state of an object. ME c
  40. 40. Observer Notify “subscribers” of changes. ME NO ME ME WHO? c
  41. 41. Joomla! ●● JObserver and JObservable ●● JObserver extended by JEvent and that by JPlugin Nooku ●● KPatternObserver and KPatternObservable
  42. 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. 43. Strategy When something can be done in several ways, make those ways interchangeable. POSSI- BILITIES c
  44. 44. Template Method The skeleton of an algorithm is fixed, but parts can be filled in differently. c
  45. 45. Visitor Make a kind of plugin-possibility for methods: in that way me- thods can be added in runtime. printservice! c
  46. 46. 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....
  47. 47. 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
  48. 48. Some books GOF: 23 “classical” patterns: very nice! classic, The Book handy examples
  49. 49. good start Fowler: extended e.g. with patterns for web Fowler: also known from refactoring combi: re- factoring & patterns
  50. 50. Resign Patterns: Ailments of Unsuitable Project-Disoriented Software
  51. 51. Joomla! Could be improved by studying design patterns and applying them. Loose coupling...
  52. 52. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×