Software Adapters : Class and Object level adapters

Software Adapters – Class and Object level adapters

Now let us look into adapters specific to the software industry. In the software industry as well adapters are needed to adapt various types of heterogeneities. Heterogeneity can be in the form of platforms, languages, method signatures ,protocols etc.

Adapter is the only design pattern which is of both the scopes ie it can be of Class scope as well as of Object scope ie Adapters in software can be implemented both using class inheritance as well as object composition.

So we will first discuss Class level adapter followed by Object level adapter and then discuss the pros and cons of the same.

Class Level Adapter

The figure below shows the class diagram of a class level adapter

Figure- Figure

In this class diagram , there are two classes representing two systems viz the SrcSystem and the TargetSystem. These are two classes have heterogeneous interfaces. Hetrogenity in interfaces is depicted here by having making the SrcEntity and TargetSystem having different signatures. SrcEntity has a method request() while the TargetSystem has a method specificRequest(). There is also a class - Adapter representing the adapter. As the Adapter should know both the interfaces, one way of doing the same is by using multiple inheritance ie Making the Adapter class inherit from both the SrcEntity and the TargetSystem.

As the Adapter is inheriting from the SrcEntity , it can override the request() in the Adapter class. The Adapter in turn can call the specificRequest() method of the TargetSystem.

Hence when the client invokes the request() method the request() method of the Adpater will get called. If any preprocessing needs to be done will be done and then the specificRequest() method of the TargetSystem will be called. The results from the TargetSystem is post processed and finally returned to the client. This is how adaption happens in a class level adapter.

Object Level Adapter

As discussed before, object composition can also be used to implement adapters.

Figure below shows the class diagram of an Object Level Adapter.

Figure- Figure

As can be seen in the class diagram, most of the things remain the same .. The difference being , here instead of having a class called as TargetSystem, we will have an interface called as TSInterface which has a method called specificRequest(). There can be one or more implementation classes of this interface which will override the specificRequest() method providing different implementations. Also in the Adapter class instead of extending from the TargetSystem as in the class level adapter , the Adapter will now keep a reference to the TSInterface.

Hence for any request from the client the request() method of the Adapter gets called … any translations that need to be done can be performed in the preprocessing stage of the method, subsequently the specificRequest() method is invoked on one of the implementation classes. Any return results may be retranslated in the post processing stage and the result will then be returned to the Client.

This is how Object level adapter functions.

Now let us compare both the mechanisms and evaluate the pros and cons of each one of them.

What can an Object level adapter do that a Class level adapter cannot do?

The first big difference is … A Class level adapter can adapt a single source to single target while an Object level adapter can adapt a single source to multiple targets. As Object level adapter uses Object Composition which is nothing but simple association and polymorphism , hence its gets the flexibility of Adapting a single source to multiple targets.

As there is no level of indirection involved, Class level adapters gives better performance as compared to Object level adapters.

Now what can a Class level adapter do that an Object level adapter cannot do?

Class level adapter if need be can override the specificRequest() method [due to multiple inheritance ] of the TargetSystem which an Object Level adapter cannot do. Object level adapter cannot do this as it just keeps a reference to the TargetSystem.

Another problem with class level adapters is only C++ as a programming language supports multiple inheritance while most of the other frequently used programming languages don’t support multiple inheritance. Hence Class level adapters are mostly limited to C++ programming language.

As flexibility is desirable in most of the cases, Object level adapters are more frequently used as compared to Class level adapters.

As we have seen from an implementation point of view, adapter is one of the very simple design patterns but from an application point of view it is one of the most important patterns.

Let us now discuss some other applications of adapter design pattern in the software industry. This is one of the patterns which has changed the way the software industry functions.

To understand this application of Adapters we will look into the architecture of the Java Programming language.

Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer