与装饰器模式类似,只继承接口;
示例代码:
interface IStudent
{
public void GetInfo();
}
class Student : IStudent
{
public void GetInfo()
{
Console.WriteLine("上小学中。。。");
}
}
class ProxyStudent : IStudent
{
private IStudent _student = new Student();
public void GetInfo()
{
Console.WriteLine("小学之前4岁!");
_student.GetInfo();
Console.WriteLine("小学之后10岁!");
}
}
class Pro
{
public static void Main()
{
IStudent student = new Student();
Console.WriteLine("-------------通常----------------");
student.GetInfo();
Console.WriteLine("-----------代理模式--------------");
ProxyStudent proxyStudent = new ProxyStudent();
proxyStudent.GetInfo();
}
}