Design patterns are the best-formalized practices a programmer can use to solve common problems when designing an application or system.
- Creational - designed for object creation to increase flexibility and reuse of existing code. Polymorphism is used.
- Structural - establishes a relationship between classes making larger structures. Inheritance is used.
- Whenever you think of designing architecture for your system, structural design patterns help you to plan for the future, and arrange classes in neat hierarchies.
- Behavioral - effective communications and interactions between objects/classes. Methods are used.
- Speed up the development process;
- Reduce the number of lines of code;
- Make sure your code is well-designed;
- Anticipate future problems arising from small issues.
- Creational
- *Singleton, restricts a class from having more than one instance and ensures a global access point to this instance.
- *Factory, the method that helps to create other objects.
- *Factory abstract, like factory but object that provides a way to encapsulate a group of individual factories.
- *Builder, separating class from building complex object.
- Borg, allows all instances of one class to share common data.
- Prototype, registers object and clones objects (if instantiation is costly).
- Pool, like flyweight but for mutable objects.
- Structural
- *Facade, hides the complexity of implementations. New API on top of other APIs.
- *Bridge, separates the implementation from abstraction. Ex: DrawingAPIOne, DrawingAPITwo
- *Adapter, makes incompatible objects adaptable to each other. Ex:
speak=english.hello(), speak=korean.anyong() - *Proxy, like decorator, adds functionality to a class without changing its interface.
- Decorator, attach new behaviors to the objects without modifying their structure.
- Composite, describes a group of objects the same way as a single instance, using a Tree data structure.
- Flyweight, cache, reuses existing instances of objects with similar/identical state.
- Behavioural
- *Strategy, enables our application to select algorithms (method) at runtime.
- *Observer, lets subscribed entities know about changes in an observable. Ex: Elon & Twitter.
- *Iterator, helps to iterate through the elements of the collection.
- *Mediator, objects in a system communicate through a Mediator instead of directly with each other.
- Publish & Subscribe, a source syndicates events/data to 0+ registered listeners.
- Visitor, adds new features to an existing hierarchy without changing its structure. Ex: electricity, hvac.
- Chain of responsibility, allows a request to pass down a chain of receivers until it is handled.
- Template, defines the skeleton of a base algorithm, deferring the definition of exact steps to subclasses.
- Momento, generates an opaque token that can be used to go back to a previous state.
- State, logic is organized into a discrete number of potential states and the next state that can be transitioned to
- By NeetCode https://youtu.be/tAuRQs_d9F8
- By Fireship https://youtu.be/tv-_1er1mWI
- https://www.linkedin.com/learning/python-design-patterns/welcome?u=2113185
- https://github.com/faif/python-patterns
- https://jellyfish.tech/implementation-of-common-design-patterns-in-python/
- https://youtube.com/playlist?list=PLO6785UZapFX5bO4ZZSO7Unn2uOTNFqfQ
- https://www.toptal.com/python/python-design-patterns
- https://python-patterns.guide