Intermediate Implementation

Intermediate Implementation is an abstract class or an interface redefining a base interface for a new family of derived classes.

 

Explanation

While designing software, we come up with interfaces that potentially lead to several (inter-changeable) implementations of it. When implementing this interface, we often notice commonalities between different implementations. These commonalities create duplicates in code and needless lines of code to understand the actual behavior of the implementation. For that reason, it makes sense to extract that common part of code into a new, intermediate implementation of the interface. Intermediate implementation becomes a new kind of interface (specifically, an abstract class), which is implemented by particular kind of child classes. These child classes prefer to work with a new, derived kind of operation instead of an original (base) operation. In other words, Intermediate Implementation gives a freedom of choice between the base classes for the specific implementer.

Eventually, a client application is aware of the base (IBaseOperation) interface only. Thanks to the transitive nature of the inheritance, base interface can be replaced by either direct implementation, or one of those implementations deriving from the Intermediate Implementation.

Intermediate Implementation is a potential target of unit-testing, which will ensure that correct translations/transfers happen from base to derived methods.

 

Example

Example of Intermediate Implementation can be expressed if we consider part of software responsible for syntax check of a sentence (see diagram below). ISentenceChecker interface is responsible for checking the syntax of a sentence. Classes directly implementing this interface accept a whole sentence by considering grammar rules between the words.

Let's say, one day we decided to rewrite how syntax checker works - now it only needs to check words in the sentence, while the structure of the whole sentence does not matter anymore. When rewriting each implementation, we will notice that sentence-to-word conversion stays the same between them, which we put into the Intermediate Implementation (WordChecker).

Classes directly implementing WordChecker abstract class don't really care about sentence-to-word conversion, they simply accept words and work on them.

Share If You Like It