When To Use Adapter?
When To Use Proxy?
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
They encapsulate complex structures.
Convert the Interface of a class into
another interface clients expect. Client uses.
Defines the domain-specific interface that
Also Known As Wrapper
Adapts the interface Adaptee to the Target interface.
Defines an existing interface that needs adapting.
Collaborates with objects conforming to the Target interface.
• Sometimes a toolkit or class library can
not be used because its interface is
incompatible with the interface required by
• 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
• Media Player
Adv anceMediaPlay er
MessageSt at em ent s
Dat abasePersist enceAdapt er
+ long messageId
+ String message
+ Timestamp timestamp
+ String client
I Dat abasePersist ence Implements
I MessagePersist ence
Abst ract Dat abaseSt ore
• Provide a surrogate or placeholder for
another object to that lets the proxy it.
Maintains a referencecontrol access toaccess
• the real subject. Surrogate
Also Known As
Defines the common interface for RealSubject and
proxy so that a Proxy can be used anywhere a
RealSubject is expected.
Defines the real object that proxy represents.
• 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.
• 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
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
Firewall Proxy :Protects targets from bad clients (or vice versa).
Virtual Proxy Example
RealI m age
I m age
Pr ox y I m age
Cache Proxy Example
Cust om er Dao
I Cust om er Dao
Cust om er CachedPr ox y