软件设计师23种设计模式总结
- 格式:docx
- 大小:895.95 KB
- 文档页数:56
创建型结构型行为型
类Factory Method Adapter
In terpreter
Template Method 对象
Abstract Factory Builder
Prototype
Si ngleto n
Apapter(对象) Bridge
Composite Decorator
Fa?ade Flyweight Proxy
Chain of Resp on sibility
Comma nd
Iterator
Mediator
Meme nto
Observer
State
Strategy
Visitor
(抽象工厂)
提供一个创建一系列相关或互相依赖对象的接口,而无须制定它们具体的类。
图10-25抽象工厂模式结构图
Abstract Factory 抽象工厂
class Program
{
static void Main(string[] args)
{
AbstractFactory factory1 = new Con creteFactory1();
Clie nt c1 = new Clie nt(factory1);
c1.Ru n();
AbstractFactory factory2 = new Con creteFactory2();
Clie nt c2 = new Clie nt(factory2);
c2.Ru n();
Co nsole.Read();
abstract class AbstractFactory
{
public abstract AbstractProductA CreateProductA(); public abstract AbstractProductB
CreateProductB(); }
class ConcreteFactory1 : AbstractFactory
{
public override AbstractProductA CreateProductA() {
return new ProductA1();
}
public override AbstractProductB CreateProductB() {
return new ProductB1();
}
}
class ConcreteFactory2 : AbstractFactory
{
public override AbstractProductA CreateProductA() {
return new ProductA2();
}
public override AbstractProductB CreateProductB() {
return new ProductB2();
}
}
abstract class AbstractProductA
{
}
abstract class AbstractProductB
{
public abstract void Interact(AbstractProductA a); }
class ProductA1 : AbstractProductA
{
}
class ProductB1 : AbstractProductB
{
public override void Interact(AbstractProductA a) {
Console.WriteLine(this.GetType().Name +
"interacts with " + a.GetType().Name);
class ProductA2 : AbstractProductA
{
}
class ProductB2 : AbstractProductB
{
public override void Interact(AbstractProductA a)
{
Console.WriteLine(this.GetType().Name +
"interacts with " + a.GetType().Name);
}
}
class Client
{
private AbstractProductA AbstractProductA;
private AbstractProductB AbstractProductB;
// Constructor
public Client(AbstractFactory factory)
{
AbstractProductB = factory.CreateProductB();
AbstractProductA = factory.CreateProductA();
}
public void Run()
{ AbstractProductB.Interact(AbstractProductA);
}
}
2. Builder (生成器) 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。