Where software design patterns meet their Platonic origins
from Greek monos (μόνος) -- one, alone
Ensures a class has only one instance and provides a global point of access to it. Like Plato's Form of the Good -- there is only one, and everything references it.
class Singleton {
static #instance;
static getInstance() {
if (!Singleton.#instance) {
Singleton.#instance = new Singleton();
}
return Singleton.#instance;
}
}
from Greek theoros (θεωρός) -- spectator, one who observes
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified. Like disciples gathered around a philosopher -- when wisdom speaks, all listen.
class Subject {
#observers = [];
subscribe(obs) {
this.#observers.push(obs);
}
notify(data) {
this.#observers.forEach(
o => o.update(data)
);
}
}
from Greek demiourgos (δημιουργός) -- craftsman, creator
Defines an interface for creating objects, but lets subclasses decide which class to instantiate. Like the Demiurge of Plato's Timaeus -- the divine craftsman who creates forms from archetypes.
class ShapeFactory {
create(type) {
switch(type) {
case 'circle':
return new Circle();
case 'square':
return new Square();
}
}
}
The canonical catalog of software archetypes
Converts the interface of a class into another interface clients expect.
StructuralDefines a family of algorithms, encapsulating each one to be interchangeable.
BehavioralAttaches additional responsibilities to an object dynamically.
StructuralEncapsulates a request as an object, allowing parameterization of clients.
BehavioralComposes objects into tree structures to represent part-whole hierarchies.
StructuralCreates new objects by copying an existing object -- the prototype.
Creational DeprecatedHow patterns relate to one another in the archetypal hierarchy