1. 程式人生 > >C++程式設計思想 第2卷 第10章 設計模式 工廠模式:封裝物件的建立

C++程式設計思想 第2卷 第10章 設計模式 工廠模式:封裝物件的建立

當發現需要新增新的型別到一個系統中時 明智的是用多型機制為這些新型別建立一個共同的介面 這種方法可以將系統多餘的程式碼與新新增特定型別的程式碼分開

採用工廠模式的一種方法就是在基類中定義一個靜態成員函式

//: C10:ShapeFactory1.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include <iostream>
#include <stdexcept>
#include <cstddef>
#include <string>
#include <vector>
#include "../purge.h"
using namespace std;

class Shape {
public:
  virtual void draw() = 0;
  virtual void erase() = 0;
  virtual ~Shape() {}
  class BadShapeCreation : public logic_error {
  public:
    BadShapeCreation(string type)
    : logic_error("Cannot create type " + type) {}
  };
  static Shape* factory(const string& type)
    throw(BadShapeCreation);
};

class Circle : public Shape {
  Circle() {} // Private constructor
  friend class Shape;
public:
  void draw() { cout << "Circle::draw" << endl; }
  void erase() { cout << "Circle::erase" << endl; }
  ~Circle() { cout << "Circle::~Circle" << endl; }
};

class Square : public Shape {
  Square() {}
  friend class Shape;
public:
  void draw() { cout << "Square::draw" << endl; }
  void erase() { cout << "Square::erase" << endl; }
  ~Square() { cout << "Square::~Square" << endl; }
};

Shape* Shape::factory(const string& type)
  throw(Shape::BadShapeCreation) {
  if(type == "Circle") return new Circle;
  if(type == "Square") return new Square;
  throw BadShapeCreation(type);
}

char* sl[] = { "Circle", "Square", "Square",
  "Circle", "Circle", "Circle", "Square" };

int main() {
  vector<Shape*> shapes;
  try {
    for(size_t i = 0; i < sizeof sl / sizeof sl[0]; i++)
      shapes.push_back(Shape::factory(sl[i]));
  } catch(Shape::BadShapeCreation e) {
    cout << e.what() << endl;
    purge(shapes);
    return EXIT_FAILURE;
  }
  for(size_t i = 0; i < shapes.size(); i++) {
    shapes[i]->draw();
    shapes[i]->erase();
  }
  purge(shapes);
  getchar();
} ///:~

輸出 Circle::draw Circle::erase Square::draw Square::erase Square::draw Square::erase Circle::draw Circle::erase Circle::draw Circle::erase Circle::draw Circle::erase Square::draw Square::erase Circle::~Circle Square::~Square Square::~Square Circle::~Circle Circle::~Circle Circle::~Circle Square::~Square

函式factory()允許以一個引數來決定建立何種型別的Shape 這裡 引數型別為string 也可以是任何資料集