c#学习无限

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

抽象工厂设计模式 (Abstract Factory)

译自:http://www.dofactory.com/Patterns/PatternAbstract.aspx

 定义
 UML类图
 参与者
 在C#中的示例代码

 

定义

提供一个接口,用于在不知道具象(concrete)类的情况下,创建一族(families)相关或互存的对象。 

使用频率:   高

UML类图

        我理解的现实世界中的类图

参与者

    参与这种模式的类和/或对象是:

  • AbstractFactory 抽象工厂(CarFactory 车工厂)
    • 生产抽象产品
  • ConcreteFactory 实体工厂(BMWFactory BensFactory 宝马车工厂、奔驰车工厂
    • 生产实体产品
  • AbstractProduct 抽象产品   (Seat,Tyre 汽车座椅,轮胎)
    • 提供实体产品访问接口
  • Product 产品  HankookSeat, ZhengyangSeat, HankookTyre, ZhengxinTyre 韩泰座椅,正阳红座椅,韩泰轮胎,正阳红轮胎)
    • 功能实现
  • Client 客户端  (Car 车)
    • 调用抽象工厂、抽象产品累

在C#中的示例代码

这种结构的代码演示了Abstract Factory模式创建对象的并行层次。创建对象已经被抽象,也没有必要在客户端代码中硬编码的类名。

View Code
// Abstract Factory pattern -- Structural example

using System;
 
namespace DoFactory.GangOfFour.Abstract.Structural
{
  /// <summary>
  /// MainApp startup class for Structural
  /// Abstract Factory Design Pattern.
  /// </summary>
  class MainApp
  {
    /// <summary>
    /// Entry point into console application.
    /// </summary>
    public static void Main()
    {
      // Abstract factory #1
      AbstractFactory factory1 = new ConcreteFactory1();
      Client client1 = new Client(factory1);
      client1.Run();
 
      // Abstract factory #2
      AbstractFactory factory2 = new ConcreteFactory2();
      Client client2 = new Client(factory2);
      client2.Run();
 
      // Wait for user input
      Console.ReadKey();
    }
  }
 
  /// <summary>
  /// The 'AbstractFactory' abstract class
  /// </summary>
  abstract class AbstractFactory
  {
    public abstract AbstractProductA CreateProductA();
    public abstract AbstractProductB CreateProductB();
  }
 
 
  /// <summary>
  /// The 'ConcreteFactory1' class
  /// </summary>
  class ConcreteFactory1 : AbstractFactory
  {
    public override AbstractProductA CreateProductA()
    {
      return new ProductA1();
    }
    public override AbstractProductB CreateProductB()
    {
      return new ProductB1();
    }
  }
 
  /// <summary>
  /// The 'ConcreteFactory2' class
  /// </summary>
  class ConcreteFactory2 : AbstractFactory
  {
    public override AbstractProductA CreateProductA()
    {
      return new ProductA2();
    }
    public override AbstractProductB CreateProductB()
    {
      return new ProductB2();
    }
  }
 
  /// <summary>
  /// The 'AbstractProductA' abstract class
  /// </summary>
  abstract class AbstractProductA
  {
  }
 
  /// <summary>
  /// The 'AbstractProductB' abstract class
  /// </summary>
  abstract class AbstractProductB
  {
    public abstract void Interact(AbstractProductA a);
  }
 
 
  /// <summary>
  /// The 'ProductA1' class
  /// </summary>
  class ProductA1 : AbstractProductA
  {
  }
 
  /// <summary>
  /// The 'ProductB1' class
  /// </summary>
  class ProductB1 : AbstractProductB
  {
    public override void Interact(AbstractProductA a)
    {
      Console.WriteLine(this.GetType().Name +
        " interacts with " + a.GetType().Name);
    }
  }
 
  /// <summary>
  /// The 'ProductA2' class
  /// </summary>
  class ProductA2 : AbstractProductA
  {
  }
 
  /// <summary>
  /// The 'ProductB2' class
  /// </summary>
  class ProductB2 : AbstractProductB
  {
    public override void Interact(AbstractProductA a)
    {
      Console.WriteLine(this.GetType().Name +
        " interacts with " + a.GetType().Name);
    }
  }
 
  /// <summary>
  /// The 'Client' class. Interaction environment for the products.
  /// </summary>
  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);
    }
  }
}
 

 

现实世界中的代码演示了如何使用不同的工厂在电脑游戏中创建不同的动物世界。虽然洲工厂创建的动物是不同的,动物之间的相互作用保持不变。

 

View Code
using System;

namespace Pattern.AbstractFactory
{
    #region AbstractProductA

    abstract class Seat
    {
        public abstract void ShowDetail();
    }

    #endregion

    #region ProductA1

    class HankookSeat : Seat
    {
        public override void ShowDetail()
        {
            Console.Write("韩泰座椅");
        }
    }

    #endregion

    #region ProductA2

    class ZhengyangSeat : Seat
    {
        public override void ShowDetail()
        {
            Console.Write("正阳红座椅");
        }
    }

    #endregion


    #region AbstractProductB

    abstract class Tyre
    {
        public abstract void ShowDetail();
    }

    #endregion

    #region ProductB1

    class HankookTyre : Tyre
    {
        public override void ShowDetail()
        {
            Console.Write("韩泰轮胎");
        }
    }

    #endregion

    #region ProductB2

    class ZhengxinTyre : Tyre
    {
        public override void ShowDetail()
        {
            Console.Write("正新轮胎");
        }
    }

    #endregion


    #region AbstractFactory

    abstract class CarFactory
    {
        public abstract Seat CreateSeat();
        public abstract Tyre CreateTyre();
    }

    #endregion

    #region ConcreteFactory1

    class BMWFactory : CarFactory
    {
        public override Seat CreateSeat()
        {
            return new HankookSeat();
        }

        public override Tyre CreateTyre()
        {
            return new HankookTyre();
        }
    }

    #endregion

    #region ConcreteFactory2

    class BensFactory : CarFactory
    {
        public override Seat CreateSeat()
        {
            return new ZhengyangSeat();
        }

        public override Tyre CreateTyre()
        {
            return new ZhengxinTyre();
        }
    }

    #endregion

    #region Client

    class Car
    {
        private Seat _seat;
        private Tyre _tyre;

        public Car(CarFactory factory)
        {
            _seat = factory.CreateSeat();
            _tyre = factory.CreateTyre();
        }

        public void ShowConfiguration()
        {
            _tyre.ShowDetail();
            _seat.ShowDetail();
            Console.WriteLine();
        }
    }

    #endregion
}

调用

View Code
using System;
using System.Collections.Generic;
using Pattern.AbstractFactory;

namespace Pattern
{
    class MainApp
    {
        static void Main()
        {
            Car car = new Car(new BMWFactory());
            car.ShowConfiguration();

            car = new Car(new BensFactory());
            car.ShowConfiguration();
        }
    }
}

 

posted on 2013-05-07 14:14  网网由鱼  阅读(174)  评论(0编辑  收藏  举报