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. 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.
6. 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
7. 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()
8. 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
9. 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.
10. 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.
11. 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).