Problem

The new operator hard-wires the type of object to be created.

Solution

Specify the kinds of objects you'd like to be able to create, build a prototypical instance of that, and create new objects by cloning those prototypes.

Related Patterns

Discussion

Clients clone() the abstract base class, supplying an optional data type designating the desired subtype. The new operator is abandoned in favor of prototype cloning.

Examples

JavaScript uses prototypes extensively for object creation. The object constructor function in JS is the prototype for that object (which can be called with the new operator).

Code

JavaScript has string built-in support for prototypes:

var myprototype = function myprototypeobj(){
  this.foo = function(){alert("foo");};
  this.bar = function(){alert("bar");};
}
var myobj.prototype = myprototype;
myobj.foo(); myobj.bar(); // myobj is created with the methods of myprototype