Structure patterns are concerned with how classes and objects are composed - Pdf 11


ADAPTER PATTERN
BY
Sravanthi Karumanchi

Structure Pattern
Structure patterns are concerned with how
classes and objects are composed to form
large structures.
Dierent categories
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy

Scenario
Outlets and Plugs
Outlets in the US require a certain kind of plug.
For example, a plug made in India for Indian outlet may not be used
in USA.
To use these appliances in USA or vice-versa we may need to
purchase an adapter.

Motivation
Sometimes a toolkit or class library can
not be used because its interface is
incompatible with the interface required
by an application.

SpecificRequest( )
Adapter
Request( )
Adaptee->SpecificRequest( )
adaptee

Object Adapter Example
Representation
Application Adaptation Legacy System
Financial
amount()
Client
FinancialAdapter
amount()
Principal
ComputeValue()
Legacy Adaptee
{legacyadaptee.ComputeValue();}

Class Pattern
Client
Target
Request( )
Adaptee
SpecificRequest( )
Adapter
Request( )
Adaptee->SpecificRequest( )
implementation


Adapter delegates request to Adaptee.
Clients Adapter Adaptee
request
delegate

Adaptability
Use the adapter when
Want to use an existing class and its interface doesn’t match the
one we need.
(Object Adapters only) we need to use several existing subclasses,
but it’s impractical to adapt their interface by sub classing every
one. An object adapter can adapt the interface of its parent class.

Consequences
Class Adapter
Adapts Adaptee to Target by committing to a concrete Adapter class.
Lets Adapter override some of the Adaptee’s behavior by
subclassing.
Introduces only one object and no additional pointer indirection is
needed to get the adaptee.
Object Adapter
Lets a single adapter work with a group of adaptees such as a base
class and all its sub classes.
The adapter can add functioanlity to all adaptees at once.
Makes it harder to override Adaptee behavior as the Adapter may not
know with what Adaptee it is working with.

How much adapting should be done?
The amount of work Adapter does depends on how
similar the Target Interface is to Adapteee’s


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status