C# 设计模式之 职责链模式
2012-03-02 12:22
671 查看
每个职责类包含职责类对象,如果自己处理不了,交给职责类处理
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DesignPytternDemo { public abstract class DataTypeHandler { protected DataTypeHandler _handler; public void SetHandler(DataTypeHandler handler) { this._handler = handler; } public abstract void Handle(object data); } public class IntHandler : DataTypeHandler { public override void Handle(object data) { if (data is int) { Console.WriteLine("int handler handle it!"); } else { if (this._handler != null) { this._handler.Handle(data); } } } } public class BoolHandler : DataTypeHandler { public override void Handle(object data) { if (data is bool) { Console.WriteLine("BoolHandler handler handle it!"); } else { if (this._handler != null) { this._handler.Handle(data); } } } } public class DoubleHandler : DataTypeHandler { public override void Handle(object data) { if (data is double) { Console.WriteLine("double handler handle it!"); } else { if (this._handler != null) { this._handler.Handle(data); } } } } } DataTypeHandler bh1 = new IntHandler(); DataTypeHandler bh2 = new BoolHandler(); DataTypeHandler bh3 = new DoubleHandler(); bh1.SetHandler(bh2); bh2.SetHandler(bh3); int a = 1; bool b = true; double c = 1.2; bh1.Handle(a); bh1.Handle(b); bh1.Handle(c);
相关文章推荐
- C# 设计模式-职责链(Chain of Responsibility)
- C#设计模式——职责链模式(Chain Of Responsibility Pattern)
- C#设计模式之14——职责链模式
- [导入]C#面向对象设计模式纵横谈(20):(行为型模式) Chain Of Responsibility 职责链模式.zip(8.18 MB)
- C#设计模式之职责链
- C#设计模式系列:职责链模式(Chain of Responsibility)
- C#设计模式之二十职责链模式(Chain of Responsibility Pattern)【行为型】
- [导入]C#面向对象设计模式纵横谈(14):Chain of Responsibility 职责链模式(行为型模式).zip(9.54 MB)
- C#设计模式之职责链
- C#设计模式之二十职责链模式(Chain of Responsibility Pattern)【行为型】
- C#设计模式之ChainOfResponsibility职责链模式解决真假美猴王问题实例
- C#设计模式——职责链模式(Chain Of Responsibility Pattern)
- C#设计模式(23种设计模式)
- C#设计模式--工厂方法
- 《设计模式--基于C#的工程化实现及扩展》 Security Design Pattern 系列 1 公钥体系与分布式环境要求
- 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)
- 乐在其中设计模式(C#) - 建造者模式(Builder Pattern)
- HeadFirst 设计模式一及其c#实现
- 设计模式六大原则---单一职责原则
- C#面向对象模式设计第二十讲:Chain Responsibility 职责链模式(行为型模式)