Structure |
|
Intention |
Provides a proxy for other objects to control access to this object. |
Applicability |
- Use P r o x y mode when you need to replace a simple pointer with a more general and complex object pointer. Here are some of the things you can do with P r o x y mode: 1) remote proxy provides local representation of an object in different address spaces. NEXTSTEP[ADD94] uses n x P r o x Y class to accomplish this. COPLIEN[COP92] called the agent "Ambassador" (a M b a s S A d o r). 2) virtual agent (virtual proxy) creates expensive objects as needed. The i m a G e P r o x y described in the motivation section is an example of such an agent. 3) The Protection agent (Protection proxy) Controls access to the original object. The protection agent is used when the object should have different access rights. For example, in the C h o i c e s operating system [c I R M 9 3] k e m e l P R o x I e s provides access protection for operating system objects. 4) Smart Reference replaces a simple pointer that performs some additional operations when accessing an object. Typical uses of it include:
- A reference count that points to the actual object, so that when the object has no references, it can be automatically freed (also known as S M a r tP o i n T e R s[E d e 9 2]).
- When a persistent object is referenced for the first time, it is loaded into memory.
- Before accessing an actual object, check to see if it has been locked to ensure that other objects cannot change it.
|
1 usingSystem;2 usingSystem.Threading;3 4 /// <summary>5 ///Summary description for Client.6 /// </summary>7 Abstract classCommonsubject8 {9 Abstract Public voidRequest (); Ten } One A classActualsubject:commonsubject - { - PublicActualsubject () the { - //assume constructor here does some operation that takes quite a - //while-hence the need for a proxy-to delay incurring this - //delay until (and if) the actual subject is needed +Console.WriteLine ("starting to construct Actualsubject"); -Thread.Sleep ( +);//represents lots of processing! +Console.WriteLine ("finished constructing Actualsubject"); A } at - Override Public voidRequest () - { -Console.WriteLine ("executing request in Actualsubject"); - } - } in - classProxy:commonsubject to { + Actualsubject Actualsubject; - the Override Public voidRequest () * { $ if(Actualsubject = =NULL)Panax NotoginsengActualsubject =NewActualsubject (); - actualsubject.request (); the } + A } the + Public classClient - { $ Public Static intMain (string[] args) $ { -Proxy p =NewProxy (); - the //Perform Actions here - // . . . Wuyi the if(1==1)//At Some later point, based on a condition, -P.request ();//we determine if we need to use subject Wu - return 0; About } $}
Proxy Mode
Proxy mode of structural design pattern