前言
代理模式通过引入一个中间代理对象去控制对原始对象的访问,并在访问过程中进行控制或增强。同时保持客户与原始对象之间的解耦。
代码
csharp
//主题
public abstract class Subject
{
public abstract void Request();
}
//实际主题
public class RealSubject : Subject
{
public override void Request()
{
Console.WriteLine("real request address: 10.110.119.120");
}
}
// 代理主题
public class Proxy : Subject
{
private RealSubject _subject;
public Proxy()
{
_subject = new RealSubject();
}
public override void Request()
{
Console.WriteLine("init request config....");
_subject.Request();
Console.WriteLine("use proxy realm name request :https://www.baidu.com");
}
}
/*
* 结构型模式:Structural Pattern
* 代理模式:Proxy Pattern
*/
internal class Program
{
static void Main(string[] args)
{
Proxy proxy = new Proxy();
proxy.Request();
Console.ReadLine();
}
}