1. 意图

        旨在提供对象创建管理接口,对一系列相关或相互依赖的对象提供统一创建接口,避免在软件中使用各类创建其对象。

2. 适用性

        《Gof 设计模式-可复用面向对象软件的基础》中对此模式的适用性描述如下:

  • 一个系统要独立于它的产品的创建、组合和表示时。
  • 一个系统要由多个系列产品中的一个来配置时。
  • 要强调一系列相关的产品对象的设计以便进行联合使用时。
  • 提供一个产品类库,而只想显示他们的接口而不是实现时。

3. 实现

  • 简单工厂模式:实现相对简单,类成员函数可以根据不同的参数创建不同的对象,被创建的类对象一般都拥有相同的基类。
class Computer {
public:
  virtual ~Computer (){}
  virtual void Print() = 0;
};

class Memory : public Computer {
  virtual void Print() override { std::cout << "Memory" << std::endl; }
};
class Cpu : public Computer {
  virtual void Print() override { std::cout << "Cpu" << std::endl; }
};
class Screen : public Computer {
  virtual void Print() override { std::cout << "Screen" << std::endl; }
};

class SimpleFactory {
public:
  enum class EnumCreateType { kMemory, kCpu, kScreen };
  Computer *Create(const EnumCreateType &type) {
    switch (type) {
    case EnumCreateType::kMemory:
      return new Memory;
      break;
    case EnumCreateType::kCpu:
      return new Cpu;
      break;
    case EnumCreateType::kScreen:
      return new Screen;
      break;
    default:;
    }
    return nullptr;
  }
};

void Test() {
  SimpleFactory factor;
  Computer *ptr = factor.Create(SimpleFactory::EnumCreateType::kMemory);
  if (ptr) {
    ptr->Print();
    delete ptr;
  }
  ptr = factor.Create(SimpleFactory::EnumCreateType::kCpu);
  if (ptr) {
    ptr->Print();
    delete ptr;
  }
  ptr = factor.Create(SimpleFactory::EnumCreateType::kScreen);
  if (ptr) {
    ptr->Print();
    delete ptr;
  }
}

int main() {
  Test();
  return 0;
}

执行输出

Memory
Cpu
Screen

简单工厂模式模板实现

class Computer {
public:
  virtual ~Computer (){}
  virtual void Print() = 0;
};

class Memory : public Computer {
public:
  Memory(int id) : Computer(), m_id(id) {}
  virtual void Print() override { std::cout << "Memory " << m_id << std::endl; }

private:
  int m_id;
};
class Cpu : public Computer {
public:
  Cpu(int id) : Computer(), m_id(id) {}
  virtual void Print() override { std::cout << "Cpu " << m_id << std::endl; }

private:
  int m_id;
};
class Screen : public Computer {
public:
  Screen(int id) : Computer(), m_id(id) {}
  virtual void Print() override { std::cout << "Screen " << m_id << std::endl; }

private:
  int m_id;
};

class SimpleFactory {
public:
  enum class EnumCreateType { kMemory, kCpu, kScreen };

  template <typename... Args>
  Computer *Create(const EnumCreateType &type, Args &&...args) {
    switch (type) {
    case EnumCreateType::kMemory:
      return new Memory(std::forward<Args>(args)...);
      break;
    case EnumCreateType::kCpu:
      return new Cpu(std::forward<Args>(args)...);
      break;
    case EnumCreateType::kScreen:
      return new Screen(std::forward<Args>(args)...);
      break;
    default:;
    }
    return nullptr;
  }
};

void Test() {
  SimpleFactory factor;
  Computer *ptr = factor.Create(SimpleFactory::EnumCreateType::kMemory, 10);
  if (ptr) {
    ptr->Print();
    delete ptr;
  }
  ptr = factor.Create(SimpleFactory::EnumCreateType::kCpu, 20);
  if (ptr) {
    ptr->Print();
    delete ptr;
  }
  ptr = factor.Create(SimpleFactory::EnumCreateType::kScreen, 30);
  if (ptr) {
    ptr->Print();
    delete ptr;
  }
}

int main() {
  Test();
  return 0;
}

执行输出

Memory 10
Cpu 20
Screen 30
  • 工厂方法模式:使用多个工厂类来创建多种类对象。
class Computer {
public:
  virtual ~Computer() {}
  virtual void Print() = 0;
};

class Memory : public Computer {
  virtual void Print() override { std::cout << "Memory" << std::endl; }
};
class Cpu : public Computer {
  virtual void Print() override { std::cout << "Cpu" << std::endl; }
};
class Screen : public Computer {
  virtual void Print() override { std::cout << "Screen" << std::endl; }
};

class FactoryMethod {
public:
  virtual ~FactoryMethod() {}
  virtual Computer *Create() = 0;
};

class MemoryFactory : public FactoryMethod {
public:
  virtual Computer *Create() override { return new Memory; }
};

class CpuFactory : public FactoryMethod {
public:
  virtual Computer *Create() override { return new Cpu; }
};

class ScreenFactory : public FactoryMethod {
public:
  virtual Computer *Create() override { return new Screen; }
};

void Test() {
  try {
    FactoryMethod *factor = new MemoryFactory;
    Computer *ptr = factor->Create();
    if (ptr) {
      ptr->Print();
      delete ptr;
      delete factor;
    }

    factor = new CpuFactory;
    ptr = factor->Create();
    if (ptr) {
      ptr->Print();
      delete ptr;
      delete factor;
    }

    factor = new ScreenFactory;
    ptr = factor->Create();
    if (ptr) {
      ptr->Print();
      delete ptr;
      delete factor;
    }

  } catch (const std::bad_alloc &err) {
    std::cout << err.what() << std::endl;
  }
}

int main() {
  Test();
  return 0;
}

执行输出

Memory
Cpu
Screen
  • 抽象工厂模式:一个工厂子类不止能创建一种具有相同规则的对象,相对于工厂模式就可以有效的减少创建工厂子类的数量。
#include <iostream>

class Memory {
public:
  virtual ~Memory() {}
  virtual void Print() = 0;
};
class CMemory : public Memory {
public:
  virtual void Print() override { std::cout << "CMemory" << std::endl; }
};
class RMemory : public Memory {
public:
  virtual void Print() override { std::cout << "RMemory" << std::endl; }
};
class FMemory : public Memory {
public:
  virtual void Print() override { std::cout << "FMemory" << std::endl; }
};

class Cpu {
public:
  virtual ~Cpu() {}
  virtual void Print() = 0;
};
class CCpu : public Cpu {
public:
  virtual void Print() override { std::cout << "CCpu" << std::endl; }
};
class RCpu : public Cpu {
public:
  virtual void Print() override { std::cout << "RCpu" << std::endl; }
};
class FCpu : public Cpu {
public:
  virtual void Print() override { std::cout << "FCpu" << std::endl; }
};

class Screen {
public:
  virtual ~Screen() {}
  virtual void Print() = 0;
};
class CScreen : public Screen {
public:
  virtual void Print() override { std::cout << "CScreen" << std::endl; }
};
class RScreen : public Screen {
public:
  virtual void Print() override { std::cout << "RScreen" << std::endl; }
};
class FScreen : public Screen {
public:
  virtual void Print() override { std::cout << "FScreen" << std::endl; }
};

class AbstractFactory {
public:
  virtual ~AbstractFactory() {}

  virtual Memory *CreateMemory() = 0;
  virtual Cpu *CreateCpu() = 0;
  virtual Screen *CreateScreen() = 0;
};

class CFactory : public AbstractFactory {
public:
  virtual Memory *CreateMemory() override { return new CMemory; }
  virtual Cpu *CreateCpu() override { return new CCpu; }
  virtual Screen *CreateScreen() override { return new CScreen; }
};

class RFactory : public AbstractFactory {
public:
  virtual Memory *CreateMemory() override { return new RMemory; }
  virtual Cpu *CreateCpu() override { return new RCpu; }
  virtual Screen *CreateScreen() override { return new RScreen; }
};

class FFactory : public AbstractFactory {
public:
  virtual Memory *CreateMemory() override { return new FMemory; }
  virtual Cpu *CreateCpu() override { return new FCpu; }
  virtual Screen *CreateScreen() override { return new FScreen; }
};

class Computer {
public:
  void Assemble(Memory *memory, Cpu *cpu, Screen *screen) {
    memory->Print();
    cpu->Print();
    screen->Print();
  }
};

void Test() {
  AbstractFactory *factor1 = new CFactory;
  Memory *m1 = factor1->CreateMemory();
  Cpu *c1 = factor1->CreateCpu();
  Screen *s1 = factor1->CreateScreen();

  AbstractFactory *factor2 = new RFactory;
  Memory *m2 = factor2->CreateMemory();
  Cpu *c2 = factor2->CreateCpu();
  Screen *s2 = factor2->CreateScreen();

  AbstractFactory *factor3 = new FFactory;
  Memory *m3 = factor3->CreateMemory();
  Cpu *c3 = factor3->CreateCpu();
  Screen *s3 = factor3->CreateScreen();

  Computer computer;
  computer.Assemble(m1, c2, s3);
  computer.Assemble(m2, c3, s2);
  computer.Assemble(m3, c1, s1);

  delete factor1;
  delete factor2;
  delete factor3;
  delete m1;
  delete m2;
  delete m3;
  delete c1;
  delete c2;
  delete c3;
  delete s1;
  delete s2;
  delete s3;
}

int main() {
  Test();
  return 0;
}

执行输出

CMemory    RCpu    FScreen
RMemory    FCpu    RScreen
FMemory    CCpu    CScreen

4. 优缺点

  • 它分离了具体的类
  • 有利于产品的一致性
  • 难以支持新种类的产品

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部