Proxy (proxy mode - structural mode)

Direct and indirect
People often have a way to deal with complex software systems, which is to add a layer of indirection, so as to obtain a more flexible solution to the system that meets specific needs.
insert image description here
Agent Mode
Motivation

insert image description here
Intent
insert image description here
Structure
insert image description here
Code
Version1

//version1
   //由于集中控制需求,放在集中公司总部机器上
   class Employee
   {
    
    
      public void GetSalary()
      {
    
    
      }

      public void Report()
      {
    
    
      }

      public void ApplyVacation()
      {
    
    
      }
   }
//可能应用于各个地区机器上
   class HrSystem
   {
    
    
      public void Process()
      {
    
    
         //此时这样不可用,这样使用原则是Employee类一定与HrSystem在同一地址空间
         Employee employee = new Employee();
         employee.Report();
         //...
         employee.ApplyVacation();
         //...
      }
   }

Solve
Version2

//version2
    //由于集中控制需求,放在集中公司总部机器上
    interface  IEmployee
    {
    
    
        void GetSalary();

        void Report();

        void ApplyVacation();
    }
    
    //运行在Internet远端一台机器上
    class Employee:IEmployee
    {
    
    
        public void GetSalary()
        {
    
    
        }

        public void Report()
        {
    
    
        }

        public void ApplyVacation()
        {
    
    
        }
    }

 //EmployeeProxy和HrSystem是运行在同一个地址空间
    //运行在本地Windows Form
    class EmployeeProxy : IEmployee
    {
    
    
        public EmployeeProxy()//构造器
        {
    
    
            //对对象创建的一种SOAP封装
        }

        public void GetSalary()
        {
    
    
            //对访问对象的一种SOPA封装
            //发送SOAP数据,到远程机器上。(监听来调用Employee)
            //如果有返回值,接收返回值SOAP,解包,返回原生(RAW)的C#数据
        }

        public void Report()
        {
    
    
            //...
        }

        public void ApplyVacation()
        {
    
    
            //...
        }
    }    
//可能应用于各个地区机器上
    class HrSystem
    {
    
    
        public void Process()
        {
    
    
            IEmployee employee = new EmployeeProxy();
            
            employee.Report();
            employee.ApplyVacation();
        }
    }

Key points
insert image description here
C# does not support operator overloading, use the interface to use the proxy
Copy-on-Write to achieve
1. Most string implementation methods
insert image description here

string s1 = "hello";
string s2 = "hello";
//ToUpper()  转化为大写字母
//s1.ToUpper();//不能改变S1

s1=s1.ToUpper();//这样才可以改变s1

2. Copy-on-Write implementation
insert image description here

StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = new StringBuilder("hello");

sb1.Replace("l", "b");//sb1改变,将sb1中l改为b

Note: When there is only one StringBuilder, it is no longer copied and directly modified in place.
When the string is not prone to modification, using String Builder is more memory-intensive than using String.

Guess you like

Origin blog.csdn.net/weixin_51565051/article/details/131801464