1. Trang chủ
  2. » Giáo án - Bài giảng

Structure patterns are concerned with how classes and objects are composed

17 332 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 17
Dung lượng 112 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Adapter Pattern tells us how to wrap up existing classes inside a new target interface, when the need arises to reuse existing class implementations but with a different interface for th

Trang 1

ADAPTER PATTERN

BY Sravanthi Karumanchi

Trang 2

Structure Pattern

Structure patterns are concerned with how classes and objects are composed to form large structures Different categories

Adapter

Bridge

Composite

Decorator

Façade

Flyweight

Proxy

Trang 3

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

Trang 4

Sometimes a toolkit or class library can not be

used because its interface is incompatible with the interface required by an application

We can not change the library interface, since we may not have its source code

Even if we did have the source code, we probably should not change the library for each

domain-specific application

Trang 5

Adapter Pattern

Adapters are used to enable objects with different interfaces to communicate with each other

Adapter Pattern tells us how to wrap up existing classes inside a new target interface, when the

need arises to reuse existing class implementations but with a different interface for the clients

They are also termed as wrappers

Trang 6

Variations in Adapters

Class Adapters

Use multiple inheritance to compose classes

Object Adapters

Object adapters use a compositional technique

to adapt one interface to another

Trang 7

Object Pattern

Request( )

Adaptee SpecificRequest ( )

Adapter Request( )

Adaptee->SpecificRequest( ) adaptee

Trang 8

Object Adapter Example

Representation

Financial

amount()

Client

FinancialAdapter amount()

Principal

ComputeValue()

Legacy Adaptee

{legacyadaptee.ComputeValue();}

Trang 9

Class Pattern

Request( )

Adaptee SpecificRequest ( )

Adapter Request( )

Adaptee->SpecificRequest( ) implementation

Trang 10

Class Adapter Example

Target Class

class RoundPeg { public: void virtual roundPegOperation = 0; }

Adaptee Class

class OldSquarePeg {

public: void squarePegOperation()

{{ do something } }

Adapter Class

class PegAdapter: private OldSquarePeg, public RoundPeg { public: void virtual roundPegOperation()

{ add some corners; squarePegOperation(); } }

Client

Trang 11

Object Adapter Example

Target Class

class RoundPeg { public: void virtual roundPegOperation = 0; }

Adaptee Class

class OldSquarePeg { public: void squarePegOperation() { do something } }

Adapter Class

class PegAdapter: public RoundPeg {

private: OldSquarePeg* square;

public: PegAdapter() { square = new OldSquarePeg; }

void virtual roundPegOperation() { add some corners;

square->squarePegOperation(); }

Trang 12

Clients call operations on the Adapter

instance and Adapter delegates request to Adaptee.

Trang 13

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

Trang 14

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.

Trang 15

How much adapting should be done?

The amount of work Adapter does depends on how similar the Target Interface is to Adapteee’s

Does the adapter provide two-way transparency?

A two-way adapter supports both the Target and the

Adaptee interface It allows an adapted object

(Adapter) to appear as an Adaptee object or a Target object.

Implementation Issues

Trang 16

Two-way Adapter

A two-way adapter would also allow a RoundPeg be used in place of the SquarePeg

class OldSquarePeg { public: void virtual

squarePegOperation() { function } }

class RoundPeg { public: void virtual

roundPegOperation() { function } }

class PegAdapter: public OldSquarePeg, RoundPeg

{ public: void virtual roundPegOperation() { add some corners; squarePegOperation(); }

void virtual squarePegOperation() { add some corners; roundPegOperation(); } }

Trang 17

Pluggable Adapters

A class is more reusable when you

minimize the assumptions other classes must make to use it.This is achieved by building interface adaptation.

Ngày đăng: 19/03/2014, 22:32

TỪ KHÓA LIÊN QUAN

w