设计模式之工厂模式(c++)

我们可以从面包房购买到提拉米苏、拿破仑,当他们新出一款抹茶蛋糕时,收银台是不会被拆掉的,还是需要花钱购买
工厂模式就是为我们提供了创建对象的统一接口,新增的类型不会影响到接口
工厂模式包含三种:静态工厂、多态工厂、抽象工厂,它属于构造型设计模式

  1. 静态工厂使用简单,适合于变化较少的情况
namespace STATIC_FACTORY {
class Shape {
public:
    class BadCreation : public std::logic_error {
    public:
        BadCreation(const string& type) 
        : std::logic_error("Can not create " + type)
        {
        }
    };
    static Shape* createFactory(const string& type) throw(BadCreation);
    virtual void draw() = 0;
    virtual void erase() = 0;
    virtual ~Shape() {};
};
class Circle : public Shape {
    Circle() {}
    friend class Shape;
public:
    void draw() { cout << "Circle::draw()" << endl; }
    void erase() { cout << "Circle::erase()" << endl; }
};
class Square : public Shape {
    Square() {}
    friend class Shape;
public:
    void draw() { cout << "Square::draw()" << endl; }
    void erase() { cout << "Square::erase()" << endl; }
};
Shape* Shape::createFactory(const string& type)
    throw(Shape::BadCreation)
{
    if ("Circle" == type) {
        return new Circle();
    }
    else if ("Square" == type) {
        return new Square();
    }
    else {
        throw BadCreation(type);
        return NULL;
    }
}
void staticFactoryTest()
{
    const string types[] = {"Circle", "Square", "abc", "Circle"};
    vector<Shape*> shapes;
    try {
        for (int i = 0; i < sizeof(types)/sizeof(types[0]); i++)
        {
            shapes.push_back(Shape::createFactory(types[i]));
        }
    } catch (Shape::BadCreation e) {
        cout << e.what() << endl;
        return;
    }
    for (int i = 0; i < shapes.size(); i++)
    {
        shapes[i]->draw();
        shapes[i]->erase();
    }
}
}

2.多态工厂是静态工厂的升级版,是将factory抽象出来,可以更容易的增加构建的类型,但同时增加了开销

工厂模式类图

class Shape {
public:
    virtual void draw() = 0;
    virtual void erase() = 0;
    virtual ~Shape() {}
};
class ShapeFacotryInitializer;
class ShapeFactory {
    virtual Shape *create() = 0;
    static map<string, ShapeFactory*> factories;
public:
    virtual ~ShapeFactory() {}
    friend class ShapeFacotryInitializer;
    class BadShapeCreation : public logic_error {
    public:
        BadShapeCreation(string type)
        : logic_error("Cannot create type " + type) {}
    };
    static Shape *createShape(const string& id) throw(BadShapeCreation) {
        if (factories.find(id) != factories.end())
            return factories[id]->create();
        else
            throw BadShapeCreation(id);
    }
};

map<string, ShapeFactory*> ShapeFactory::factories;

class Circle : public Shape {
    Circle() {}
    friend class ShapeFacotryInitializer;
    class Factory;
    friend class Factory;
    class Factory : public ShapeFactory {
    public:
        Shape *create() { return new Circle(); }
    };
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 ShapeFacotryInitializer;
    class Factory;
    friend class Factory;
    class Factory : public ShapeFactory {
    public:
        Shape *create() { return new Square(); }
    };
public:
    void draw() { cout << "Square::draw" << endl; }
    void erase() { cout << "Square::erase" << endl; }
    ~Square() { cout << "Square::~Square" << endl; }
};

class ShapeFacotryInitializer {
    static ShapeFacotryInitializer si;
    ShapeFacotryInitializer() {
        ShapeFactory::factories["Circle"] = new Circle::Factory();
        ShapeFactory::factories["Square"] = new Square::Factory();
    }
    ~ShapeFacotryInitializer() {
        map<string, ShapeFactory*>::iterator it = ShapeFactory::factories.begin();
        while (it != ShapeFactory::factories.end())
            delete it++->second;
    }
};

ShapeFacotryInitializer ShapeFacotryInitializer::si;

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

int FactoryTest() {
    vector<Shape*> shapes;
    try {
        for (int i = 0; i < sizeof(sl)/sizeof(sl[0]); i++)
            shapes.push_back(ShapeFactory::createShape(sl[i]));
    } catch(ShapeFactory::BadShapeCreation e) {
        cout << e.what() << endl;
        return -1;
    }
    for (int i = 0; i < shapes.size(); i++) {
        shapes[i]->draw();
        shapes[i]->erase();
    }
}

3.抽象工厂是工厂模式的一个集合,是工厂模式与策略模式的结合,提高了构建对象的灵活度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值