创建型模式

1. 简单工厂

特点:根据条件来创建不同的具体产品

simple_factory

2. 工厂方法

特点:有抽象的产品和抽象的工厂,但是这里的工厂只负责生产一样产品,如需多个产品,则需要多个工厂来生产

Factory method

3. 抽象工厂

特点:一个工厂负责生产一系列不同的产品

abstract_factory

4. Builder

特点:使用同样的建造流程来创建不同的产品

builder

示例代码
  1. class Director {
  2.     public Device Construct(DeviceBuilder builder) {
  3.         string xml = builder.GetDescriptorFile();
  4.         builder.LoadDescriptorFile();
  5.         return builder.Simulate();
  6.     }
  7. }
  8.  
  9. /// <summary>
  10. /// This is the product (would be built by builder)
  11. /// </summary>
  12. class Device {
  13. }
  14.  
  15. /// <summary>
  16. /// abstract builder: 与标准的builder不太一致的地方是,这里的buildpart都用了abstract,实际代码可以不用abstract
  17. /// </summary>
  18. class DeviceBuilder {
  19.     public abstract string GetDescriptorFile();
  20.     public abstract void LoadDescriptorFile();
  21.     public abstract Device Simulate();
  22. }
  23.  
  24. /// <summary>
  25. /// Concrete builder
  26. /// </summary>
  27. class CameraBuilder : DeviceBuilder {
  28.     public override string GetDescriptorFile() {
  29.         throw new NotImplementedException();
  30.     }
  31.  
  32.     public override void LoadDescriptorFile() {
  33.         throw new NotImplementedException();
  34.     }
  35.  
  36.     public override Device Simulate() {
  37.         throw new NotImplementedException();
  38.     }
  39. }
posted @ 2010-03-16 23:40  如斯夫  阅读(203)  评论(0编辑  收藏  举报