OOjs/Registries and factories

From MediaWiki.org
Jump to navigation Jump to search

OO.Registry is a base class that provides a simple map interface for associating arbitrary data with a symbolic name. Used in place of a plain object to provide additional registration or lookup functionality. Commonly, subclasses of OO.Registry create and maintain specialized indexes during registration and provide alternative lookup methods which use these indexes.

OO.Factory extends of classes with instantiation abstraction. Used for referring to classes by a symbolic name, which allows the class to be overridden without modification of calling code. Commonly, subclasses of OO.Factory, like OO.Registry, provide additional registration and lookup functionality; but in addition also often provide methods for accessing static information about registered classes.

Basic usage[edit]

Any class with a static name property can be used with a factory.

// Instantiation
var factory = new OO.Factory();

// Classes
function Cherry() {}
OO.initClass( Cherry );
Cherry.static.name = 'red';
factory.register( Cherry );

function Banana() {}
OO.initClass( Banana );
Banana.static.name = 'yellow';
factory.register( Banana );

// Access
function createFruit( color ) {
    return factory.create( color );
}

See also[edit]