c# Design Pattern-Proxy Pattern of Structural Pattern

Preface        

        For some reason it is necessary to provide a proxy to an object to control access to the object. At this time, the access object is not suitable or cannot directly reference the target object, and the proxy object acts as an intermediary between the access object and the target object. When learning the proxy mode, you can learn about Aop aspect programming AOP aspect programming_aop programming_Ahao who loves bananas' blog-CSDN blog . The underlying implementation of Aop usually uses the proxy mode

The Proxy mode is divided into three roles:

  1. Abstract role (Subject) class: declares the business methods implemented by real roles and proxy objects through interfaces or abstract classes.
  2. Real Subject class: implements the specific business in the abstract role, is the real object represented by the proxy object, and is the object to be ultimately referenced.
  3. Proxy class: Provides the same interface as the real role, which contains a reference to the real role. It can access, control or extend the functions of the real role.

Example

Let's assume a scenario. There is a calculation method in the computer. Pass in two numbers and get a total. But now you need to add an integer calculator to remove the decimal part of the decimal passed in and only calculate the integer part. But originally The sum method may need to be used in other places, so the original method cannot be modified. We use the proxy mode to implement this function.

abstract character

Create an abstract character computer with a calculation method inside it

public interface IComputer
{
    void calculator(double a, double b);
}

real character

Implement class sum to add the two numbers passed in.

public class sum : IComputer
{
    public void calculator(double a, double b)
    {
        Console.WriteLine(a + b);
    }
}

Proxy class ProxySum

In the agent class, the real character sum is directly referenced, but before that, the parameters are decimalized.

public class ProxySum : IComputer
{
    private sum sum = new sum();

    public void calculator(double a, double b)
    {
        a = Math.Floor(a);
        b = Math.Floor(b);
        sum.calculator(a, b);
    }
}

test

First use the original real character sum and pass in two decimals

    public static void Main(string[] args)
    {
        var sum = new sum();
        sum.calculator(1.111,3.999);
    }

Got result 5.11

 

 Then we use the proxy class proxySum

    public static void Main(string[] args)
    {
        var proxySum = new ProxySum();
        proxySum.calculator(1.111,3.999);
    }

 The result obtained is the result of adding after removing decimals.

 Summarize

advantage:

The proxy mode plays an intermediary role between the client and the target object and protects the target object;

The proxy object can extend the functions of the target object; the proxy mode can separate the client from the target object, reducing the coupling of the system to a certain extent;

Disadvantages: Increases system complexity;

 

Guess you like

Origin blog.csdn.net/weixin_65243968/article/details/132225788