`
feiliboos
  • 浏览: 665597 次
文章分类
社区版块
存档分类
最新评论

c# 设计模式(1)一 创建型

 
阅读更多

为了方便阅读,我把一篇设计模式的资料拆分开发,分为三个大的部分,如下:

名称

Factory Method

结构

意图

定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。

适用性

  • 当一个类不知道它所必须创建的对象的类的时候。
  • 当一个类希望由它的子类来指定它所创建的对象的时候。
  • 当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。

Code Example

namespace FactoryMethod_DesignPattern

{

using System;

// These two classes could be part of a framework,

// which we will call DP

// ===============================================

class DPDocument

{

}

abstract class DPApplication

{

protected DPDocument doc;

abstract public void CreateDocument();

public void ConstructObjects()

{

// Create objects as needed

// . . .

// including document

CreateDocument();

}

abstract public void Dump();

}

// These two classes could be part of an application

// =================================================

class MyApplication : DPApplication

{

override public void CreateDocument()

{

doc = new MyDocument();

}

override public void Dump()

{

Console.WriteLine("MyApplication exists");

}

}

class MyDocument : DPDocument

{

}

/// <summary>

/// Summary description for Client.

/// </summary>

public class Client

{

public static int Main(string[] args)

{

MyApplication myApplication = new MyApplication();

myApplication.ConstructObjects();

myApplication.Dump();

return 0;

}

}

}

名称

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;

}

}

}

分享到:
评论

相关推荐

    C#面向对象设计模式纵横谈-1.Singleton 单件(创建型模式)

    C#面向对象设计模式纵横谈 第二课 Singleton 单件(创建型模式)

    C#设计模式

    《C#设计模式》由James W. Cooper编著,主要介绍如何用最常见的设计模式编写C#程序。全书分为4个部分,首先介绍了C#语言和面向对象程序设计的一般原则,可作为C#程序设计的快速入门教程;然后分别讲述了创建型模式、...

    C#面向对象设计模式纵横谈-创建型模式

    C#面向对象设计模式纵横谈-创建型模式

    C#设计模式随书光盘

    本书主要介绍如何用最常见的设计模式编写C#程序。全书分为四个部分,首先介绍了C#语言和面向对象程序设计的一般原则,可作为C#程序设计的快速入门教程;然后分别讲述了创建型模式、结构型模式和行为型模式。每一类...

    C#面向对象设计模式纵横谈(视频与源码)

    C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式) C#面向对象设计模式纵横谈(3):Abstract Factory 抽象工厂模式(创建型模式) C#面向对象设计模式纵横谈(4):Builder 生成器模式(创建型模式) C#面向...

    C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式)

    C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式)

    C#设计模式(23种设计模式)

    C#设计模式(23种设计模式) 部分内容概述如下,下载可看全部哦!!! 还等什么呢?? 创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式...

    C#设计模式_设计模式_C#_

    创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype)结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接...

    C#23种设计模式样例代码和UML图

    C#23种设计模式样例代码和UML图等 创建型模式(抽象工厂模式、工厂方法模式、单例模式、建造者模式、原型模式); 行为型模式(策略模式、 迭代器模式、原型模式、职责链模式、 模板方法、 命令模式、 解释器模式、 ...

    C# .Net设计模式与代码实现

    包含以下文章所有代码及测试: C# .Net设计模式与代码实现(一)——创建型模式 C# .Net设计模式与代码实现(二)——结构型模式 C# .Net设计模式与代码实现(三)——行为型模式

    c#设计模式

    总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。 结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元...

    C#设计模式(pdf格式)

    C#设计模式,适合初学者,讲了创建,型行为型,结构型模型

    C#面向对象设计模式纵横谈(4):Builder 生成器模式(创建型模式)

    C#面向对象设计模式纵横谈(4):Builder 生成器模式(创建型模式) 体验课程

    李建忠 C#面向对象设计模式纵横谈(3):Abstract Factory 抽象工厂模式(创建型模式)

    C#面向对象设计模式纵横谈(3):Abstract Factory 抽象工厂模式(创建型模式)

    C#视频-面向对象设计模式纵横谈(2):Singleton 单件(创建型模式)

    C#视频-面向对象设计模式纵横谈(2):Singleton 单件(创建型模式)

    C#面向对象设计模式纵横谈\4 创建型模式Builder生成器模式.zip

    在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要资源分。敬请期待。 这是第4节:创建型...

    C#设计模式(中文版)

    [美]James W.Cooper 著 第一部分 C#面向对象程序设计 第二部分 创建型模式 第三部分 结构型模式 第四部分 行为型模式

    【 C#资源 】 C#设计模式(含随书源码)

    本资源《C#设计模式(含随书源码)》是一本专为C#开发者打造的设计模式学习指南。它详细介绍了多种常用的设计模式,包括创建型模式、结构型模式和行为型模式等,每种模式都通过生动的案例和代码示例进行了深入剖析。...

    C#设计模式含随书源码

    本书主要介绍如何用最常见的设计模式编写C#程序。全书分为四个部分,首先介绍了C#语言和面向对象程序设计的一般原则,可作为C#程序设计的快速入门教程;然后分别讲述了创建型模式、结构型模式和行为型模式。每一类...

    C#设计模式(含随书源码)

    本书主要介绍如何用最常见的设计模式编写C#程序。全书分为四个部分,首先介绍了C#语言和面向对象程序设计的一般原则,可作为C#程序设计的快速入门教程;然后分别讲述了创建型模式、结构型模式和行为型模式。每一类...

Global site tag (gtag.js) - Google Analytics