Design patterns are a typical solutions to specific problems.
Creational Patterns
- Factory
- Abstract Factory
- Builder
- Prototype
- Singleton
Structural Patterns
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
Behavioral Patterns
- Chain of Responsibility
- Command
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
- Review Readme, make it easier to read
- Review Prototype example, use interface instead of a concrete class
Creational patterns are focused on object creation and reuse.
Factory separates object creation from usage.
[+] Class responsibilities are distributed (Single Responsibility Principle).
[+] Any new object type can be introduced easily (Open/Closed Principle)
[-] Code becomes more complex with new classes.
Abstract factory is a factory of factories. It is responsible to pick a certain factory which creates objects with related aspects. It is one step further than the factory pattern. Abstract factory is more complex but more flexible.
[+] Class responsibilities are distributed (Single Responsibility Principle).
[+] Any new object type can be introduced easily (Open/Closed Principle)
[-] Code becomes more complex with new classes.
Builder pattern enables us to build complex objects (with a lot of fields) in an easy and clean way.
[+] Class responsibilities are distributed (Single Responsibility Principle).
[-] Additional builder classes are introduced with plan implementation, yet it's pretty lightweight with the help of libraries.
Prototype pattern helps us to build complex objects.
[+] It provides pre-built objects instead of creating it from scratch.
[-] It gets complicated when objects are too complex and/or have circular dependencies.
Singleton pattern enabled us to create a single instance of a class, then re-use when needed. Singleton class has two responsibilities; generating a single unique instance of a class, providing a global access point to that class.
[+] Guarantees there is a single instance of a class.
[-] Requires special attention in a multi-threaded environment.
[-] Since it has static methods, it is hard to unit test.
[-] Violates single responsibility principle since it has two responsibilities.
Structural patterns are focused on building flexible structures with objects.
Adapter adapts incompatible classes to work together with our code. It's helpful when we can't change the incompatible classes such as in 3rd party libraries.
[+] Business logic is left untouched since the conversion is made outside [Single Responsibility]
[+] New adapters can be introduced without changing the existing code [Open/Closed Principle]
[-] Code becomes more complex with new classes.
Bridge pattern splits our monolithic class to the functional dimensions, for e.g. shape, color, weight.
[+] Each class can be updated without changing the original class.