A Brief Note on Classes

Keep in mind that there will be patterns in this table that reference the concept of “classes.” JavaScript is a classless language; however, classes can be simulated using functions.

The most common approach to achieving this is by defining a JavaScript function in which you then create an object using the new keyword. Use this to define new properties and methods for the object as follows:

// A Car "class"
function Car( model ) {

  this.model = model;
  this.color = "silver";
  this.year  = "2012";

  this.getInfo = function () {
    return this.model + " " + this.year;
  };

}

We can then instantiate the object using the car constructor we defined above like this:

var myCar = new Car("ford");

myCar.year = "2010";

console.log( myCar.getInfo() );

For more ways to define classes using JavaScript, see Stoyan Stefanov’s useful post on them.

Let us now proceed to review the table.

Table 8-1. 

CreationalBased on the concept of creating an object      
Class       
Factory methodMakes an instance of several derived classes based on interfaced data or events      
Object       
Abstract factoryCreates an instance of several families of classes without detailing concrete classes      
BuilderSeparates object construction from its representation; always creates the same type of object      
PrototypeA fully initialized instance used for copying or cloning      
SingletonA class with only a single instance with global access points      
        
StructuralBased on the idea of building blocks of objects      
Class       
AdapterMatches interfaces of different classes so classes can work together despite incompatible interfaces      
Object       
AdapterMatches interfaces of different classes so classes can work together despite incompatible interfaces      
BridgeSeparates an object’s interface from its implementation so the two can vary independently      
CompositeA structure of simple and composite objects that makes the total object more than just the sum of its parts      
DecoratorDynamically adds alternate processing to objects      
FacadeA single class that hides the complexity of an entire subsystem      
FlyweightA fine-grained instance used for efficient sharing of information that is contained elsewhere      
ProxyA placeholder object representing the true object      
        
BehavioralBased on the way objects play and work together      
Class       
InterpreterA way to include language elements in an application to match the grammar of the intended language      
Template methodCreates the shell of an algorithm in a method, then defers the exact steps to a subclass      
Object       
Chain of responsibilityA way of passing a request between a chain of objects to find the object that can handle the request      
CommandA way to separate the execution of a command from its invoker      
IteratorSequentially accesses the elements of a collection without knowing the inner workings of the collection      
MediatorDefines simplified communication between classes to prevent a group of classes from referring explicitly to each other      
MementoCaptures an object’s internal state to be able to restore it later      
ObserverA way of notifying change to a number of classes to ensure consistency between the classes      
StateAlters an object’s behavior when its state changes      
StrategyEncapsulates an algorithm inside a class separating the selection from the implementation      
VisitorAdds a new operation to a class without changing the class      

Get Learning JavaScript Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.