Abstract Constructor

In an OOP language, an AbstractConstructor is a mechanism to polymorphically produce an object of an AbstractBaseClass.

Most OOP languages do not support the AbstractConstructor pattern as a language feature, but rather require using DesignPatterns such as AbstractFactory, PluginArchitecture, or some combination thereof.

AbstractConstructor supports ConstructorInjection which is a DependencyInjection pattern.

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, AbstractConstructor benefits from PolicyInjection to select heuristically between available options (e.g. allowing user to decide between favoring performance, precision, or accuracy).

AbstractConstructor 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.

AbstractConstructor does not imply GarbageCollection, but AbstractConstructor + GarbageCollection is a powerful combination in that it allows one to return singletons and other shared objects from the AbstractConstructor. Without GarbageCollection, 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 SetterInjection 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, AbstractConstructor 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.

Alternative to Pass 3 is to construct a startup procedure to run later. The delay allows easily for composing multiple object graphs. FirstClass support for two-pass or three-pass object-graph construction DesignPatterns is quite useful for achieving the latter half of PrimitivesAndMeansOfComposition in ObjectOrientedProgramming.


See: DependencyInjection, NewConsideredHarmful, PolicyInjection


EditText of this page (last edited November 10, 2014) or FindPage with title or text search