Search code examples
oopdesign-patternsfactory-pattern

Frameworks use abstract classes - Factory method


From Addison Wesley - Design patterns.

(Factory method)

Frameworks use abstract classes to define and maintain relationships between objects. A framework is often responsible for creating these objects as well.

What does the author here mean by the above quote?
What does the framework represent, and "how" is it responsible for creating these objects as well?


Solution

  • It's not trivial to discuss this at such a general level. I'll try to.

    Usually a framework provides a set of abstract classes that are used in the framework's code, but whose binding is only resolved at runtime.

    The framework calls methods of instances of these classes, which you are responsible to implement in your code.

    That is, in your code you will extend such classes, and "fill in the blanks".

    The glue between your code and the framework's code is the contract defined by such abstract classes (or interfaces, fwiw): the framework knows that a given method takes parameters of type K, and return type T, thus can call it safely, assuming its semantic will be respected by the implementor.

    On the other hand, when you implement the method, you implicitly accept the contract attached to it and implement it respecting the semantics associated with it.

    Hope it was clear enough.