Strategy is a behavioral design pattern that turns a set of behaviors into objects and makes them interchangeable inside the original context object.
Different implementations of the Duck abstract class use different fly and quack behavior. Specific behavior is injected during Duck instance initialization.
The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
Different sensor displays are subscribed to the weather station. When the weather station changes its measurements, all displays are notified.
The Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.
Condiment class is a decorator that wraps condiment class instance of beverage instance. So we could build a chain of condiments wrappers above beverages. It keeps beverage implementation close for modification, but open for extension.
Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.
Singleton is a creational design pattern that lets you ensure that a class has only one instance while providing a global access point to this instance.
The Singleton class creates a new instance or returns the existing one on instance creation.
The command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as method arguments, delay or queue a request’s execution, and support undoable operations.
The invoker gives commands to receivers via a common interface and keeps the list of executed commands. When needed, we could undo commands from the last to the first one.
The adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.
There are two types of adapter: object and class adapter, one is implemented via composition and another one is implemented via multiple inheritance. The turkey adapter for duck allows the execution of duck methods on the Turkey class instance.
The facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.
The home theater facade class allows 'watch movie' and 'end movie' methods that encapsulate the complex logic of orchestrating all necessary devices.
The Template Method is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.
The abstract caffeine beverage class has 2 implementations: Tea and Coffee. It contains a few abstract methods which are implemented their proper way in the child classes.
Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
There is an abstract menu class and two implementations where menu items are implemented as different type structures. The waitress could iterate over menu items on both menus in the same manner.
Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.
There is an abstract human class and two implementations: Human and Child. When we print the Human instance's family tree, it prints it recursively.
State is a behavioral design pattern that lets an object alter its behavior when its internal state changes. It appears as if the object changed its class.
The gumball machine changes its state according to performed by user actions.
Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.
When the user asks to display the remote image, the proxy displays the fake image until the real remote image is downloaded.
You need to have at least Python3.8 installed.
- Clone the repo
git clone https://github.com/balancy/design_patterns_head_first.git
- Run specific pattern example
python3 -m patterns.chapter_01_strategy.main
You also need to have Poetry installed.
Run linting
make lint
Run type checking
make typecheck
Run tests
make test
Run all together
make check