1 / 154

Behavioral Design Patterns

Learn about the patterns of communication between objects and how behavioral design patterns use inheritance to distribute behavior between classes and composition to distribute behavior between objects.

jcramer
Télécharger la présentation

Behavioral Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Behavioral Design Patterns João Pereira yuletechnologies.co.uk . Original publication: March 2012 2019 Warning: Links, libraries and and recommended books may be outdated.

  2. Behavioral Design Patterns Concerned with algorithms and assignment of responsibilities between objects Describe the patterns of communication between objects Behavioral class patterns use inheritance to distribute behavior between classes Behavioral object patterns use composition to distribute behavior between objects Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  3. Behavioral Design Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  4. Behavioral Design Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  5. Chain of responsibility aServlet HTTP GET Request Example in Servlet Filters Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  6. Chain of responsibility aServlet Filter HTTP GET Request Example in Servlet Filters Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  7. Chain of responsibility aServlet Filter Filter HTTP GET Request Example in Servlet Filters Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  8. Chain of responsibility aServlet Filter Filter Filter HTTP GET Request Example in Servlet Filters Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  9. Chain of responsibility Servlet Filter API is defined in three interfaces Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  10. Chain of responsibility The usage of Filter is something like: Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  11. Chain of responsibility • Chain of Responsibility Design Pattern: • Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  12. Chain of responsibility sender receiver • Chain of Responsibility Design Pattern: • Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  13. Chain of responsibility • Participants: • Handler Filter defines the interface for handling requests • ConcreteHandlers AuthorizationFilter, FormatFilter and LoggingFilter implement the handler so they can handle the request • Client  The HTTPServer receiving HTTP Requests Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  14. Behavioral Design Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  15. Command Consider the next Scenario Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  16. Command You’re in charge for the design of an application to manage Database Server Instances You have MySql, PostgreSQL and Oracle Database Servers Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  17. Command Your client can have multiple nodes where DB server instances are running He also wants to group the nodes by the rooms he have in his datacenter Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  18. Command • You have to provide an interface for your client to: • Query the status of each instance (Running, Stopped) • Start each instance individually • Stop each instance individually • Start a group of instances • Stop a group of instances Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  19. Command Start/stop/getStatus Start/stop/getStatus Start/stop/getStatus Start/stop/getStatus Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  20. Command A first draft of the model can be something like: Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  21. Command • Your client wants to controls instances over the Internet, so in your first iteration of design, you provide him with an simple HTTP Web Service that understands commands like: • /?action=start&groupName=“RoomA”&ipAddress=“10.10.22.16” • /?action=stop&groupName=“RoomA”&ipAddress=“10.10.22.16” • /?action=startGroup&groupName=“RoomA” • /?action=stopGroup&groupName=“RoomA” Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  22. Command In another iteration, your model could look like: Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  23. Command A typical collaboration could look like: Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  24. Command • When you show your first iteration of the application, your client asks you how to schedule the start/stop of individual instance or a group of instances • When you look at you design, to change it to accommodate the new requirement, you soon realize that you have a coupling issue… Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  25. Command Invoker of a request is coupled to the object that handles it. It’s also bound to the time the request is handled by the receiver. Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  26. Command • After you looking at your Design Patterns Catalog, you’ve found the Command Pattern (AKA: Action, Transaction) that says: • Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  27. Command The diagram you found for the Command pattern is the following: Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  28. Command Encapsulate a request as an object…. You will test if the Command pattern suits your needs. You ended up with the following model for your command objects: Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  29. Command When you first integrate the Command pattern into your design… Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  30. Command Now, a typical collaboration could look like: Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  31. Command Invoker of a request is decoupled from the object that handles it, but still bound to the time the request is handled by the receiver. Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  32. Command You realized that you should send your commands to some kind of object that schedules the execution of commands. To reuse the scheduler object, you abstract further the commands you designed before Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  33. Command Integrating into the final model… Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  34. Command A collaboration, now, looks like: Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  35. Command You can even apply the AbstractFactory pattern Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  36. Command Also, applying Composite Design Pattern Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  37. Command • Participants in the Command Design Pattern: • Command The interface Command defines the common interface for all concrete commands • ConcreteCommandThe StartInstanceCommand and StopInstanceCommand are examples of ConcreteCommand • Client -> The CommandFactory create ConcreteCommandsand set its receiver. • Invoker TheCommandScheduleris what really is invoking the request carried by the ConcreteCommand • Receiver  The MySqlServerInstance, PostgreSQL and OracleInstance are the objects receiving and processing the request Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  38. Behavioral Design Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  39. Interpreter Consider the next Scenario Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  40. Interpreter After a few iterations working on the application to manage Database Server instances…. Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  41. Interpreter • You now have URLs in your simple HTTP Web Service looking like: • /DBServerInstanceService/roomA/22.22.22.11/start • /DBServerInstanceService/roomA/22.22.22.11/stop • /DBServerInstanceService/roomA/start • /DBServerInstanceService/roomA/stop Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  42. Interpreter The operation to apply to Group/Instance. Mus be allways specified The IP Address of the instance. It’s an optional parameter The name of the service must always exists The name of the group. Must be always provided • The syntax of your HTTP Interface is the following: • /{targetService}1/{groupName}1/{ipAddress}0..1/{operation}1 Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  43. Interpreter This syntax will grow and change frequently. You’re in charge to design an parser module to parse the URLs in your Simple HTTP Service and provide information to the existing interfaces You don’t know where the syntax will evolve to, so you have to maintain flexibility in the parsing You were also told that the parser module could be used also to parse the results in some of implementation of DBServerInstace, so you have to think in reusability Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  44. Interpreter • After you looking at your Design Patterns Catalog, you’ve found the Interpreter Pattern that says: • Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  45. Interpreter The diagram depicting the Interpreter Pattern Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  46. Interpreter • After reading about the Interpreter Design Pattern, you soon realize that you have tools available to help you in your design • Regular Expressions • Parser Generators Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  47. Interpreter • You have some good modules that you can reuse • Regular Expressions • java.util.Regex (http://download.oracle.com/javase/6/docs/api/java/util/regex/package-summary.html) • Parser Generators • ANTLR (http://www.antlr.org/) • JavaCC (http://javacc.java.net/) • Parboiled (https://github.com/sirthias/parboiled/wiki) Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  48. Behavioral Design Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  49. Iterator Consider the next Scenario Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

  50. Iterator • In the design of the application to manage Database Server instances, you are required to give the clients of CommandScheduler instances, an interface to iterate over all commands scheduled. • You remember that java already have something that can help you. • http://download.oracle.com/javase/6/docs/api/java/lang/Iterable.html Behavioral Design Patterns by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

More Related