文章分类 -  Design Pattern

摘要:状态模式: 当一个对象的状态改变时,允许其行为也进行相应的变化。示例代码:using System;usingSystem.Text;usingSystem.IO;usingSystem.Collections.Generic;namespace Hello{ //GumballMachine class public class GumballMachine { State soldOutState; State noQuarterState; State hasQuarterState; State soldState; State state; int count = 0; public 阅读全文
posted @ 2011-10-07 19:50 Erebus_NET 阅读(162) 评论(0) 推荐(0)
摘要:组合模式: 将对象组织成树形结构的形式来表现部分和整体的继承关系。组合模式让客户将独立的叶节点对象和枝节点对象进行统一处理。示例代码:using System;usingSystem.Text;usingSystem.IO;usingSystem.Collections.Generic;namespace Hello{ //self-define Exception public class UnsopportedOperationException : ApplicationException { } //Node abstract class public abstract class.. 阅读全文
posted @ 2011-10-06 12:26 Erebus_NET 阅读(123) 评论(0) 推荐(0)
摘要:设计模式原则: 类只有唯一的理由去改变。迭代器模式: 提供一种访问顺序存放元素的一种访问方式,使得我们使用这些元素的时候无需知道其底层的具体实现。示例代码:using System;usingSystem.Text;usingSystem.IO;namespace Hello{ //Iterator Interface public interface Iterator { bool HasNext(); object Next(); } //DinerMenuIterator public class DinerMenuIterator : Iterator { string[] items 阅读全文
posted @ 2011-10-05 13:24 Erebus_NET 阅读(145) 评论(0) 推荐(0)
摘要:模板模式: 定义一个算法的几本框架和操作步骤,在其派生类中具体实现算法实现步骤的细节,派生类不改变算法的结构。using System;usingSystem.Text;usingSystem.IO;namespace Hello{ public abstract class CaffeineBeverageWithHook { public voidPrepareRecipe() { BoilWater(); Brew(); PourInCup(); if (CustomerWantsCondiments()) { AddCondiments(); } } public abstract v 阅读全文
posted @ 2011-10-04 22:24 Erebus_NET 阅读(116) 评论(0) 推荐(0)
摘要:适配器模式: 将一个类的接口转换成另一个客户希望的接口。适配器使得因为接口不同的类可以同时工作。示例代码:using System;usingSystem.Text;usingSystem.IO;namespace Hello{ //Duck Interface public interface Duck { void quake(); void fly(); } //Turkey Interface public interface Turkey { void gobble(); void fly(); } //WildTurkey Class public class WildTurkey 阅读全文
posted @ 2011-10-03 12:39 Erebus_NET 阅读(121) 评论(0) 推荐(0)
摘要:命令模式:将请求封装为对象,使得客户可以将不同的请求当做参数使用,同时可以支持Undo操作。示例代码:usingSystem;usingSystem.Text;usingSystem.IO;namespaceHello{//Interface CommandpublicinterfaceCommand{voidExecute();voidUndo();}//No CommandpublicclassNoCommand:Command{publicvoidExecute(){}publicvoidUndo(){}}//Light ClasspublicclassLight{publicvoidO 阅读全文
posted @ 2011-10-01 13:42 Erebus_NET 阅读(217) 评论(0) 推荐(0)
摘要:单件模式: 确保一个类只能创建一个实例,在全局中只有唯一的访问点。示例代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Security.Cryptography;namespace Hello{ class Singleton { private int number = 0; private static SingletonuniqueInstance; private Singleton() { } publ.. 阅读全文
posted @ 2011-09-30 09:11 Erebus_NET 阅读(126) 评论(0) 推荐(0)
摘要:设计模式原则: 依赖于抽象进行设计,而不是具体的实现。工厂模式: 定义创建对象的接口,让其派生类去决定该创建什么类型的实例。示例代码:using System;usingSystem.Collections.Generic;using System.Linq;using System.Text;using System.IO;usingSystem.Security.Cryptography;namespace Hello{ //Pizza publicabstract class Pizza { protectedstring name; protectedstring dough; ... 阅读全文
posted @ 2011-09-28 10:11 Erebus_NET 阅读(137) 评论(0) 推荐(0)
摘要:装饰模式(3)设计模式原则: 对于类,我们要做到易于扩展、修改闭合。装饰模式: 为对象动态添加属性,提供一种可以替代继承来扩展类功能的方法。示例代码:using System;usingSystem.Collections.Generic;using System.Linq;using System.Text;using System.IO;usingSystem.Security.Cryptography;namespace Hello{ //饮料抽象类 publicabstract class Beverage { protectedstring description = "U 阅读全文
posted @ 2011-09-25 19:05 Erebus_NET 阅读(79) 评论(0) 推荐(0)
摘要:设计模式原则:努力使模块间为松散耦合观察者模式:建立对象之间一种一对多的依赖关系,使得一个对象的属性改变,其它依赖对象能够自动被通知以及更新。示例代码:using System;usingSystem.Collections.Generic;using System.Linq;using System.Text;using System.IO;usingSystem.Security.Cryptography;namespace Hello{ //interface interfaceSubject { voidRegisterObserver(Observer user); voidRem. 阅读全文
posted @ 2011-09-19 18:44 Erebus_NET 阅读(121) 评论(0) 推荐(0)
摘要:设计模式原则:1. 变化的部分进行封装2. 组合>继承3. 面向接口编程而不是面向实现编程策略模式: 定义一系列算法并将其封装,使其具有通用的调用方法。策略模式让算法的变化与客户端代码使用它的方法相互独立。示例代码:namespace Hello{ //主板抽象类,不变的代码 public abstract class MainBoard { public CPU cpu; public GPU gpu; public MainBoard() {} public void RunCPU() { ... 阅读全文
posted @ 2011-09-13 12:43 Erebus_NET 阅读(149) 评论(0) 推荐(0)