Beruflich Dokumente
Kultur Dokumente
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.
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
etc.
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.
Definition:-
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
interfaces."
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.>
Classification:-
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).
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
composition.
Ill be demonstrating the difference with our case study later.
Implementation:-
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.
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
Composition.
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.
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.