Chain of Responsibility模式就是当外部请求程序进行某个处理,但程序暂时无法直接决定由哪个对象负责处理时,就需要推卸责任.也就是说,当一个人被要求做什么事时,如果他可以做就自己做,如果不能做就转给下一个人,以此类推. 下面是示例程序的类图. 下面是示例程序代码. package bigjunoba.bjtu.handler; public class Trouble { private int number; public Trouble(int number) { this…
模式角色与结构: 示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.VisitorPattern { class Program { static void Main(string[] args) { ObjectStructure list = new ObjectStructure(); list.…
模板方法模式:定义一个操作中算法的框架,而将一些步骤延迟到子类中.模板方法模式使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 模式角色与结构: 实现代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.TemplateMethodPattern { class Program { static…
策略模式(Strategy Pattern):定义一系列算法类,将每一个算法封装起来,并让它们可以相互替换,策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy). 模式角色与结构: 示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.StrategyPattern { class Pro…
模式角色与结构: 示例代码:(本示例在具体状态类中实现状态切换,也可以在环境类中实现状态切换.状态模式一定程度上违背开闭原则) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.StatePattern { class Program { static void Main(string[] args) { Acco…
迭代器模式(Iterator Pattern):提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示,其别名为游标(Cursor). 模式角色与结构: 实现代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.IteratorPattern { class Program { static void Mai…
命令模式(Command Pattern):将一个请求封装为一个对象,从而让我们可用不同的请求对客户进行参数化:对请求排队或者记录请求日志,以及支持可撤销的操作.命令模式是一种对象行为型模式,其别名为动作(Action)模式或事务(Transaction)模式. 模式角色与结构:(可以有多个ConcreteCommand,分别保存多个Receiver的引用) 示例代码: using System; using System.Collections.Generic; using System.Li…
代理模式:给某一个对象提供一个代理或占位符,并由代理对象来控制对原对象的访问. 模式角色与结构: 示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.ProxyPattern { class Program { static void Main(string[] args) { Subject subje…
定义: 外观模式:为子系统中的一组接口提供一个统一的入口.外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 模式角色与结构: 示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.FacadePattern { class Program { static void Main(string[…
组合模式(Composite Pattern): 组合多个对象形成树形结构以表示具有“整体—部分”关系的层次结构.组合模式对单个对象(即叶子对象)和组合对象(即容器对象)的使用具有一致性,组合模式又可以称为“整体—部分”(Part-Whole)模式. 角色模式与结构: 组合模式的关键是定义了一个抽象构件类,它既可以代表叶子,又可以代表容器,而客户端针对该抽象构件类进行编程,无须知道它到底表示的是叶子还是容器,可以对其进行统一处理. 示例代码: using System; using System…
桥接模式(Bridge Pattern): 将抽象部分与它的实现部分分离,使它们都可以独立地变化.它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式. 模式角色与结构: 示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.BridgePattern {…
建造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 模式角色与模式: 建造者模式可以将一个产品的内部表象与产品的生产过程分割开来,从而可以是一个建造过程生成具有不同的内部表象的产品对象. 示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPatter…
Chain of Responsibility定义:Chain of Responsibility(CoR) 是用一系列类(classes)试图处理一个请求request,这些类之间是一个松散的耦合,唯一共同点是在他们之间传递request.也就是说,来了一个请求,A类先处理,如果没有处理,就传递到B类处理,如果没有处理,就传递到C类处理,就这样象一个链条(chain)一样传递下去. 如何使用责任链模式 虽然这一段是如何使用CoR,但是也是演示什么是CoR. 有一个Handler接口: publ…
熟悉VC/MFC的都知道,VC是“基于消息,事件驱动”,消息在VC开发中起着举足轻重的作用.MFC提供了消息的处理的链式处理策略,处理消息的请求将沿着预定好的路径依次进行处理.消息的发送者并不知道该消息最后是由哪个具体的对象处理的,当然它也无需也不想知道,但是结果是该消息被某个对象处理了,或者一直到一个终极的对象进行处理了. Chain of Responsibility模式,擅长解决的其实就是这样一类问题:将可能处理一个请求的对象链接成一个链,并将请求在这个链上传递,直到有对象处理该请求(可能…
职责链模式(Chain of Responsibility Pattern):避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止. 模式角色与结构: 示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.Responsibil…