Broker Design Patterns: Adapters and Proxy

19
1 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Broker Design Broker Design Patterns: Adapters Patterns: Adapters and Proxy and Proxy

description

Broker Design Patterns: Adapters and Proxy. Objectives. To introduce Class and Object Adapter patterns and discuss their use To introduce the Proxy patterns and discuss its use. Topics. The Adapter/Wrapper patterns The Proxy pattern. The Adapter/Wrapper Patterns. - PowerPoint PPT Presentation

Transcript of Broker Design Patterns: Adapters and Proxy

1© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Broker Design Broker Design Patterns: Adapters Patterns: Adapters and Proxyand Proxy

2© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

ObjectivesObjectives

To introduce Class and Object Adapter patterns and discuss their use

To introduce the Proxy patterns and discuss its use

3© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

TopicsTopics

The Adapter/Wrapper patterns

The Proxy pattern

4© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Adapter/Wrapper The Adapter/Wrapper PatternsPatterns

Often a component has reusable function but not a usable interface.

An adapter or wrapper is a component that provides a new interface to an existing component.

Analogy: electrical or plumbing adapters

5© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Class and Object Class and Object AdaptersAdapters

An adaptee may be given a new interface by an adapter in two ways:

• Inheritance—The adapter may sub-class the adaptee; this is the Class Adapter pattern

•Delegation—The adapter may hold a reference to the adaptee and delegate work to it; this is the Object Adapter pattern

6© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Class Adapter StructureClass Adapter Structure

computation using req()

«interface»DesiredInterface

request()

AdapteeClient

Adapter

request() req()

7© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Object Adapter Object Adapter StructureStructure

computation using req()

«interface»DesiredInterface

request()

ClientAdapter

request()

Adaptee

req()

8© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Object Adapter Object Adapter BehaviorBehavior

:Adapter:Client :Adaptee

request()req()

sd ObjectAdapterBehavior

9© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Example: A Thread-Safe Example: A Thread-Safe Priority Queue—Priority Queue—ProblemProblem

PriorityQueue

+ enter( j:Job )+ leave() : Job+ isEmpty() : boolean

class PriorityQueue { … public void enter( Job j ) { … } public Job leave() { … } public boolean isEmpty() { … }}

PriorityQueue works properly but is not thread-safe—how can we reuse this class in a thread-safe way?

10© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Example: A Thread-Safe Example: A Thread-Safe Priority Queue—Class Priority Queue—Class AdapterAdapter

PriorityQueue

+ enter( j:Job )+ leave() : Job+ isEmpty() : boolean

ThreadSafePriorityQueue

+ enter( j:Job ) { synchronized }+ leave() : Job { synchronized }

class PriorityQueue { … public void enter( Job j ) { … } public Job leave() { … } public boolean isEmpty() { … }}

class ThreadSafePriorityQueue extends PriorityQueue{ public synchronized void enter( Job j ) { super.enter( j ); } public synchronized Job leave() { return super.leave(); }}

11© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Example: A Thread-Safe Example: A Thread-Safe Priority Queue—Object Priority Queue—Object AdapterAdapter

PriorityQueue

+ enter( j:Job )+ leave() : Job+ isEmpty() : boolean

ThreadSafePriorityQueue

+ enter( j:Job ) { synchronized }+ leave() : Job { synchronized }+ isEmpty() : boolean

adaptee

class PriorityQueue { … public void enter( Job j ) { … } public Job leave() { … } public boolean isEmpty() { … }}

class ThreadSafePriorityQueue { private PriorityQueue adaptee; public ThreadSafePriorityQueue { adaptee = new PriorityQueue(); } public synchronized void enter( Job j ) { adaptee.enter( j ); } public synchronized Job leave() { return adaptee.leave(); } public boolean isEmpty() { return adaptee.isEmpty() }}

12© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

More Adapter More Adapter ExamplesExamples

Adding an adapter to a text editing class to make it resemble the members of a shape editing class in a graphical editor

Adding a class interface to fundamental data types that have atomic values (as in Java)

Wrapping legacy code to provide an OO interface

13© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

When to Use AdaptersWhen to Use Adapters

The current context of use expects a certain interface.

A simplified interface is needed.

Operations with slightly different functionality are needed.

14© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Proxy PatternThe Proxy Pattern

Stand-ins for object may be needed because the real object

• Is not locally available;• Is expensive to create; or• Needs protected access for security or

safety. The stand-in must

• Have the same interface as the real object;• Handle as many messages as it can;• Delegate messages to the real object

when necessary. Analogy: a stand-in or proxy

15© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Proxy Pattern StructureProxy Pattern Structure

RealSupplierProxySupplier

«interface»Supplier

request()

Client

16© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Proxy Pattern BehaviorProxy Pattern Behavior

:Client

request()

request() request()

:ProxySupplier :RealSupplier

sd ProxyBehavior

17© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Example: Image ProxyExample: Image Proxy

ProxyImage

«interface»Image

copy() : Image display() crop()

Clientactual

RealImage

- data

clone() : RealImage

18© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

When to Use ProxiesWhen to Use Proxies

Use the Proxy pattern whenever the services provided by a supplier need to be mediated or managed in some way without disturbing the supplier interface.

Kinds of proxies:• Virtual proxy—Delay the creation or loading

of large or computationally expensive objects• Remote proxy—Hide the fact that an object is

not local• Protection proxy—Ensure that only authorized

clients access a supplier in legitimate ways

19© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

SummarySummary

The Adapter patterns use inheritance (Class Adapter) or delegation (Object Adapter) to change the interface of an existing component for reuse.

The Proxy pattern provides a stand-in for a real object that mediates interaction with a client without disturbing its interface.