Tuesday, September 1, 2009

Pattern to low couple to proprietary frameworks

Most Java programmers that are using proprietary libraries or other immutable frameworks are faced with a problem: Every time you need some functionality to work with a framework class you build procedural or even worse redundant code. This leads to high coupling between the framework and the rest of the application.

Here are some thoughts to solve this problem:

Inheritance
The additional functionality is added to an inherited class. This often leads to many casts and instanceof in the source code.

Simple Wrapper
A class that holds the class of the framework. All additonal functionality is added by the wrapper class. Whenever the framework class is needed it can be retrieved by a getter of the wrapper class.

Decorator
Using the decorator pattern often is not possible. The reason is that the framework class has to implement a shared interface with the decorater class but there is no way to change the framework. If the framework class does not implement such an interface the decorator pattern can not be used.

Monkey Patching
For Java-Programmers this is unusual, but often used in ruby and other dynamic languages. With the rise of JRuby and Groovy you can now patch your Java framework classes easily and follow TDA to get DRY code. In some static languages like Scala this is possible, too (see Pimp my Library).
In my opinion monkey patching is the best way to work with unchangable frameworks.

2 comments:

  1. every thing depends from language features, for example python have buildin decorators and this solution is independent from particular frameworks, but many python framework using it.

    ReplyDelete
  2. Merkur 15c Safety Razor - Barber Pole - Deccasino
    Merkur 15C Safety https://deccasino.com/review/merit-casino/ Razor - Merkur - https://septcasino.com/review/merit-casino/ 15C wooricasinos.info for Barber https://jancasino.com/review/merit-casino/ Pole is the perfect introduction to the Merkur Safety gri-go.com Razor.

    ReplyDelete