代理模式与装饰者模式很像,起到一定的解耦作用
UML图:
示例代码:
public abstract class Person { public abstract void Buy(); }
public class RealBuyUser:Person { public override void Buy() { Console.WriteLine("我要买盒烟"); } }
public class Friend:Person { private RealBuyUser realBuyUser; public Friend(RealBuyUser realUser) { this.realBuyUser = realUser; } public override void Buy() { PreBuy(); realBuyUser.Buy(); } public void PreBuy() { Console.WriteLine("帮别人买之前我要佣金"); } }
class Program { static void Main(string[] args) { Friend friend =new Friend(new RealBuyUser()); friend.Buy(); } }