设计模式 代理模式
先写一个接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AgentPattern
{
interface GiveGift
{
void GiveDolls();
void GiveFlowers();
void GiveChocolate();
}
}
在写实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AgentPattern
{
class Pursuit : GiveGift
{
string mm;
public Pursuit(string mm)
{
this.mm = mm;
}
public void GiveChocolate()
{
Console.WriteLine(mm + " 送你巧克力\n");
}
public void GiveDolls()
{
Console.WriteLine(mm + " 送你洋娃娃\n");
}
public void GiveFlowers()
{
Console.WriteLine(mm + " 送你花\n");
}
}
}
撰写代理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AgentPattern
{
class Proxy : GiveGift
{
Pursuit gg;
public Proxy(string mm)
{
gg = new Pursuit(mm);
}
public void GiveChocolate()
{
gg.GiveChocolate();
}
public void GiveDolls()
{
gg.GiveDolls();
}
public void GiveFlowers()
{
gg.GiveChocolate();
}
}
}
主函数测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AgentPattern
{
class Program
{
static void Main(string[] args)
{
string jiaojiao = "jiaojiao";
Proxy daili = new Proxy(jiaojiao);
daili.GiveDolls();
daili.GiveFlowers();
daili.GiveChocolate();
Console.Read();
}
}
}