| Name | GO4 Definition | WHY? | Notes |
|---|---|---|---|
| Singleton | Ensure a class only has one instance, and provide global access to it. | multiple instances of an object does not share the property | module.exports = new ClassName (); |
| Prototype | Specify the kind of object to create using prototypical instance, and create new objects by copying its prototype. | Similar blueprints for houses in the same neighborhood | cloned = {...this} |
| Factory (function) | Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. | Helps clean up the main file when you have similar classes | const factory = (...) => {<condition for returning different classes>} |
| Builder | Separate the construction of a complex object from its representation so that the same construction process can create different representations. | When you need more customization than factory method | class BuilderClass feeds into new Class(Builder Class{ ... }). Also, method chaining |
Structural patterns maintain the same interface
| Name | GO4 Definition | WHY? | Notes |
|---|---|---|---|
| Adapter | Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. | Makes things compatible, while maintaining the same interface. | class AdapterClass { ... <same props and methods as the target class> } |
| Proxy | Provide a surrogate or placeholder for another object (much more bigger/expensive object) to control access to it. | Useful for (1) managing expensive objects, (2) managing a remote resource, (3) provide data validation of input, (4) provide security, (5) cache data, (6) even log actions. | Similar to an Adapter Class, need to keep the same interface |
| Composite | Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. | Allows same interface for both a single instance of a class and a cluster (array) of multiple instances. (i.e. file system: same interface for delete file/folder) | methods on the cluster Class are recursive const cluster = new Cluster ([Obj1, Obj2, Obj3]) |
| Decorator | Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. | Decorator (class) takes an object and returns a new object with similar interface | const newObj = new DecoratorClass (inputObj) |
| Name | GO4 Definition | Why? | Notes |
|---|---|---|---|
| Chain of Responsibility | Avoid coupling the sender of a request to its receiver by giving more than one object a change to handle the request. Chain the receiving objects and pass the request along the chain. | Think of a sandwich assembly line. You can enter/retrieve the sandwich from any point in the process. | subClasses are chained to form the mainClass. Constructor of the mainClass bundles the objects together, but the methods are deferred to the subClass. |
| Command | Encapsulate a request as an object, thereby letting you parameterize with different requests, queue or log requests, and support undoable operations. | Conductor Class provides a way to queue up commands, undo, redo, etc. | conductor.run(new CommandClass()); |
| Iterator | Provide a way to access the element of an aggregate object sequentially without exposing its underlying representation. | In another words, an iterator class is just a bundle of methods for navigating an aggregate data. (i.e. next, prev, start, last, ... etc) | JUST BECAUSE |
| Observer | Define a one-to-many dependency between objects so that when one object changes state, all its depents are notified and updated automatically | Use Cases: Subcription / Following | <Observable>.subscribe(<Observer Class> Observable's constructor stores observer objects (as array, etc) and a method that triggers observer's reaction |
| Strategy | Define a family of algorithms, encapsulate each one,and make them interchageable. Strategy lets the algorithm vary independently from clients that use it. | Use Case: DECOUPLING payment process from checkout process. Facilitates flexible payment methods (card/gift card/pay pal/etc) | ActionClass's constructor is built on a strategy class, which is a bundle of static methods. Action object's method invokes the strategy's static method. |