Motivation : In an object-oriented system, some objects are subject to some reasons (for example, the overhead of object creation is large, some operations require security control, or external access is required ), direct access may cause a lot of trouble to the user or system structure. How can we manage/control the unique complexity of these objects without losing transparent operation objects? Adding an indirect layer is a common solution in software development.
Application: Remote Object Access, copy-on-write
Scenario: To enable the ThinkPad operation on the ApplicationProgramTransparent, isolates the complexity of hardware operations, and places a proxy layer (equivalent to the operating system) between the application and ThinkPad. This proxy provides consistent APIs to the application, hardware operations.
Structure
CodeImplementation
Namespace Designpattern. Proxy
{
Public Interface Ithinkpad
{
VoidStart ();
VoidOperate ();
VoidShutdown ();
}
/**/ /// <Summary>
///Entity for ThinkPad operations
/// </Summary>
Public Class ThinkPad: ithinkpad
{
Public Void Start ()
{
}
Public Void Operate ()
{
}
Public Void Shutdown ()
{
}
}
/**/ /// <Summary>
///Proxy, which can be called by applications to perform ThinkPad operations
/// </Summary>
Public Class Thinkpadproxy: ithinkpad
{
Ithinkpad ThinkPad;
Public Thinkpadproxy (ithinkpad ThinkPad)
{
This. ThinkPad=ThinkPad;
}
Public Void Start ()
{
//.
ThinkPad. Start ();
//.
}
Public Void Operate ()
{
//.
ThinkPad. Operate ();
//.
}
Public Void Shutdown ()
{
//.
ThinkPad. Shutdown ();
//.
}
}
}
Namespace Designpattern. Proxy
{
Public Class Application
{
Ithinkpad ThinkPad = New Thinkpadproxy ( New ThinkPad ());
Public Void Startthinkpad ()
{
ThinkPad. Start ();
}
Public Void Operatethinkpad ()
{
ThinkPad. Operate ();
}
Public Void Shutdownthinkpad ()
{
ThinkPad. Shutdown ();
}
}
}
Key Points:
1. In an object-oriented system, using some objects directly brings about many problems. Acting as a proxy object at the indirect layer is a common method to solve this problem.
2. When using this mode, the implementation methods and granularity vary greatly. Some may perform fine-grained control on a single object, such as copy-on-write technology. Some may provide an abstract proxy layer for the component module and proxy the object at the architecture level.
3. This mode does not require consistency of interfaces. As long as indirect control is implemented, it is acceptable to lose some transparency.