Frameworks vs Libraries as Inheritance vs Composition?

For quite some time inheritance was the dominant model of structuring programs in OO languages like Java. Very often it was used as a mechanism for reusing code – “common” functions where placed in an abstract class, so that subclasses can use them.

However, this often proves to be very limiting, as you can only inherit from a single class. The code becomes constrained and tied to one particular framework-specific class. Not to mention testing – such base classes often depend on outside state, making tests hard to setup.

That’s why nowadays time and again you can hear that you should prefer composition over inheritance (see for example this StackOverflow question). When using composition, you can leverage multiple re-usable chunks of code, and combine them in an arbitrary way. Also using IoC/Dependency Injection strongly favors composition.

I think the above inheritance-composition opposition strongly resembles the framework-library distinction. When using a framework, you are forced into a specific structure, where you must model your code in one specific way. Quite obviously it’s often hard or impossible to use two frameworks in one layer/module. That’s how hacks, ugly workarounds, reflection madness, etc. is born.

Libraries on the other hand (unless they are deliberately wrongly written), can be freely combined. Just like composition of classes, you can compose usage of many libraries in one module. Your code can be kept clean and only use the library functionality it really requires.

I’m not saying that frameworks are bad – just like inheritance, they may be very useful if used in the correct places. However, next time you put your code into a framework, maybe it’s better to think twice: can this functionality be implemented using composition, with the help of a library? Won’t this make my code cleaner and more maintainable?

Adam

  • Wolfgang

    Good point about reuse: frameworks do not scale from one to many frameworks.

    Another criterion for the decision between framework and library: state and/or lifecycle.

    If the lifecycle of an object is an important aspect, the object should be managed by a framework. The point of control should be in the framework, so that there is a defined order of execution.

    If on the other hand the functions can be used without restrictions on order or lifecycle stages, they should be put into a library.

  • http://perlalchemy.blogspot.com Zbigniew Łukasiak

    Doesn’t the definition of Framework imply using inheritance? Even if not – then there must be a direct analogy – in Framework it is the Framework code that calls your code – just like in the Template Method pattern (involving inheritance).

    Some time ago I’ve written: http://perlalchemy.blogspot.com/2010/02/frameworks-are-framing-libraries-are.html

  • http://www.warski.org Adam Warski

    Not really, you could have a framework where you pass in callbacks as parameters, I suppose. These callbacks could implement an interface, but otherwise be ordinary objects.

  • Pingback: JavaPins()

  • Olivia Jennifer

    Wonderful article. But recently if you are looking
    for a new project management job or if you might have asked for a promotion
    within your company then you might have heard about PMP credentials!!. It is
    true that a
    Project
    Management Professional (PMP)

    credential is one of the most sought certifications by employers and employees
    alike. For its detail information about a PMP certifications you can go through
    http://www.pmstudy.com/