1.1 Definitions
Provides a proxy for other objects to control access to this object.
In fact, two classes implement an interface or an abstract class, instantiate Class B objects in Class A, then class A will be able to proxy class B to implement the interface method.
1.2 Class Diagram
An instance of the Realsubject class is declared in the Porxy proxy class, invoking the method of the Realsubject instance when it is necessary to invoke a method that they all implement. The agent for Realsubject is implemented.
1.3 Code
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceProxy Mode8 {9 Abstract classSubjectTen { One Public Abstract voidRequest (); A } - - classRealsubject:subject the { - Public Override voidRequest () - { -Console.WriteLine ("a real request"); + } - } + A classProxy:subject at { - Realsubject rs; - - - Public Override voidRequest () - { in if(rs = =NULL) - { tors =NewRealsubject (); + } - Rs. Request (); the } * } $ Panax Notoginseng -}
View Code
Call:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceProxy Mode8 {9 class ProgramTen { One Static voidMain (string[] args) A { -Subject s =NewProxy (); - s.request (); the - } - } -}
View Code
1.4 Summary
4 major scenarios for proxy mode:
1, the remote agent, for an object in different address space to provide local representation.
2, virtual agent, as needed to create a large cost of objects. The picture in the browser is downloaded, which is used to optimize it.
3, the security agent, control the real access to the object's permissions.
4, can only reference, call the real object is the agent can help us do extra things, such as record the number of citations, release objects and so on.
Design mode Note 5: Proxy mode