Escolar Documentos
Profissional Documentos
Cultura Documentos
Infosys
INFOSYS LIMITED
Bangalore
Date:
COPYRIGHT NOTICE
2016 Infosys Limited, Bangalore, India. All rights reserved. Infosys believes the
information in this document is accurate as of its publication date; such information is
subject to change without notice. Infosys acknowledges the proprietary rights of other
companies to the trademarks, product names and such other intellectual property rights
mentioned in this document. Except as expressly permitted, neither this document nor any
part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by
any means, electronic, mechanical, printing, photocopying, recording or otherwise, without
the prior permission of Infosys Limited and/or any named intellectual property rights
holders under this document.
Infosys Limited
Hosur Road
Electronic City, 3rd Cross
Bangalore 560 100
India.
Telephone: (91) (80)28520 261-270
Fax: (91) (80) 8520 362
Website: http://www.infosys.com
1. Please keep the content short and concise with concentration on project experience/learning.
6. The watermark need to be changed to restricted/ confidential/ highly confidential based on the confidentiality
class of the content presented in the document.
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.
So, when class adapter uses the concept of inheritance, object adapter uses the concept of
composition.
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.
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.
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:-
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.
between those and to implement the operations required by the Target but not implemented
by the Adaptee.
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.
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.
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.