Você está na página 1de 10

Adapter Pattern using Project Management Activity

Disclaimer: - This is a simple case study created by me for understanding of Adapter Pattern. There is no evidence of Adapter pattern used in the given Project
Management activity.

Design Pattern definition:-

In software engineering, a software design pattern is a general reusable solution to a
commonly occurring problem within a given context in software design. It is not a finished
design that can be transformed directly into source or machine code.

Here we will discuss the demonstration of Adapter Pattern in Task allocation to the team for a
system development (A Project Management Activity). Firstly lets talk about what Adapter
Pattern is.
The Adapter Pattern is like and adapter of a plug in real life. It is used like an interface or a
bridge between two objects. In real world we have adapter for power supplies, memory chips
In Software Development, adapter has same application. Suppose we have one class which
expects some type of objects. Now we have one more object having same features but
exposing a different interface. Of course we need to use both of them with same application
code without implementing a new class or changing existing application code. Here we can
create an adapter between these two incompatible objects and classes.

According to Gang of Four (Erich Gamma, John Vlissides, Ralph Johnson, and
Richard Helm), the intent of using adapter pattern is to
"Convert the interface of a class into another interface that clients expect. Adapter
lets classes work together that couldn't otherwise because of incompatible

To understand this definition let us use a simple example. Suppose you know only English and
Hindi and you are going to a Spain where you need to meet your clients who knows only
Spanish. You dont have any prior experience with Spanish language. You will be needing a
mediator or translator for make things work. The translator/mediator here must know both
Spanish and your language. You and your client will be both interacting with him. This
translator/mediator is acting as an adapter here which is helping you to do your job.
Another example is when a client needs a software to be developed in J2EE and he hires you for
the job. You are managing the project and you hires a team for this job. You are not actually
taking part in the development activities but you are acting as an intermediary between client
and the development team to get the task done for him. You are playing the role of and
adapter here. <We will be actually using this example to demonstrate this design pattern here.>

There are two types of Adapters:-
1. Class Adapter pattern: - This type of adapter uses multiple interfaces (polymorphic in
nature) either implementing or inheriting the interface that is expected (e.g. Translated
language) and the interface which is preexisting (e.g. Known language).

Figure 1. Class Adapter UML Class Diagram

2. Object Adapter Pattern: - In this type of adapter pattern, the pattern contains the
instance or object of the class it wraps. In this situation, the adapter makes calls to
instance of the wrapped object.
Figure 2. Object Adapter UML Class diagram

So, when class adapter uses the concept of inheritance, object adapter uses the concept of
Ill be demonstrating the difference with our case study later.

There are basically four components of Adapter pattern.
1. Adaptee: - Defines an existing interface that needs adapting i.e. it represents the
component with which client wants to interact with.
2. Target: - Defines the domain specific interface that the client uses i.e. it basically
represents the interface of the adapter that helps the client interact with the adaptee.
3. Adapter: - Adapts the interface Adaptee to the Target interface i.e. it implements the
Target interface, defined above and connects the adaptee, with the client using the
target interface implementation.
4. Client: - The main client that wants to get the operation done from the Adaptee.

Figure 3. Class Diagram of sample Adapter Pattern

Class Adapter using case study example: -
Suppose you are working on a clients project. You have a team of developers specialized in
Java. They are working on that project directly. You are not involved in any development
activity. (Remember this is important, you are not involved in any development activity.)
Client knows the development team and he is interacting with them with your help. So client is
telling you requirements and you are allocating the work to development team. So you are
acting as an Adapter here. I will demonstrate this later.

Let us demonstrate this with a simple code.

So the project is in running state now. Now your client has a new assignment and needs to
work on C#. As you have only Java developers and you dont want to lose the project, you try to
create a new team with c# developers.
Since it is a new technology and you are having hands on experience on c#, you decide that you
will monitor the teams and get the things done. So you will act as an adapter here by being an
intermediate between client and new c# team. You get the requirement from client and pass
them to the team. Hence the updated code is as below: -

This is why you must not involve in the coding part as you are acting as an adapter here. The
main requirement that forced us to use an adapter here was that client is not compatible with
the new team added. So you must act as a mediator on behalf of your new team talk to client
and get the project done.
Here we have brought a new team in the picture using concept of inheritance. So the
components as per our code are:-
1. Adaptee: - C# Team added i.e. CSharpDevTeam class.
2. Target: - whole dev team i.e. DevTeams Class.
3. Adapter: - you represented by SalAdapter class.
4. Client: - client himself.
Object Adapter demonstration:-
Now as you have completed the whole project in Csharp, your client is very happy and he wants
to give you one more project. Now he looks compatible with the new C# team also, so he is
ready to interact with the team directly. But he wants you to be in the middle again. So this
time you will be delegating the responsibilities to the team given by client himself. Ill explain
this part in detail. Let us see the changed code for object adapter.
Notice there is only small change in the code i.e. implementation of Adapter class. Logically
both the projects are being delegated to the Csharp team itself for completion but the
difference lies in the way this is implemented. Please see the reason below for this.
The difference between object and class adapter is the basic concept behind the both. Where
class adapter uses the concept of Inheritance, Object adapter uses the concept of
So when you were handling the first project your team worked under you as inherited team. In
the next project, you were acting as a cover member for the previous team and delegated the
responsibilities to the team. This acting as a cover is the essence of composition.
Inheritance: - In object-oriented programming, inheritance is the concept that when a class of objects
is defined, any subclass that is defined can inherit the definitions of one or more general classes.

Composition: - Composition over inheritance (or composite reuse principle) in object-oriented

programming is the principle that classes should achieve polymorphic behavior and code reuse by their
composition (by containing instances of other classes that implement the desired functionality) rather
than inheritance from a base or parent class.

When to use Adapter Pattern: -

1. When you have a third-party code that cannot interact with the client code. For
example, you might want to use a third-party logger service, but your code is having
incompatibility issues, you can use this pattern.
2. When you want to use an existing code with extended functionality but not without
changing it, as it is being used in other components, you can extend it using the adapter
3. Again you can use an object adapter for a code, which is using sealed class components,
or needs multiple inheritance. For a requirement where you need to use single
inheritance, you can choose a class adapter.

How Much the Adapter Should Do?

It should do how much it has to in order to adapt. It's very simple, if the Target and Adaptee are
similar then the adapter has just to delegate the requests from the Target to the Adaptee. If
Target and Adaptee are not similar, then the adapter might have to convert the data structures
between those and to implement the operations required by the Target but not implemented
by the Adaptee.

Adapter Pattern As a special case of Strategy Pattern: -

There are many cases when the adapter can play the role of the Strategy Pattern. If we have
several modules implementing the same functionality and we wrote adapters for them, the
adapters are implementing the same interface. We can simply replace the adapters objects at
run time because they implements the same interface.
Let us see the code as per over above case study. The code is as below: -

Here the problem is that client wants both java and Csharp team in his project and will give the
requirements while project is in execution. So you have to delegate responsibilities in runtime
of the project. So the adapter class is changed in the way that it is making a decision according
to the required team instance.

What is the scope of the adapter: -

An adapter can also have some business logic. Suppose there is a need of a functionality which
is not implemented either by Adaptee or Target, that functionality an adapter can hold. For e.g.
in an Online Currency Conversion System, suppose tax calculation is not there in previous code
and currency libraries. The tax calculation part can be added in the adapter itself.
But the implementation of business logic inside adapter must be limited if necessary as it
makes cohesion between the adapter and the other components strong, and it affects the
modifiability of the system.

Application of Adapter pattern: -

Adapters are encountered everywhere. From real world adapters to software adapters
Non Software Examples of Adapter Patterns: Power Supply Adapters, card readers and
adapters, etc.
Software Examples of Adapter Patterns: Wrappers used to adopt 3rd parties libraries and
frameworks - most of the applications using third party libraries use adapters as a middle layer
between the application and the 3rd party library to decouple the application from the library.
If another library has to be used only an adapter for the new library is required without having
to change the application code.