标签:blog 使用 ar 文件 2014 c on log r
1.概述2. 类应设计的对扩展开放,对修改关闭。
实例:
<pre name="code" class="csharp"> // 装饰者模式抽象类 public abstract class BaseDecorator { protected ReturnInfo returnInfo = new ReturnInfo() { Code = 1, IsSuccess = false }; public BaseDecorator decorator = null; //装饰者模式必须要传入上一个装饰者对象,如果没有则传null public BaseDecorator(BaseDecorator _decorator) { decorator = _decorator; } // 执行方法 public abstract ReturnInfo Run(string orderId,string supplierId,string supOrderId); // 返回结果 protected ReturnInfo GetResult() { returnInfo.Code = 0; returnInfo.IsSuccess = true; returnInfo.Message = "操作成功"; return returnInfo; } }
public class DecoratorA:BaseDecorator { public DecoratorA(BaseDecorator decorator) : base(decorator) { } public override Model.ReturnInfo Run(string orderId, string supplierId, string supOrderId) { //具体执行的方法 //判断是否还有装饰者需要执行 if (decorator!=null) { return decorator.Run(orderId, supplierId,supOrderId); } else { return GetResult(); } } }
public class DecoratorB:BaseDecorator { public DecoratorB(BaseDecorator decorator) : base(decorator) { } public override Model.ReturnInfo Run(string orderId, string supplierId, string supOrderId) { //具体执行的方法 //判断是否还有装饰者需要执行 if (decorator!=null) { return decorator.Run(orderId, supplierId,supOrderId); } else { return GetResult(); } } }
public class Test { BaseDecorator decorator = new DecoratorA(null); decorator = new DecoratorB(decorator); //程序是从下往上执行的 return decorator.Run(orderId,supplierId,supOrderId); }
标签:blog 使用 ar 文件 2014 c on log r
原文地址:http://blog.csdn.net/zzr494684778/article/details/39671399