Vous êtes sur la page 1sur 18

Design

Design Patterns
Patterns
CE00362-3
CT070-3-3

Structural Pattern Adapter


Introduction & Overview

CT070-3-3 - Design Patterns Adapter Slide 1 (of 12)


Learning Outcomes

At the end of this session, you should be


able to;

Know about Structural Design pattern


Describe benefits of Adapter pattern
Recognise and apply Adapter pattern
given a scenario

CT070-3-3 - Design Patterns Adapter Slide 2 (of 12)


Structural Design patterns
Structural patterns provide different ways
to create a class structure, for example
using inheritance and composition to
create a large object from small objects.

CT070-3-3 - Design Patterns Adapter


Structural Design Pattern
These patterns help us to structure objects, the
relationships and inheritance between classes.
They help us to
Adapt between interfaces.
Aggregate and manage objects.
Add additional functionality to an object at
runtime.
Abstract (hide) complex code (or interface)
behind a more simplified interface, as well
as share and shuttle data between processes.
CT070-3-3 - Design Patterns Adapter
Structural Design Pattern -Types

Adapter

Bridge

Decorator

Flyweight

Proxy

CT070-3-3 - Design Patterns Adapter


What Happens?

When you want to provide a service


that a client needs,
You often cant negotiate the
method names the client wants to
call
You have to support the interface
the client expects.

CT070-3-3 - Design Patterns Adapter Slide 7 (of 12)


Description - Adapter

Name: Adapter
Problem:
An "off the shelf" component offers compelling
functionality that you would like to reuse, but its
"view of the world" is not compatible with the
philosophy and architecture of the system
currently being developed.

CT070-3-3 - Design Patterns Adapter Slide 4 (of 12)


Description - Adapter
Context
Adapter is about creating an intermediary abstraction
that translates, or maps, the old component to the new
system. Clients call methods on the Adapter object
which redirects them into calls to the legacy component.
This strategy can be implemented either with
inheritance or with aggregation.
Adapter functions as a wrapper or modifier of an
existing class. It provides a different or translated view
of that class.

CT070-3-3 - Design Patterns Adapter


Description - Adapter
Solution:
Convert the interface of a class into another interface
client expects.
Identify the players: the component(s) that want to be
accommodated (i.e. the client), and the component that
needs to adapt (i.e. the adaptee).
Identify the interface that the client requires.
Design a "wrapper" class that can "impedance match" the
adaptee to the client.
The adapter/wrapper class "has a" instance of the
adaptee class.The adapter/wrapper class "maps" the
client interface to the adaptee interface.
The client uses (is coupled to) the new interface
CT070-3-3 - Design Patterns Adapter Slide 5 (of 12)
Description - Adapter

Also Known As: Wrapper


Applicability:
- You want to use an existing class, and its
interface does not match the one you need.
- You want to create a reusable class that
cooperates with unrelated or unforeseen
classes, that is, classes that dont necessarily
have compatible classes.

CT070-3-3 - Design Patterns Adapter


Adapter Pattern Class Design

CT070-3-3 - Design Patterns Adapter Slide 6 (of 12)


Adapter Pattern Case Study

Sample Code

CT070-3-3 - Design Patterns Adapter


Adapter

You may have an existing class with different


method names
Apply Adapter
Provide the interface a client expects using
the services of a class with a different
interface

CT070-3-3 - Design Patterns Adapter Slide 8 (of 12)


Adapter Case Study

Lloyds bank is an international bank offers


services worldwide. For offshore account
holders, the tax rate is 0.04%. And, in India it
offers two types of accounts, Standard and
Platinum. Tax rules are not applied for an Indian
bank accounts. Now the offshore bank is
incompatible to Indian account types. We need
to design an Account Adapter to make both the
incompatible account types to work together.

CT070-3-3 - Design Patterns Adapter


Solution

CT070-3-3 - Design Patterns Adapter


Summary

Provide the interface a client expects using


the a class with a different interface.
Make your services more usable by
defining an interface.
Its dangerous to adapt when a client
doesnt expect it

CT070-3-3 - Design Patterns Adapter Slide 10 (of 12)


References

Steven John Metsker, Design Patterns Java


Workbook, Addison Wesley

Erich Gamma et. al., Design Patterns


Elements of Reusable Object-Oriented
Software, Addison Wesley

CT070-3-3 - Design Patterns Adapter Slide 11 (of 12)


Question and Answer Session

Q&A

CT070-3-3 - Design Patterns Adapter Slide 12 (of 12)

Vous aimerez peut-être aussi