this repo contains implementations of various design patterns in java, mostly inspired by christopher okhravi's videos, with a few extras pulled from other sources like mosh hamedani’s work.
below are the definitions of key design patterns from "design patterns: elements of reusable object-oriented software" by erich gamma, richard helm, ralph johnson, and john vlissides (the "gang of four"):
- purpose: provide a way to create related objects without specifying their exact classes.
- roles:
AbstractFactory
,ConcreteFactory
,AbstractProduct
,ConcreteProduct
.
- purpose: let incompatible interfaces work together by converting one interface into another that clients expect.
- roles:
Target
,Adapter
,Adaptee
.
- purpose: separate an abstraction from its implementation so both can change independently.
- roles:
Abstraction
,RefinedAbstraction
,Implementation
,ConcreteImplementation
.
- purpose: turn a request into an object so you can queue it, log it, and support undo operations.
- roles:
Command
,ConcreteCommand
,Receiver
,Invoker
,Client
.
- purpose: arrange objects into tree structures to represent whole-part relationships, so you can treat them all uniformly.
- roles:
Component
,Leaf
,Composite
.
- purpose: add responsibilities to an object dynamically, as an alternative to subclassing.
- roles:
Component
,ConcreteComponent
,Decorator
,ConcreteDecorator
.
- purpose: simplify interactions with a system by providing a unified interface to its more complex subsystems.
- roles:
Facade
,Subsystem
classes.
- purpose: let subclasses decide which object to create, instead of having the parent class specify the exact class.
- roles:
Creator
,ConcreteCreator
,Product
,ConcreteProduct
.
- purpose: provide a way to sequentially access elements of a collection without exposing the underlying structure.
- roles:
Iterator
,ConcreteIterator
,Aggregate
,ConcreteAggregate
.
- purpose: capture an object’s internal state without exposing its details, so it can be restored later.
- roles:
Memento
,Originator
,Caretaker
.
- purpose: use a default object when the real object is missing, to avoid null references.
- roles:
AbstractObject
,RealObject
,NullObject
.
- purpose: automatically notify and update dependents when an object changes state.
- roles:
Subject
,Observer
,ConcreteSubject
,ConcreteObserver
.
- purpose: provide a stand-in for an object, controlling access to it.
- roles:
Proxy
,Subject
,RealSubject
.
- purpose: ensure a class only has one instance, and provide a way to access it globally.
- roles:
Singleton
.
- purpose: change an object’s behavior based on its internal state.
- roles:
Context
,State
,ConcreteState
.
- purpose: define a set of interchangeable algorithms, allowing clients to switch between them.
- roles:
Strategy
,Context
,ConcreteStrategy
.
- purpose: define the overall structure of an algorithm, while allowing subclasses to fill in specific steps.
- roles:
AbstractClass
,ConcreteClass
.