01 Abstract Factory(抽象工厂)
简要说明
抽象工厂模式拥有一组工厂类,这些工厂类实现同一个抽象工厂接口,这个接口定义了一系列创建方法,每一个方法创建一种产品,所有方法所创建的产品组成一个系列。不同的具体工厂类创建不同系列的系列产品,系列中每一种产品实现同一个接口。客户类依赖抽象工厂接口及各个产品接口,并通过实例化不同的具体工厂创建不同的产品系列。
抽象工厂适用于客户类拥有与产品系列无关的加工逻辑的场合,此时通过抽象工厂模式,将创建产品系列的逻辑与加工逻辑分离,那么同样的加工逻辑就可以作用于不同的产品系列,至于创建哪个系列的产品则由客户类拥有的具体的工厂决定。
结构类图
结构示例代码
/************************************************** |
* |
* Design Pattren Quick Start |
* 01 Abstract Factory - 抽象工厂 |
* |
**************************************************/ |
namespace DesignPatternQuickStart.AbstractFactory |
{ |
/// |
/// 产品A的接口 |
/// |
interface IProductA { } |
|
/// |
/// 品种为1的产品A |
/// |
class ProductA1 : IProductA { } |
|
/// |
/// 品种为2的产品A |
/// |
class ProductA2 : IProductA { } |
|
/// |
/// 产品B的接口 |
/// |
interface IProductB { } |
|
/// |
/// 品种为1的产品B |
/// |
class ProductB1 : IProductB { } |
|
/// |
/// 品种为2的产品B |
/// |
class ProductB2 : IProductB { } |
|
/// |
/// 抽象工厂接口 |
/// |
interface IAbstractFactory |
{ |
IProductA CreateProductA(); |
IProductB CreateProductB(); |
} |
|
/// |
/// 生产1系列的具体工厂,用于生产品种为1为产品A和B |
/// |
class Factory1 : IAbstractFactory |
{ |
public IProductA CreateProductA() |
{ |
return new ProductA1(); |
} |
|
public IProductB CreateProductB() |
{ |
return new ProductB1(); |
} |
} |
|
/// |
/// 生产2系列的具体工厂,用于生产品种为2为产品A和B |
/// |
class Factory2 : IAbstractFactory |
{ |
public IProductA CreateProductA() |
{ |
return new ProductA2(); |
} |
|
public IProductB CreateProductB() |
{ |
return new ProductB2(); |
} |
} |
|
/// |
/// 客户类 |
/// |
class Client |
{ |
public void OpreateMethod() |
{ |
IAbstractFactory factory1 = new Factory1(); |
IProductA productA1 = factory1.CreateProductA(); |
IProductB productB1 = factory1.CreateProductB(); |
|
IAbstractFactory factory2 = new Factory2(); |
IProductA productA2 = factory2.CreateProductA(); |
IProductB productB2 = factory2.CreateProductB(); |
} |
} |
} |
02 Builder(生成器)
简要说明
生成器模式首先定义一个生成器接口,接口中定义了生成一个产品(或逻辑上可看做整体的产品)各个部分的方法及返回这个产品的方法。不同的生成器可以封装不同的生成算法。客户类在构造函数中通过生成器生成需要的这个产品。生成器适用于待构建对象十分复杂,并根据具体情况存在不同构建算法的场合,使用生成器模式后复杂对象的构建过程与使用过程分离。
结构类图
结构示例代码
/************************************************** |
* |
* Design Pattren Quick Start |
* 02 Builder - 生成器 |
* |
**************************************************/ |
namespace DesignPatternQuickStart.Builder |
{ |
/// |
/// 含有不同模块的产品 |
/// |
class Product |
{ |
public int PartA { get; set; } |
public object PartB { get; set; } |
public string PartC { get; set; } |
} |
|
/// |
/// 生成器接口 |
/// |
interface IBuilder |
{ |
void BuildPartA(int partA); |
void BuildPartB(object partB); |
void BuildPartC(string partC); |
Product GetResult(); |
} |
|
/// |
/// 具体的生成器 |
/// |
class Builder : IBuilder |
{ |
protected Product _product; |
|
public Builder() |
{ |
this._product = new Product(); |
} |
|
//此处可以使用任意复杂的构造算法构造不同模块 |
public void BuildPartA(int partA) |
{ |
this._product.PartA = partA; |
} |
|
public void BuildPartB(object partB) |
{ |
this._product.PartB = partB; |
} |
|
public void BuildPartC(string partC) |
{ |
this._product.PartC = partC; |
} |
|
public Product GetResult() |
{ |
return this._product; |
} |
} |
|
/// |
/// 生成器的客户类 |
/// |
class Client |
{ |
protected Product _product; |
|
public Client() |
{ |
IBuilder buider = new Builder(); |
buider.BuildPartA(100); |
buider.BuildPartB(null); |
buider.BuildPartC("abc"); |
|
this._product = buider.GetResult(); |
} |
} |
} |
03 工厂方法(Factory Method)
简要说明
工厂方法通过一个抽象类实现了所有对产品的加工操作代码,唯独将产品的构建方法写成抽象方法。继承这个抽象类的具体类只重写其构建方法,这样就实现了对于不同被构建产品复用相同的加工操作逻辑。工厂方法适用于需要在子类中才能决定实例化哪个被操作对象,同时这些被操作对象又复用相同操作逻辑的场合。
结构类图
结构示例代码
/************************************************** |
* |
* Design Pattren Quick Start |
* 03 FactoryMethod - 工厂方法 |
* |
**************************************************/ |
namespace DesignPatternQuickStart.FactoryMethod |
{ |
/// |
/// 产品接口 |
/// |
interface IProduct { } |
|
/// |
/// A类型产品 |
/// |
class ProductA : IProduct { } |
|
/// |
/// B类型产品 |
/// |
class ProductB : IProduct { } |
|
/// |
/// 含有工厂方法的抽象业务类 |
/// |
abstract class ACreator |
{ |
protected abstract IProduct FactoryMethod(); |
|
public void OpreateMethod() |
{ |
IProduct product = FactoryMethod(); |
//对product的一系列操作 |
} |
} |
|
/// |
/// 生产A类型产品的具体业务类 |
/// |
class CreatorA : ACreator |
{ |
protected override IProduct FactoryMethod() |
{ |
return new ProductA(); |
} |
} |
|
/// |
/// 生产B类型产品的具体业务类 |
/// |
class CreatorB : ACreator |
{ |
protected override IProduct FactoryMethod() |
{ |
return new ProductB(); |
} |
} |
|
/// |
/// 客户类 |
/// |
class Client |
{ |
public void OpreateMethod() |
{ |
ACreator creatorA = new CreatorA(); |
creatorA.OpreateMethod(); |
|
ACreator creatorB = new CreatorB(); |
creatorB.OpreateMethod(); |
} |
} |
} |
04 Prototype(原型)
简要说明
原型模式定义一个原型接口,其中有克隆自身的方法及此接口所有实现及后代都应该具有的方法的定义。其实现及实现的后来均重写克隆方法,使得克隆方法返回自身的一个克隆,克隆对象具有母对象的所有特性。客户类使用不同对象的克隆实现对象的创建。原型模式适用于需要创建许多来自同一抽象的不同后代对象,同时希望通过已有对象克隆来创建新对象的场合。
结构类图
结构示例代码
/************************************************** |
* |
* Design Pattren Quick Start |
* 04 Prototype - 原型 |
* |
**************************************************/ |
namespace DesignPatternQuickStart.Prototype |
{ |
/// |
/// 原型接口 |
/// |
interface IPrototype |
{ |
IPrototype Clone(); |
} |
|
/// |
/// 具体原型类A |
/// |
class PrototypeA : IPrototype |
{ |
public string Member1 { get; set; } |
|
public IPrototype Clone() |
{ |
PrototypeA cloneObject = new PrototypeA(); |
cloneObject.Member1 = Member1; |
return cloneObject; |
} |
} |
|
/// |
/// 具体原型类AA |
/// |
class PrototypeAA : PrototypeA |
{ |
public string Member2 { get; set; } |
|
public new IPrototype Clone() |
{ |
PrototypeAA cloneObject = new PrototypeAA(); |
cloneObject.Member1 = Member1; |
cloneObject.Member2 = Member2; |
return cloneObject; |
} |
} |
|
/// |
/// 客户类 |
/// |
class Client |
{ |
public void OpreateMethod() |
{ |
IPrototype prototypeA1 = new PrototypeA(); |
IPrototype prototypeA2 = prototypeA1.Clone(); |
IPrototype prototypeAA1 = new PrototypeAA(); |
IPrototype prototypeAA2 = prototypeAA1.Clone(); |
} |
} |
} |
05 Singleton(单件)
简要说明
单件模式试图保证单件类在全局只存在一个实例。常用的方法是将构造函数设为私有,然后提供一个公用静态方法用于获取单例类的实例,这个公用静态方法保证每次都返回同一个实例。单例模式适用于需要保证某个类在全局只存在一个实例的场合。
结构类图
结构示例代码
/************************************************** |
* |
* Design Pattren Quick Start |
* 05 Singleton - 单件 |
* |
**************************************************/ |
namespace DesignPatternQuickStart.Singleton |
{ |
/// |
/// 单件类 |
/// |
class Singleton |
{ |
static Singleton _instance; |
|
private Singleton() { } |
|
public static Singleton CreateInstance() |
{ |
return null == _instance ? _instance = new Singleton() : _instance; |
} |
} |
|
/// |
/// 客户类 |
/// |
class Client |
{ |
Singleton singleton = Singleton.CreateInstance(); |
} |
} |





浙公网安备 33010602011771号