13. Hot swapping algorithms at runtime.
Isolates the code of the algorithms from
the other classes.
Replaces inheritance with composition.
Follows the Open/Closed Principle.
Strategy Pattern
Pros & Cons
Increases overall code complexity by
creating multiple additional classes.
Client must be aware of the differences
between strategies to pick a proper one.
14. Strategy lets you:
Define a family of algorithms.
Encapsulate each one.
Make them interchangeable.
Strategy lets the algorithm:
Vary independently from the clients that use it.
Strategy Pattern
Intent
22. Helps to eliminate code duplication.
Flexibility lets subclasses decide how
to implement steps in an algorithm.
Pros & Cons
You are limited with a skeleton of an
existing algorithm.
Built in inheritance rather than composition.
Template methods tend to be harder to
maintain the more steps they have.
Template Method
23. Template Method lets you:
define the skeleton of an algorithm
Allow subclasses to:
redefine certain steps of the algorithm without changing its structure.
Intent
Template Method
34. Eliminates state machine conditionals.
Organizes the code related to
particular states into separate classes.
Simplifies the code of the context.
Pros & Cons
Can be an overkill if a state machine has
only a few states or rarely changes.
State Pattern
35. State pattern allows an object to:
alter its behavior when its internal state changes.
The object will appear to change its class.
Intent
State Pattern
46. Subject is not coupled to concrete
observer classes.
You can register and remove objects
dynamically.
Follows the Open/Closed Principle.
Strategy Pattern
Pros & Cons
Observers are notified in random order.
If not used carefully, It can add unnecessary
complexity
Observer Pattern
47. Observer lets you:
define a one-to-many dependency between objects, so that when one object changes state,
all its dependents are notified and updated automatically.
Strategy Pattern
Intent
Observer Pattern
57. Decouples classes that invoke operations from classes
that perform them.
Allows reversal (undo) of operations.
Allows deferred execution of operations.
Allows assembling simple commands into larger ones.
Follows the Open/Closed Principle.
Strategy Pattern
Pros & Cons
Increases overall code complexity by creating
multiple additional classes.
Observer PatternCommand Pattern
58. Command lets you:
turn a request into stand-alone object, which can be used to parametrize objects with
different requests and support undoable operations.
Intent
Command Pattern
66. Decouples the collection's code.
Provides multiple ways to traverse
the same data structure.
Allows parallel traversing of the same
collection.
Pros & Cons
Can be overkill for programs that work
with simple collections.
Iterator Pattern
67. Iterator pattern lets you:
access the elements of an aggregate object sequentially
Without:
exposing its underlying representation.
Intent
Iterator Pattern