1.54k likes | 1.55k Views
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.
E N D
Behavioral Design Patterns João Pereira yuletechnologies.co.uk . Original publication: March 2012 2019 Warning: Links, libraries and and recommended books may be outdated.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Command • Participants in the Command Design Pattern: • Command The interface Command defines the common interface for all concrete commands • ConcreteCommandThe 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.