Design Mode Study Note 12-proxy Mode

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.