Daily Patterns #09
-
Upload
vladislav-bauer -
Category
Documents
-
view
216 -
download
0
Transcript of Daily Patterns #09
-
7/31/2019 Daily Patterns #09
1/2
proxy design pattern...Intent
Provide a surrogate orplaceholder for another
object to controlaccess to it.
Use an extra level ofindirection to support
distributed, controlled,or intelligent access.
Add a wrapper anddelegation to protectthe real component
from undue complexity.
Problem
You need to supportresource-hungry
objects, and you do notwant to instantiate suchobjects unless and until
they are actuallyrequested by the client.
Discussion
Design a surrogate, or proxy, object that:instantiates the real object the first time the clientmakes a request of the proxy, remembers theidentity of this real object, and forwards theinstigating request to this real object. Then allsubsequent requests are simply forwarded directlyto the encapsulated real object.
There are four common situations in which the Proxy pattern is applicable.
1. A virtual proxy is a placeholder for expensive to create objects. The realobject is only created when a client first requests/accesses the object.
2. A remote proxy provides a local representative for an object that resides ina different address space. This is what the stub code in RPC and CORBAprovides.
3. A protective proxy controls access to a sensitive master object. Thesurrogate object checks that the caller has the access permissions requiredprior to forwarding the request.
4. A smart proxy interposes additional actions when an object is accessed.Typical uses include:
- Counting the number of references to the real object so that it can befreed automatically when there are no more references (aka smart
pointer),- Loading a persistent object into memory when its first referenced,- Checking that the real object is locked before it is accessed to ensurethat no other object can change it.
-
7/31/2019 Daily Patterns #09
2/2
The Proxy provides a surrogateor place holder to provide
access to an object. A checkor bank draft is a proxy forfunds in an account. A check
can be used in place of cashfor making purchases and
ultimately controls access tocash in the issuers account.
By defining a Subjectinterface, the presence of the
Proxy object standing in placeof the RealSubject is
transparent to the client.
http://www.youtube.com/watch?v=I-ZZVTqFe2E
Check list
1. Identify the leverage oraspect that is best
implemented as a wrapper orsurrogate.
2. Define an interface that will make the proxyand the original component interchangeable.
3. Consider defining a Factory that canencapsulate the decision of whether a proxyor original object is desirable.
4. The wrapper class holds a pointer to thereal class and implements the interface.
5. The pointer may be initialized atconstruction, or on first use.
6. Each wrapper method contributes itsleverage, and delegates to the wrappeeobject.
Adapter provides a differentinterface to its subject. Proxyprovides the same interface.
Decorator provides an enhancedinterface.
Decorator and Proxy have
different purposes but similarstructures. Both describe howto provide a level of indirection
to another object, and theimplementations keep a
reference to the object towhich they forward requests.
http://www.youtube.com/watch?v=I-ZZVTqFe2Ehttp://www.youtube.com/watch?v=I-ZZVTqFe2Ehttp://www.youtube.com/watch?v=I-ZZVTqFe2Ehttp://www.youtube.com/watch?v=I-ZZVTqFe2E