Adapter & Proxy Pattern
Philip zhong Feb, 2014
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
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.
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.
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
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()
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
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.
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.
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).
Virtual Proxy Example
RealI m age

Implements
I m age
Delegates
Uses
Implements
Client

Pr ox y I m age
Cache Proxy Example
Cust om er Dao

Implements
Delegates
I Cust om er Dao
Implements
Cust om er CachedPr ox y

+ Uses

Google Cache
Dynamic Proxy Example
Dynamic Proxy Example
Remote Proxy Example
• Java RMI(Remote Method Invocation)
Synchronization Proxy Example
Table

Implements
I Table

Delegates
Implements
Row Lock TablePr ox y
Q & A

Adapter Poxy Pattern

  • 1.
    Adapter & ProxyPattern Philip zhong Feb, 2014
  • 2.
    Agenda • • • • • • • • • • Pattern Taxonomy What’s Adapter? AdapterMotivation When To Use Adapter? Adaptor Examples What’s Proxy? Proxy Motivation When To Use Proxy? Proxy Types Proxy Example
  • 3.
    Pattern Taxonomy  StructuralPatterns 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.
    What’s Adapter Convert theInterface 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.
  • 6.
    Adapter Motivation • Sometimesa 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
  • 7.
    Adapter Example • MediaPlayer 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()
  • 8.
    Adapter Examples OracleSt ore MessageStat 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
  • 9.
    What’s Proxy • Providea 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.
  • 10.
    Proxy Motivation • Thereare 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.
  • 11.
    Proxy Types • VirtualProxy :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).
  • 12.
    Virtual Proxy Example RealIm age Implements I m age Delegates Uses Implements Client Pr ox y I m age
  • 13.
    Cache Proxy Example Custom er Dao Implements Delegates I Cust om er Dao Implements Cust om er CachedPr ox y + Uses Google Cache
  • 14.
  • 15.
  • 16.
    Remote Proxy Example •Java RMI(Remote Method Invocation)
  • 17.
    Synchronization Proxy Example Table Implements ITable Delegates Implements Row Lock TablePr ox y
  • 18.