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
, or some combination thereof.
which is a DependencyInjection
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).
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.
does not imply GarbageCollection
, but AbstractConstructor
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.
- Pass 1: examine object-graph representation; use AbstractConstructor 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. FirstClass
support for two-pass or three-pass object-graph construction DesignPatterns
is quite useful for achieving the latter half of PrimitivesAndMeansOfComposition