Adapter Poxy Pattern


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Adapter Poxy Pattern

  1. 1. Adapter & Proxy Pattern Philip zhong Feb, 2014
  2. 2. Agenda • • • • • • • • • • Pattern Taxonomy What’s Adapter? Adapter Motivation When To Use Adapter? Adaptor Examples What’s Proxy? Proxy Motivation When To Use Proxy? Proxy Types Proxy Example
  3. 3. Pattern Taxonomy  Structural Patterns Adapters, Bridges, Facades, and Proxies are variations on a single theme: They reduce the coupling between two or more classes. They introduce an abstract class to enable future extensions. They encapsulate complex structures.
  4. 4. What’s Adapter Convert the Interface of a class into Target another interface clients expect. Client uses. Defines the domain-specific interface that • Adapter  Also Known As Wrapper •  Adapts the interface Adaptee to the Target interface.  Adaptee Defines an existing interface that needs adapting.  Client Collaborates with objects conforming to the Target interface.
  5. 5. Adapter Motivation • 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
  6. 6. Adapter Example • Media Player Client Uses VlcPlay er + playVlc() + playMp4() MediaPlay er Implements Implements Implements AudioPlay er + play() Uses MediaAdapt er + play() Uses Adv anceMediaPlay er Implements Mp4Play er + playVlc() + playMp4()
  7. 7. Adapter Examples OracleSt ore MessageSt at em ent s Message Dat abasePersist enceAdapt er + long messageId + String message + Timestamp timestamp + String client I Dat abasePersist ence Implements Uses Implements MessagePersist ence I MessagePersist ence Uses Client Abst ract Dat abaseSt ore Uses Implements MysqlSt ore
  8. 8. What’s Proxy • Provide a surrogate or placeholder for  Proxy another object to that lets the proxy it. Maintains a referencecontrol access toaccess • the real subject. Surrogate Also Known As  Subject Interface Defines the common interface for RealSubject and proxy so that a Proxy can be used anywhere a RealSubject is expected.  RealSubject Defines the real object that proxy represents.
  9. 9. Proxy Motivation • There are situations in which a client does not or can not reference an object directly, but wants to still interact with the object. • The proxy object has the same interface as the target object. • The proxy holds a reference to the target object and can forward requests to the target as required (delegation!). • In effect, the proxy object has the authority the act on behalf of the client to interact with the target object.
  10. 10. Proxy Types • Virtual Proxy :Allows the creation of a memory intensive object on • demand. The object will not be created until it is really needed. Cache Proxy :Provides temporary storage of the results of expensive target operations so that multiple clients can share the results. • Protection (Access) Proxy: Provides different clients with different levels of access to a target object, we can through java dynamic proxy API to do. • Remote Proxy :Provides a reference to an object located in a different • • • • address space on the same or different machine. e.g.: RMI & CORBA Synchronization Proxy :Provides multiple accesses to a target object. Copy-On-Write Proxy :Defers copying (cloning) a target object until required by client actions. Really a form of virtual proxy. Smart Reference Proxy :Provides additional actions whenever a target object is referenced such as counting the number of references to the object. Firewall Proxy :Protects targets from bad clients (or vice versa).
  11. 11. Virtual Proxy Example RealI m age Implements I m age Delegates Uses Implements Client Pr ox y I m age
  12. 12. Cache Proxy Example Cust om er Dao Implements Delegates I Cust om er Dao Implements Cust om er CachedPr ox y + Uses Google Cache
  13. 13. Dynamic Proxy Example
  14. 14. Dynamic Proxy Example
  15. 15. Remote Proxy Example • Java RMI(Remote Method Invocation)
  16. 16. Synchronization Proxy Example Table Implements I Table Delegates Implements Row Lock TablePr ox y
  17. 17. Q & A