摘要: 设计模式原则: 类只有唯一的理由去改变。迭代器模式: 提供一种访问顺序存放元素的一种访问方式,使得我们使用这些元素的时候无需知道其底层的具体实现。示例代码: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 阅读(115) 评论(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 阅读(91) 评论(0) 推荐(0) 编辑
摘要: 将对象序列化可以方便网络进行对象的传输,然后在另一个程序中将序列化的对象反序列化。通常在以下情况使用序列化可以提高效率:1.将一个组件存入文件,当需要时载入;2.如果想将软件生命周期才存在的对保存,需要进行序列化;3.需要将对象进行网络传输时;4.保存对象后要按照对象原来的形式重建对象。对象的序列化有两种形式:一种是xml形式,还有一种是二进制形式。序列化为xml形式示例:using System;usingSystem.Text;usingSystem.IO;usingSystem.Xml.Serialization;namespace Hello{ public class Custome 阅读全文
posted @ 2011-10-03 14:16 Erebus_NET 阅读(454) 评论(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 阅读(97) 评论(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 阅读(196) 评论(0) 推荐(0) 编辑
摘要: EncodingEncoding类在System.Text名字空间中。using System;usingSystem.Text;usingSystem.IO;namespace Hello{ class Program { static void Main(string[] args) { string test = "Thisis our test string."; byte[] ascb; byte[] unicb; byte[] utfb; ascb = Encoding.ASCII.GetBytes(test); Console.WriteLine(" 阅读全文
posted @ 2011-09-30 20:39 Erebus_NET 阅读(214) 评论(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 阅读(102) 评论(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 阅读(104) 评论(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 阅读(67) 评论(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 阅读(104) 评论(0) 推荐(0) 编辑