Software Design Patterns:
The Command Pattern
What is the primary purpose of this pattern? Turns a request into a stand-
alone object that contains all information about the request. This
transformation lets you pass requests as a method arguments, delay or
queue a request’s execution, and support undoable operations.
You basically turn methods into objects containing all information needed to
run. These objects, or commands, can be passed into an invoker that runs
them without knowing anything about how they work, making it easy to add
new commands and decoupling code.
What are secondary benefits, if any?
Single Responsibility Principle. You can decouple classes that invoke
operations from classes that perform these operations.
Open/Closed Principle. You can introduce new commands into the app
without breaking existing client code.
You can implement undo/redo.
You can implement deferred execution of operations.
You can assemble a set of simple commands into a complex one.
What kind of pattern is it? Creational, structural, behavioral? Behavioral.
What are key words associated with this pattern? Command, invoker,
receiver, execute
Template Method
What is the primary purpose of this pattern? Defines the skeleton of an
algorithm in the superclass but lets subclasses override specific steps of the
algorithm without changing its structure.
You make a template for a class that can be easily repurposed by subclasses
to avoid repeated code.
What are secondary benefits, if any?
You can let clients override only certain parts of a large algorithm,
making them less affected by changes that happen to other parts of
the algorithm.
You can pull the duplicate code into a superclass.
What kind of pattern is it? Creational, structural, behavioral? Behavioral
What are key words associated with this pattern? Template, abstract class,
override,
Facade
What is the primary purpose of this pattern? Provides a simple interface to a
complex subsystem which contains lots of moving parts. A facade might
provide limited functionality in comparison to working with the subsystem
directly. However, it includes only those features that clients really care
about.
When you have a complex set of instructions, you can have them all grouped
together. Example: a home theater with lots of complex machines involved
like projector, wifi, streaming service, sound system and popcorn machine is
a lot to manage each individually, but if you group it into one system with
commands like, prepare_movie() and finished_movie() that start and end the
necessary machines, we can reduce complexity on the user end.
What are secondary benefits, if any?
You can isolate your code from the complexity of a subsystem.
What kind of pattern is it? Creational, structural, behavioral? Structural
What are key words associated with this pattern? Façade, interface,
subsystem,
Adapter
What is the primary purpose of this pattern? Allows objects with incompatible
interfaces to collaborate. An adapter is a special object that converts the
interface of one object so that another object can understand it. (Use the
Adapter class when you want to use some existing class, but its interface
isn’t compatible with the rest of your code.
When you have two objects with an incompatibility, instead of changing one
of the classes to fit into the other, you can create a middleman that adapts
the object to one of the classes. Example, you have an app that downloads
stock data in XML, and an analytics library that only takes JSON. Instead of
modifying the library to take XML, you can create an adapter that takes in
XML and converts it into compatibly JSON.
What are secondary benefits, if any?
Single Responsibility Principle. You can separate the interface or data
conversion code from the primary business logic of the program.
Open/Closed Principle. You can introduce new types of adapters into
the program without breaking the existing client code, as long as they
work with the adapters through the client interface.
What kind of pattern is it? Creational, structural, behavioral? Structural
What are key words associated with this pattern? Existing class, adapter,
Iterator
What is the primary purpose of this pattern? Lets you traverse elements of a
collection without exposing its underlying representation (list, stack, tree,
etc.). The main idea of the Iterator pattern is to extract the traversal
behavior of a collection into a separate object called an iterator.
What are secondary benefits, if any?
Single Responsibility Principle. You can clean up the client code and the
collections by extracting bulky traversal algorithms into separate
classes.
Open/Closed Principle. You can implement new types of collections and
iterators and pass them to existing code without breaking anything.
You can iterate over the same collection in parallel because each
iterator object contains its own iteration state.
For the same reason, you can delay an iteration and continue it when
needed.
What kind of pattern is it? Creational, structural, behavioral? Behavioral
What are key words associated with this pattern? Iterator, iterable collection,
concrete iterator
Composite
What is the primary purpose of this pattern?
What are secondary benefits, if any?
What kind of pattern is it? Creational, structural, behavioral?
What are key words associated with this pattern?
Visitor
What is the primary purpose of this pattern?
What are secondary benefits, if any?
What kind of pattern is it? Creational, structural, behavioral?
What are key words associated with this pattern?
Null Object
What is the primary purpose of this pattern?
What are secondary benefits, if any?
What kind of pattern is it? Creational, structural, behavioral?
What are key words associated with this pattern?
MVC
What is the primary purpose of this pattern?
What are secondary benefits, if any?
What kind of pattern is it? Creational, structural, behavioral?
What are key words associated with this pattern?