Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Broker Design Patterns: Adapters and Proxy."— Presentation transcript:

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

2 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2 Objectives  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 3 Topics  The Adapter/Wrapper patterns  The Proxy pattern

4 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 The Adapter/Wrapper Patterns  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 5 Class and Object Adapters 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 6 Class Adapter Structure

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

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

9 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9 Example: A Thread-Safe Priority Queue—Problem 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 10 Example: A Thread-Safe Priority Queue—Class Adapter

11 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11 Example: A Thread-Safe Priority Queue—Object Adapter

12 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 More Adapter Examples  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 13 When 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 14 The 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 15 Proxy Pattern Structure

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

17 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17 Example: Image Proxy

18 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18 When 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 19 Summary  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.


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

Similar presentations


Ads by Google