C++编程思想 第2卷 第10章 设计模式 工厂模式:封装对象的创建 虚构造函数

使用工厂方法模式的主要目标是更好地组织代码
使得在创建对象时不需要选择准确的构造函数类型
告诉工厂
现在还不能确切地知道需要什么类型的对象
但是有一些信息
创建类型适当的对象

//: C10:VirtualConstructor.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 <string>
#include <stdexcept>
#include <stdexcept>
#include <cstddef>
#include <vector>
#include "../purge.h"
using namespace std;

class Shape {
  Shape* s;
  // Prevent copy-construction & operator=
  Shape(Shape&);
  Shape operator=(Shape&);
protected:
  Shape() { s = 0; }
public:
  virtual void draw() { s->draw(); }
  virtual void erase() { s->erase(); }
  virtual void test() { s->test(); }
  virtual ~Shape() {
    cout << "~Shape" << endl;
    if(s) {
      cout << "Making virtual call: ";
      s->erase(); // Virtual call
    }
    cout << "delete s: ";
    delete s; // The polymorphic deletion
    // (delete 0 is legal; it produces a no-op)
  }
  class BadShapeCreation : public logic_error {
  public:
    BadShapeCreation(string type)
    : logic_error("Cannot create type " + type) {}
  };
  Shape(string type) throw(BadShapeCreation);
};

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

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

Shape::Shape(string type) throw(Shape::BadShapeCreation) {
  if(type == "Circle")
    s = new Circle;
  else if(type == "Square")
    s = new Square;
  else throw BadShapeCreation(type);
  draw();  // Virtual call in the constructor
}

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

int main() {
  vector<Shape*> shapes;
  cout << "virtual constructor calls:" << endl;
  try {
    for(size_t i = 0; i < sizeof sl / sizeof sl[0]; i++)
      shapes.push_back(new Shape(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();
    cout << "test" << endl;
    shapes[i]->test();
    cout << "end test" << endl;
    shapes[i]->erase();
  }
  Shape c("Circle"); // Create on the stack
  cout << "destructor calls:" << endl;
  purge(shapes);
  getchar();
} ///:~

输出
virtual constructor calls:
Circle::draw
Square::draw
Square::draw
Circle::draw
Circle::draw
Circle::draw
Square::draw
Circle::draw
test
Circle::draw
end test
Circle::erase
Square::draw
test
Square::draw
end test
Square::erase
Square::draw
test
Square::draw
end test
Square::erase
Circle::draw
test
Circle::draw
end test
Circle::erase
Circle::draw
test
Circle::draw
end test
Circle::erase
Circle::draw
test
Circle::draw
end test
Circle::erase
Square::draw
test
Square::draw
end test
Square::erase
Circle::draw
destructor calls:
~Shape
Making virtual call: Circle::erase
delete s: Circle::~Circle
~Shape
delete s: ~Shape
Making virtual call: Square::erase
delete s: Square::~Square
~Shape
delete s: ~Shape
Making virtual call: Square::erase
delete s: Square::~Square
~Shape
delete s: ~Shape
Making virtual call: Circle::erase
delete s: Circle::~Circle
~Shape
delete s: ~Shape
Making virtual call: Circle::erase
delete s: Circle::~Circle
~Shape
delete s: ~Shape
Making virtual call: Circle::erase
delete s: Circle::~Circle
~Shape
delete s: ~Shape
Making virtual call: Square::erase
delete s: Square::~Square
~Shape
delete s:

基类Shape包含一个对象指针作为唯一的数据成员
指针指向Shape类型的对象
基类实际上是一个代理
这是客户端程序唯一看到和与之进行交互的对象
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值