Abstract Constructor

In an OOP language, an Abstract Constructor is a mechanism to polymorphically produce an object of an Abstract Base Class.

Most OOP languages do not support the Abstract Constructor pattern as a language feature, but rather require using Design Patterns such as Abstract Factory, Plugin Architecture, or some combination thereof.

It is possible that you have overlapping constructor options for a given value (e.g. you need a clock, but you can choose between the local hardware clock vs. an network-time-protocol clock). At this point, Abstract Constructor benefits from Policy Injection to select heuristically between available options (e.g. allowing user to decide between favoring performance, precision, or accuracy).

Abstract Constructor supports data-driven object construction, since the selection of object is based entirely on dynamic input data and available constructors (potentially via plugins) rather than being based upon static code.

Abstract Constructor does not imply Garbage Collection, but Abstract Constructor + Garbage Collection is a powerful combination in that it allows one to return singletons and other shared objects from the Abstract Constructor. Without Garbage Collection, one would need to be concerned about figuring out when to delete a potentially shared object, which would defeat its use for singletons and shared objects.


Other approaches to handling shared objects exist, such as constructing a pool of named objects then using Setter Injection to obtain references to appropriate objects from the pool - i.e. a two-pass approach that allows sharing of objects other than just singletons. In this case, Abstract Constructor is still useful for data-driven construction for the pool of objects. This approach does require a little discipline - in particular, to not send any messages as part of the first or second passes. A third pass could be utilized for 'startup' procedures.

Pass 1: examine object-graph representation; use Abstract Constructor to produce and name each object within the graph. (Now all 'points' are loaded)

Pass 2: to each object, pass the pool of objects in the configuration so that all references in the object-graph are closed. (Now all 'edges' are loaded)

Pass 3 (optional): for each object, call the 'init' method (Now everything is running).

Alternative to Pass 3 is to construct a startup procedure to run later. The delay allows easily for composing multiple object graphs. First Class support for two-pass or three-pass object-graph construction Design Patterns is quite useful for achieving the latter half of Primitives And Means Of Composition in Object Oriented Programming.


See original on c2.com