- Separate component when object construction gets too complicated
- Can create mutually cooperating sub-builders
- Often has a fluent interface
- Factory method more expressive than initializer
- Factory can be an outside class or inner class
- Creation of object from an existing object
- Requires explicit deep copy
- When you need to ensure just a single instance exisits
- Easy to make with a decorator or metaclass
- Consider using dependency injection
- Converts the interface you get to the interface you need
- Decouple abstraction from implementation
- Allows clients to treat individual objects and compositions of objects uniformly
- Attach additional responsibilities to object
- Python has functional decorator
- Provide a single unified interface over a set of interface
- Friendly and easy-to-use, but can provide access to low-level features
- Efficiently support very large numbers of similar objects
- Provide a surrogate object that forwards calls to the real object while performing additional functions
- E.g., access control, communication, logging, etc.
- Allow components to process information/events in a chain
- Each element in the chain refers to next element; or
- Make a list and go through it
- Encapsulate a request into a separate object
- Good for audit, replay, undo/redo
- Part of CQS/CQRS
- Transform textual input into object-oriented structures
- Used by interpreters, compilers, static analysis tools, etc.
- Compiler Theory is a separate branch of Computer Science
- Provide an interface for accessing elements of an aggregate object
__iter__/__next
are stateful, butyield
is much more convenient
- Provides mediation services between two objects
- E.g., message passing, chat room
- Yield tokens representing system states
- Tokens do not allow direct manipulation, but can be used in appropriate APIs
- Allows notifications of changes/happenings in a component
- We model system by having one of a possible states and transitions between these states
- Such a system is called a state machine
- Special frameworks exists to orchestrate state machines
- Both define a skeleton algorithm with details filled in by implementor
- Strategy uses ordinary composition, template method uses inheritance
- Allows non-intrusive addition of functionality to hierarchies