Behavioral Design Patterns
“Behavioral design patterns is basically the ways in which objects interact with each other particularly with the passing of responsibilities back and forth.”
Closing out the series introducing design patterns the guys discuss behavioral design patterns. These deal with communication between entities. They provide easy and flexible communication between objects.
10:32 Chain of Responsibility
“One way to think about this is like a factory assembly line.”
The chain of responsibility is a way of passing a request between a chain of objects. This pattern creates a chain of receiver objects for a request allowing you to launch and leave requests with a single pipeline with many handlers. The handlers can arbitrarily be reordered.
“It’s basically just a bag of stuff you’re passing on.”
The command pattern encapsulates a command request as an object parameterizing clients with different requests. It is basically an object oriented callback.
“You’ll see this in the guts of compilers…I’ve not had to use this very often.”
Interpreters are a way to include language elements in a program to evaluate language grammar or expression. These include SQL parsing or symbol processing engines.
“This basically the thing you use in a for loop.”
Iterator patterns allow you to sequentially access the elements of a collection without exposing the underlying representation. Standard libraries are iterators.
The mediator pattern defines a simple communication between classes. It reduces the complexity between multiple objects in a many-to-many relationship and allows for the individual objects to be interchangeable.
“If you’re sending a command to an object with a memento pattern you would capture the state first.”
Use a memento pattern to capture and restore an object’s internal state. These promote undo or rollback to full object status. This is used heavily in text editors or programs where you need caching.
34:50 Null Object
“Null is not a thing, it’s not knowing a thing.”
The null object acts as a default value of an object. Abstract classes specify operations, concrete classes, and a null object class. The are used when the object requires a collaborator or abstract null handling from the client.
“X observes Y…Y doesn’t have to know what is observing it.”
The observer pattern is a way of notifying changes to a number of objects in a one-to-many relationship. If an object is modified the observer notifies its dependents of the modification.
“I used this pattern when I wrote the contact forms for our websites.”
The state pattern alters an object’s behavior when its state is changed. In effect it creates an object-oriented state machine.
An algorithm encapsulated in a class is a strategy pattern allowing behavior or algorithms to be changed at runtime. Objects can have various strategies, i.e. a context object’s behavior will vary depending on the strategy object.
49:08 Template Method
To defer the exact steps of an algorithm to a subclass use a template method. It creates a skeleton of an algorithm in an operation. The subclasses then may redefine some steps without changing the structure. Base classes declare an algorithm place holder then derived classes implement them.
“I’ve used this recently with parsing Odata expressions.”
The visitor pattern defines a new operation to a class without changing the class. This is a classic technique for recovering lost type information.
- 2 Module Ports
- 2 USB Ports
- 1 microUSB
- 602.11bgn WiFi
- 580MHz MediaTek MT720n
- 48MHz Atmel SAMD21 coprocessor
- 64 MB DDR2
- 32 MB Flash
- 10 Pin:
- Color Sensor
Tricks of the Trade
These patterns are not really all that hard, it’s the language around the patterns that trips people up. The way things get expressed in scientific disciplines tends to be using specialized language that may not be easy for the uninitiated. The thing to remember is that you’ll arrive at these patterns with or without the technical jargon. Most of the jargon used is not for a difficult concept but rather a shorthand for expressing an idea to better share with another person.