Title: Chapter 8 Structural Patterns: Proxy and Faade
1Chapter 8 -Structural Patterns Proxy and
Façade
CIS 476/566 Software Architecture and Design
Patterns
- Dr. Brahim Medjahed
- brahim_at_umich.edu
2Agenda
- Proxy Pattern
- Façade Pattern
3Design Patterns in GoF The Big Picture
4Proxy Pattern
5Proxy Pattern - Intent
- One of the most used patterns
- Provide a surrogate or placeholder for another
object to control access to it - Reduces the cost of accessing objects
- Uses another object (the proxy) that acts as a
stand-in for the real object - The proxy creates the real object only if the
user asks for it
6Motivation
- A proxy is
- A person authorized to act for another person
- An agent or substitute
- Authority to act for another
- There are situations in which a client does not
or can not reference an object directly, but
wants to still interact with the object - A proxy object can act as the intermediary
between the client and the target object
7Client Proxy Real 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
8Scenario 1
- Document editor that embed graphical objects in a
document - Creating graphical objects expensive
- But, opening a document should be fast!
- Solution
- Creating expensive objects on demand (e.g., when
an object becomes visible) - But, we dont want to complicate the editors
implementation - What do we put in the document in place of the
image?
9Scenario 1 (contd)
- Solution (contd)
- Images are stored and loaded separately from text
- If a RealImage is not loaded a ProxyImage
displays a grey rectangle in place of the image - ProxyImage also stores its extent, that is its
width and height - The client cannot tell that it is dealing with a
ProxyImage instead of a RealImage
10Scenario 1 UML Class Diagram
11Scenario 2
- An Internet Service Provider (ISP) notices that
many of its clients are frequently accessing the
same web pages, - Multiple copies of the web documents being
transmitted through its server. - Solution ISP's server can cache recently
accessed pages - When a client request arrives, the server can
check to see if the document is already in the
cache and then return the cached copy. - The ISP's server accesses the target web server
only if the requested document is not in the
cache or is out of date. - The cache acts as a proxy
12Proxy Pattern - Applicability
- Proxies are useful wherever there is a need for a
more sophisticated reference to a object than a
simple pointer or simple reference can provide
13Proxy Pattern Applicability (contd)
- Remote Proxy - Provides a reference to an object
located in a different address space - Proxy local representative of the real subject
in a different address space (e.g., Stubs in RPC) - Virtual Proxy - Allows the creation of a memory
intensive object on demand (e.g., scenario 1). - Protection Proxy - Provides different clients
with different levels of access to a target
object (see Exercise 2)
14Proxy Pattern Applicability (contd)
- Smart Reference Proxy - Provides additional
actions whenever a target object is referenced - Counting the number of references to the object
- Lock real subject to prevent that other client
simultaneously change it. - Cache Proxy - Provides temporary storage of the
results of expensive target operations so that
multiple clients can share the results (e.g.,
scenario 2)
15Proxy Pattern - Structure
16Proxy Pattern Structure At Run-Time
17Participants
- Proxy
- Maintains a reference that lets the proxy access
the real subject. - Provides an interface identical to Subject's so
that a proxy can by substituted for the real
subject. - controls access to the real subject and may be
responsible for creating and deleting it. - Subject
- 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 the proxy
represents.
18Example
- Code for UML class diagram in slide 15
- do-It() displays the message called
realSubjet.do-It()
19Example - Client
- // Client test application class
Client static void Main() //
Create proxy and request a service Proxy p
new Proxy() p.do-It()
20Example Subject and RealSubject
- // "Subject" abstract class Subject
public abstract void do-It()
// "RealSubject" class RealSubject
Subject public override void
do-It() Console.WriteLine("Called
RealSubject.do-It()")
21Example - Proxy
- // "Proxy" class Proxy Subject RealSu
bject rs public override void
do-It() if (rs null)
rs new RealSubject() rs.do-It(
)
22Exercise 1
- Implement UML Class Diagram in Slide 10
23Exercise 1 - Implementation
24Exercise 2
- The RealClient class stores an account number.
Only users who know a valid password can access
this account number. - The RealClient is protected by a ProtectionProxy
which knows the password. - If a user wants to get an account number, first
the proxy asks the user to authenticate only if
the user entered a correct password does the
proxy invoke the RealClient to get the account
number for the user. - Questions
- UML Class Diagram
- Implementation
25Exercise 2 UML Class Diagram
26Exercise 2 Implementation
27Façade Pattern
28Façade Pattern - Intent
- Provides a unified interface to a set of objects
in a subsystem. - A facade defines a higher-level interface that
makes the subsystem easier to use.
29Motivation
- Context
- Often, an application contains several complex
packages. - A programmer working with such packages has to
manipulate many different classes - Problem
- Programmers need to use only a subset of a
complex system - Or, they need to interact with the system in a
particular way - Example record all calls to specific methods
- Solution
- Façade presents a new interface for the clients
of the existing system
30Interaction Through Façade
Client Classes
Subsystem Classes
31Scenario
Compiler
32Façade Pattern - Structure
33Participants
- Façade
- Knows which subsystem classes are responsible for
a request. - Delegates client requests to appropriate
subsystem objects. - Subsystem classes
- Implement subsystem functionality.
- Handle work assigned by the Façade object.
- Have no knowledge of the façade and keep no
reference to it.
34Façade - Applicability
- You want to provide a simple interface to a
complex subsystem - There are many dependencies between clients and
the implementation classes of an abstraction - You want to layer your system
35Façade - Collaborations
- Client communicate with the subsystem by sending
requests to Façade, which forwards them to the
appropriate subsystem objects - Clients that use the façade dont have to access
its subsystem objects directly
36Example
37Exercise
- Use the Facade pattern as a MortgageApplication
object which provides a simplified interface to a
large subsystem of classes measuring the
creditworthyness of an applicant. - The façade has one method IsEligible(Customer
cust, int amount) - The subsystem has 4 classes
- Customer constructor
- Bank hasSufficientSavings()
- Loan hasNoBadLoans()
- Credit hasGoodCredit()
- Questions
- UML class diagram
- Implementation
38Exercise UML Class Diagram
39Exercise Implementation