为了方便阅读,我把一篇设计模式的资料拆分开发,分为三个大的部分,如下:
名称 | Factory Method |
结构 | |
意图 | 定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。 |
适用性 |
|
Code Example | namespace FactoryMethod_DesignPattern // These two classes could be part of a framework, // =============================================== abstract public void CreateDocument(); public void ConstructObjects() // These two classes could be part of an application // ================================================= class MyApplication : DPApplication override public void CreateDocument() Console.WriteLine("MyApplication exists"); /// Summary description for Client. public static int Main(string[] args) MyApplication myApplication = new MyApplication(); |
名称 | Abstract Factory |
结构 | |
意图 | 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 |
适用性 |
|
Code Example | namespace AbstractFactory_DesignPattern { using System; // These classes could be part of a framework, // which we will call DP // =========================================== abstract class DPDocument { abstract public void Dump(); } abstract class DPWorkspace { abstract public void Dump(); } abstract class DPView { abstract public void Dump(); } abstract class DPFactory { abstract public DPDocument CreateDocument(); abstract public DPView CreateView(); abstract public DPWorkspace CreateWorkspace(); } abstract class DPApplication { protected DPDocument doc; protected DPWorkspace workspace; protected DPView view; public void ConstructObjects(DPFactory factory) { // Create objects as needed doc = factory.CreateDocument(); workspace = factory.CreateWorkspace(); view = factory.CreateView(); } abstract public void Dump(); public void DumpState() { if (doc != null) doc.Dump(); if (workspace != null) workspace.Dump(); if (view != null) view.Dump(); } } // These classes could be part of an application class MyApplication : DPApplication { MyFactory myFactory = new MyFactory(); override public void Dump() { Console.WriteLine("MyApplication exists"); } public void CreateFamily() { MyFactory myFactory = new MyFactory(); ConstructObjects(myFactory); } } class MyDocument : DPDocument { public MyDocument() { Console.WriteLine("in MyDocument constructor"); } override public void Dump() { Console.WriteLine("MyDocument exists"); } } class MyWorkspace : DPWorkspace { override public void Dump() { Console.WriteLine("MyWorkspace exists"); } } class MyView : DPView { override public void Dump() { Console.WriteLine("MyView exists"); } } class MyFactory : DPFactory { override public DPDocument CreateDocument() { return new MyDocument(); } override public DPWorkspace CreateWorkspace() { return new MyWorkspace(); } override public DPView CreateView() { return new MyView(); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { public static int Main(string[] args) { MyApplication myApplication = new MyApplication(); myApplication.CreateFamily(); myApplication.DumpState(); return 0; } } } |
名称 | Builder |
结构 | |
意图 | 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。 |
适用性 |
|
Code Example | namespace Builder_DesignPattern { using System; // These two classes could be part of a framework, // which we will call DP // =============================================== class Director { public void Construct(AbstractBuilder abstractBuilder) { abstractBuilder.BuildPartA(); if (1==1 ) //represents some local decision inside director { abstractBuilder.BuildPartB(); } abstractBuilder.BuildPartC(); } } abstract class AbstractBuilder { abstract public void BuildPartA(); abstract public void BuildPartB(); abstract public void BuildPartC(); } // These two classes could be part of an application // ================================================= class ConcreteBuilder : AbstractBuilder { override public void BuildPartA() { // Create some object here known to ConcreteBuilder Console.WriteLine("ConcreteBuilder.BuildPartA called"); } override public void BuildPartB() { // Create some object here known to ConcreteBuilder Console.WriteLine("ConcreteBuilder.BuildPartB called"); } override public void BuildPartC() { // Create some object here known to ConcreteBuilder Console.WriteLine("ConcreteBuilder.BuildPartC called"); } } /// <summary> /// Summary description for Client. /// </summary> public class Client { public static int Main(string[] args) { ConcreteBuilder concreteBuilder = new ConcreteBuilder(); Director director = new Director(); director.Construct(concreteBuilder); return 0; } } } |
名称 | Prototype |
结构 | |
意图 | 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。 |
适用性 |
|
Code Example | namespace Prototype_DesignPattern { using System; // Objects which are to work as prototypes must be based on classes which // are derived from the abstract prototype class abstract class AbstractPrototype { abstract public AbstractPrototype CloneYourself(); } // This is a sample object class MyPrototype : AbstractPrototype { override public AbstractPrototype CloneYourself() { return ((AbstractPrototype)MemberwiseClone()); } // lots of other functions go here! } // This is the client piece of code which instantiate objects // based on a prototype. class Demo { private AbstractPrototype internalPrototype; public void SetPrototype(AbstractPrototype thePrototype) { internalPrototype = thePrototype; } public void SomeImportantOperation() { // During Some important operation, imagine we need // to instantiate an object - but we do not know which. We use // the predefined prototype object, and ask it to clone itself. AbstractPrototype x; x = internalPrototype.CloneYourself(); // now we have two instances of the class which as as a prototype } } /// <summary> /// Summary description for Client. /// </summary> public class Client { public static int Main(string[] args) { Demo demo = new Demo(); MyPrototype clientPrototype = new MyPrototype(); demo.SetPrototype(clientPrototype); demo.SomeImportantOperation(); return 0; } } } |
名称 | Singleton |
结构 | |
意图 | 保证一个类仅有一个实例,并提供一个访问它的全局访问点。 |
适用性 |
|
Code Example | namespace Singleton_DesignPattern { using System; class Singleton { private static Singleton _instance;
public static Singleton Instance() { if (_instance == null) _instance = new Singleton(); return _instance; } protected Singleton(){} // Just to prove only a single instance exists private int x = 0; public void SetX(int newVal) {x = newVal;} public int GetX(){return x;} } /// <summary> /// Summary description for Client. /// </summary> public class Client { public static int Main(string[] args) { int val; // can't call new, because constructor is protected Singleton FirstSingleton = Singleton.Instance(); Singleton SecondSingleton = Singleton.Instance(); // Now we have two variables, but both should refer to the same object // Let's prove this, by setting a value using one variable, and // (hopefully!) retrieving the same value using the second variable FirstSingleton.SetX(4); Console.WriteLine("Using first variable for singleton, set x to 4"); val = SecondSingleton.GetX(); Console.WriteLine("Using second variable for singleton, value retrieved = {0}", val); return 0; } } } |