Design Patterns

2,641 views

Published on

A short overview about the design patterns described in Russ Olson's book "Design Patterns in Ruby".

0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,641
On SlideShare
0
From Embeds
0
Number of Embeds
36
Actions
Shares
0
Downloads
214
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Design Patterns

  1. 1. Design Patterns <ul><li>Part One </li></ul><ul><li>Part Two </li></ul>
  2. 2. Part 1 <ul><li>What design is </li></ul><ul><li>What it's not </li></ul><ul><li>Why patterns? </li></ul>
  3. 3. (Part 2 is about Patterns en détail)
  4. 4. What design is <ul><li>Process </li></ul><ul><ul><li>Thought </li></ul></ul><ul><ul><li>Methodology </li></ul></ul><ul><ul><li>Discussion </li></ul></ul><ul><li>Attitude </li></ul><ul><ul><li>Simplicity </li></ul></ul><ul><ul><li>Finding solutions for a given problem </li></ul></ul>
  5. 5. What it's not <ul><li>Enforcing schemas </li></ul><ul><li>Dictating solutions </li></ul><ul><li>Finding problems for a given solution </li></ul><ul><li>One Solution Fits All </li></ul>
  6. 6. Why patterns? <ul><li>reuse </li></ul><ul><ul><li>similar problems occur over and over again – just reuse the code </li></ul></ul><ul><li>separation </li></ul><ul><ul><li>when you separate concerns you should have a single point of failure for everything anyway </li></ul></ul>
  7. 7. Why patterns? <ul><li>reuse </li></ul><ul><ul><li>similar problems occur over and over again – with small but significant differences between one and the other </li></ul></ul><ul><li>separation </li></ul><ul><ul><li>separate those parts of the solution that change from those that stay the same </li></ul></ul>
  8. 8. Why patterns? <ul><li>reuse </li></ul><ul><ul><li>a pattern once identified can be used when ever you encounter a similar problem </li></ul></ul><ul><li>separation </li></ul><ul><ul><li>patterns are ideas and practices, not classes and methods </li></ul></ul>
  9. 9. Part 2 Patterns en détail
  10. 10. What have we? <ul><li>the Template </li></ul><ul><li>the Strategy </li></ul><ul><li>the Observer </li></ul><ul><li>the Composite </li></ul><ul><li>the Iterator </li></ul><ul><li>the Command </li></ul><ul><li>the Adapter </li></ul><ul><li>the Proxy </li></ul><ul><li>the Decorator </li></ul><ul><li>the Singleton </li></ul><ul><li>the Factory </li></ul><ul><li>the Builder </li></ul><ul><li>the Interpreter </li></ul><ul><li>the DSL </li></ul><ul><li>the Meta </li></ul><ul><li>the COC </li></ul>
  11. 11. Quite a bunch! Let's start with the classic patterns.
  12. 12. Template (Method) <ul><li>abstract class/module </li></ul><ul><li>several virtual methods </li></ul><ul><li>one method that runs the virtual methods in a certain order </li></ul><ul><li>concrete classes that implement the virtual methods </li></ul><ul><li>a call to the template method on the concrete class </li></ul>
  13. 13. Template (Method) <ul><li>When to use? </li></ul><ul><ul><li>there is a certain sequence of actions done ever again but how the actions executed differs from case to case </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Tests </li></ul></ul><ul><ul><ul><li>setup() </li></ul></ul></ul><ul><ul><ul><li>test_xy() </li></ul></ul></ul><ul><ul><ul><li>teardown() </li></ul></ul></ul><ul><ul><li>Processors </li></ul></ul><ul><ul><ul><li>some object loading </li></ul></ul></ul><ul><ul><ul><li>process_object() </li></ul></ul></ul>
  14. 14. Strategy <ul><li>context </li></ul><ul><ul><li>a property containing the strategy </li></ul></ul><ul><li>abstract strategy </li></ul><ul><ul><li>a virtual method for doing the strategy </li></ul></ul><ul><li>concrete strategies </li></ul><ul><ul><li>the concrete strategy method </li></ul></ul><ul><ul><li>all the methods need to accomplish the task </li></ul></ul>
  15. 15. Strategy <ul><li>When to use? </li></ul><ul><ul><li>There is some action that needs to be done in several different ways which differ far beyond what a Template covers </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Processors </li></ul></ul><ul><ul><ul><li>on_message() </li></ul></ul></ul><ul><ul><li>Miners </li></ul></ul><ul><ul><ul><li>run() </li></ul></ul></ul>
  16. 16. Observer <ul><li>abstract subject </li></ul><ul><ul><li>observers as property </li></ul></ul><ul><ul><li>logic to add/remove/inform observers </li></ul></ul><ul><li>observer </li></ul><ul><ul><li>update method </li></ul></ul><ul><li>concrete subject </li></ul><ul><ul><li>business logic including informing observers </li></ul></ul>
  17. 17. Observers <ul><li>When to use? </li></ul><ul><ul><li>You want to be informed when some state in the subject changes and react to that. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>ActiveRecord::Observer </li></ul></ul>
  18. 18. Composite <ul><li>abstract component </li></ul><ul><ul><li>virtual methods </li></ul></ul><ul><li>concrete component 'leaf' </li></ul><ul><ul><li>concrete methods </li></ul></ul><ul><li>concrete component 'composite' </li></ul><ul><ul><li>property for subcomponents </li></ul></ul><ul><ul><li>concrete methods that delegate to the subcomponents </li></ul></ul>
  19. 19. Composite <ul><li>When to use? </li></ul><ul><ul><li>You want to treat some object the same, no matter how complex the operation is you want to have done, but you want to model the operation in as small steps as necessary. </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>GUIs like FXRuby </li></ul></ul><ul><ul><ul><li>windows, frames, buttons, labels </li></ul></ul></ul>
  20. 20. Iterator <ul><li>internal </li></ul><ul><ul><li>method that yields a block on every of the object's properties/elements </li></ul></ul><ul><li>external </li></ul><ul><ul><li>an object keeps track of which property of another element you are currently reading and if there are more to read </li></ul></ul>
  21. 21. Iterator <ul><li>When to use? </li></ul><ul><ul><li>general </li></ul></ul><ul><ul><ul><li>you need access (in order) to each property of an object to do something with it </li></ul></ul></ul><ul><ul><li>internal </li></ul></ul><ul><ul><ul><li>you want the object to be able to expose its properties on its own </li></ul></ul></ul><ul><ul><li>external </li></ul></ul><ul><ul><ul><li>you want to decouple the exposition from the object, for use independent of the property container </li></ul></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Enumerable module </li></ul></ul>
  22. 22. Command <ul><li>abstract command class </li></ul><ul><ul><li>virtual methods </li></ul></ul><ul><li>concrete command classes </li></ul><ul><ul><li>concrete methods </li></ul></ul>
  23. 23. Command <ul><li>When to use? </li></ul><ul><ul><li>You need something to be done without care of how it is done. You may even delay the operation, aggregate actions or provide ways to undo something. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Background#do </li></ul></ul><ul><ul><li>ActsAsPublished </li></ul></ul><ul><ul><ul><li>bulk updates </li></ul></ul></ul><ul><ul><li>Migrations </li></ul></ul><ul><ul><ul><li>up/down </li></ul></ul></ul>
  24. 24. Adapter <ul><li>client </li></ul><ul><ul><li>knows some target </li></ul></ul><ul><ul><ul><li>target's methods </li></ul></ul></ul><ul><li>target (adapter) </li></ul><ul><ul><li>adaptee </li></ul></ul><ul><ul><ul><li>adaptee's methods </li></ul></ul></ul><ul><ul><li>methods that call adaptee's methods </li></ul></ul><ul><li>or </li></ul><ul><ul><li>redefined adaptee class </li></ul></ul><ul><ul><ul><li>happens... </li></ul></ul></ul>
  25. 25. Adapter <ul><li>When to use? </li></ul><ul><ul><li>When ever you want you client target to access one of some server classes which have completely different interfaces. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>ActiveRecord::Adapter::XY </li></ul></ul><ul><ul><li>abstract search </li></ul></ul>
  26. 26. Proxy <ul><li>abstract service </li></ul><ul><ul><li>virtual method </li></ul></ul><ul><li>concrete service </li></ul><ul><ul><li>concrete method </li></ul></ul><ul><li>service proxy (< abstract service) </li></ul><ul><ul><li>property for the concrete service </li></ul></ul><ul><ul><li>proxy method calling the concrete method </li></ul></ul>
  27. 27. Proxy <ul><li>When to use? </li></ul><ul><ul><li>protection </li></ul></ul><ul><ul><ul><li>making sure some state is checked before the methods get loaded </li></ul></ul></ul><ul><ul><li>remoting </li></ul></ul><ul><ul><ul><li>your real service is over the net or somewhere and you don't want the client to bother manage the connection </li></ul></ul></ul><ul><ul><li>virtualization </li></ul></ul><ul><ul><ul><li>you want the concrete service to be loaded first when some method is called on him, not on initialization of the service </li></ul></ul></ul><ul><li>Example: DRb </li></ul>
  28. 28. Some thought on a Proxy <ul><li>virtual proxy means some object is loaded only later </li></ul><ul><li>observers load all our model code on initialization of the webserver </li></ul><ul><li>why not warp the models in a proxy? </li></ul><ul><li>problem: we would perhaps have to use the proxy everywhere </li></ul><ul><li>perhaps a Command could help out catching the observe command and being run by the Model on load </li></ul>
  29. 29. Decorator <ul><li>abstract component </li></ul><ul><ul><li>virtual methods </li></ul></ul><ul><li>concrete component </li></ul><ul><ul><li>concrete methods </li></ul></ul><ul><li>decorator component </li></ul><ul><ul><li>component property </li></ul></ul><ul><ul><li>new methods </li></ul></ul><ul><ul><li>methods calling the new methods and then the method on the property component </li></ul></ul>
  30. 30. Decorator <ul><li>When to use? </li></ul><ul><ul><li>On occasion you want something to be done to the parameter before the actual method is called or you want some other things to be executed along with the original method. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Acts_as_... </li></ul></ul><ul><ul><li>alias_method_chain </li></ul></ul>
  31. 31. Singleton <ul><li>one class </li></ul><ul><ul><li>one instance </li></ul></ul><ul><ul><ul><li>a private initialize method </li></ul></ul></ul><ul><ul><ul><li>other methods </li></ul></ul></ul><ul><li>alternatively a module or a class </li></ul><ul><ul><li>methods </li></ul></ul>
  32. 32. Singleton <ul><li>When to use? </li></ul><ul><ul><li>You want something to be loaded just once, like a config file, a connection object or the like. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>the communipediaApi configuration </li></ul></ul><ul><ul><ul><li>Class style singleton </li></ul></ul></ul><ul><ul><li>the sitemap </li></ul></ul><ul><ul><ul><li>instance singleton </li></ul></ul></ul>
  33. 33. Factory <ul><li>abstract factory class </li></ul><ul><ul><li>virtual object initialization methods </li></ul></ul><ul><li>concrete factory class </li></ul><ul><ul><li>concrete methods for initializing several objects </li></ul></ul>
  34. 34. Factory <ul><li>When to use? </li></ul><ul><ul><li>In a certain place you need to be handed instances of different objects depending on some criteria. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Campaign </li></ul></ul><ul><ul><ul><li>new() loads a certain campaign type depending on what you tell it </li></ul></ul></ul>
  35. 35. Builder <ul><li>product class </li></ul><ul><ul><li>properties </li></ul></ul><ul><li>builder class </li></ul><ul><ul><li>property product </li></ul></ul><ul><ul><li>methods to set the products properties </li></ul></ul><ul><ul><li>additional methods </li></ul></ul><ul><li>director </li></ul><ul><ul><li>property builder </li></ul></ul>
  36. 36. Builder <ul><li>When to use? </li></ul><ul><ul><li>You need objects only built in a certain state but you don't what to have the object bother enforcing the right state. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>ActiveRecord models </li></ul></ul><ul><ul><ul><li>validations </li></ul></ul></ul><ul><ul><ul><li>data is built to fit the chosen DB </li></ul></ul></ul>
  37. 37. Interpreter <ul><li>client </li></ul><ul><ul><li>properties expressions </li></ul></ul><ul><ul><li>property context </li></ul></ul><ul><li>abstract expression </li></ul><ul><ul><li>virtual interpret method </li></ul></ul><ul><li>terminal expression </li></ul><ul><ul><li>interpret method </li></ul></ul><ul><li>nonterminal expression </li></ul><ul><ul><li>properties sub expressions </li></ul></ul><ul><ul><li>interpret method </li></ul></ul>
  38. 38. Interpreter <ul><li>When to use? </li></ul><ul><ul><li>You want to introduce some specific expression language to a certain task who's code you need to execute. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Ruby </li></ul></ul><ul><ul><ul><li>itself (which is too complex for the pattern actually) </li></ul></ul></ul><ul><ul><li>regex </li></ul></ul>
  39. 39. That's it. Not really. But so much for the classic patterns.
  40. 40. Ruby patterns. Now for some more ruby specific patterns.
  41. 41. Domain Specific Language <ul><li>file filled with (syntactically) correct ruby code </li></ul><ul><li>loader class </li></ul><ul><ul><li>methods used as keywords in the file </li></ul></ul>
  42. 42. Domain Specific Language <ul><li>When to use? </li></ul><ul><ul><li>You want non-programmers to define e.g. behaviors of something. You like the ease of defining config files without needing an interpreter to load it again. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>campaign </li></ul></ul><ul><ul><ul><li>the chain methods could be used as DSL </li></ul></ul></ul>
  43. 43. Meta (Programming) <ul><li>meta class </li></ul><ul><ul><li>methods </li></ul></ul><ul><li>classes defined by the meta class methods </li></ul><ul><ul><li>methods definded by the meta class methods </li></ul></ul>
  44. 44. Meta (Programming) <ul><li>When to use? </li></ul><ul><ul><li>You need objects with certain behaviour or set of methods depending on some conditions but you don't want to define every possible combination in a separate class. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>ActsAsSolr </li></ul></ul><ul><ul><li>pick a file... </li></ul></ul>
  45. 45. Convention over Configuration <ul><li>classes </li></ul><ul><ul><li>intelligent defaults </li></ul></ul><ul><li>configurations </li></ul><ul><ul><li>items that override the defaults </li></ul></ul>
  46. 46. Convention over Configuration <ul><li>When to use? </li></ul><ul><ul><li>You want a behavior that you can set in certain ways and you know one that fits most of the time or serves as fallback. </li></ul></ul><ul><li>Examples </li></ul><ul><ul><li>Rails::* </li></ul></ul><ul><ul><li>search_backend </li></ul></ul>
  47. 47. That's it. (finally)

×