抽象工厂模式

抽象工厂模式


  抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

  在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

介绍


[意图]:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

[主要解决]:主要解决接口选择的问题。

[何时使用]:系统的产品有多于一个的产品族,而系统只消费其中某一族的产品。

[如何解决]:在一个产品族里面,定义多个产品。

[关键代码]:在一个工厂里聚合多个同类产品。

[应用实例]:工作了,为了参加一些聚会,肯定有两套或多套衣服吧,比如说有商务装(成套,一系列具体产品)、时尚装(成套,一系列具体产品),甚至对于一个家庭来说,可能有商务女装、商务男装、时尚女装、时尚男装,这些也都是成套的,即一系列具体产品。假设一种情况(现实中是不存在的,要不然,没法进入共产主义了,但有利于说明抽象工厂模式),在您的家中,某一个衣柜(具体工厂)只能存放某一种这样的衣服(成套,一系列具体产品),每次拿这种成套的衣服时也自然要从这个衣柜中取出了。用 OOP 的思想去理解,所有的衣柜(具体工厂)都是衣柜类的(抽象工厂)某一个,而每一件成套的衣服又包括具体的上衣(某一具体产品),裤子(某一具体产品),这些具体的上衣其实也都是上衣(抽象产品),具体的裤子也都是裤子(另一个抽象产品)。

[优点]:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。

[缺点]:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。

[使用场景]:

  • QQ 换皮肤,一整套一起换。
  • 生成不同操作系统的程序。

[注意事项]:产品族难扩展,产品等级易扩展。
 

实现1


#include <iostream>
#include <memory>
#include <algorithm>
#include <cstring>


bool stringCompareIgnoreCase(const std::string& lhs, const std::string& rhs)
{
    return strcasecmp(lhs.c_str(), rhs.c_str());
}


class Shape
{
public:
    virtual void draw()const{};
};

class Cirecle final : public Shape
{
    virtual void draw()const override
    {
        std::cout << "call Cirecle::draw()" << std::endl;
    }
};

class Square final : public Shape
{
    virtual void draw()const override
    {
        std::cout << "call Square::draw()" << std::endl;
    }
};

class Rectamgle final : public Shape
{
    virtual void draw()const override
    {
        std::cout << "call Rectamgle::draw()" << std::endl;
    }
};


class Color
{
public:
    virtual void fill()const{}
};

class Red final : public Color
{
    virtual void fill()const override
    {
        std::cout << "call Red::fill()" << std::endl;
    }
};

class Green final : public Color
{
    virtual void fill()const override
    {
        std::cout << "call Green::fill()" << std::endl;
    }
};

class Blue final : public Color
{
    virtual void fill()const override
    {
        std::cout << "call Blue::fill()" << std::endl;
    }
};


class AbstractFactory
{
public:
    virtual std::shared_ptr<Shape> getShape(std::string ShapeType){}
    virtual std::shared_ptr<Color> getColor(std::string ColorType){}
};

class ShapeFactory final : public AbstractFactory
{
public:
    virtual std::shared_ptr<Shape> getShape(std::string ShapeType) override
    {
        if(ShapeType.empty())
            return nullptr;
        
        if(ShapeType == "Cirecle")
            return std::make_shared<Cirecle>();
        else if(ShapeType == "Square")
            return std::make_shared<Square>();
        else if(ShapeType == "Rectamgle")
            return std::make_shared<Rectamgle>();
        else
            return nullptr;    
    }
};

class ColorFactory final : public AbstractFactory
{
public:
    virtual std::shared_ptr<Color> getColor(std::string ColorType) override
    {
        if(ColorType.empty())
            return nullptr;
        
        if(ColorType == "Red")
            return std::make_shared<Red>();
        else if(ColorType == "Green")
            return std::make_shared<Green>();
        else if(ColorType == "Blue")
            return std::make_shared<Blue>();
        else
            return nullptr;    
    }
};


class FactoryProducer final
{
public:
    std::shared_ptr<AbstractFactory> getFactory(std::string FactoryType)
    {
        if(FactoryType.empty())
            return nullptr;
        
        if(FactoryType == "Shape")
            return std::make_shared<ShapeFactory>();
        else if(FactoryType == "Color")
            return std::make_shared<ColorFactory>();
        else
            return nullptr;
    }
};



int main(int argc, char const *argv[])
{
    
    std::shared_ptr<AbstractFactory> pShapeFactory = std::make_shared<FactoryProducer>()->getFactory("Shape");
    std::weak_ptr<AbstractFactory> weakp_ShapeFactory(pShapeFactory);
    auto realp_ShapeFactory = weakp_ShapeFactory.lock();
    if(realp_ShapeFactory)
    {
        std::shared_ptr<Shape> pShape =  realp_ShapeFactory->getShape("Crecle");
        std::weak_ptr<Shape> weakp_Shape(pShape);
        auto realp_Shape = weakp_Shape.lock();
        if(realp_Shape)
        {
            realp_Shape->draw();
        }

        pShape = pShapeFactory->getShape("Square");
        weakp_Shape = pShape;
        realp_Shape = weakp_Shape.lock();
        if(realp_Shape)
        {
            realp_Shape->draw();
        }

    }


    std::shared_ptr<AbstractFactory> pColorFactory = std::make_shared<FactoryProducer>()->getFactory("Color");
    std::weak_ptr<AbstractFactory> weakp_ColorFactory(pColorFactory);
    auto realp_ColorFactory = weakp_ColorFactory.lock();
    if(realp_ColorFactory)
    {
        std::shared_ptr<Color> pColor = realp_ColorFactory->getColor("Red");
        std::weak_ptr<Color> weakp_Color(pColor);
        auto realp_Color = weakp_Color.lock();
        if(realp_Color)
        {
            realp_Color->fill();
        }

        pColor = realp_ColorFactory->getColor("Blue");
        weakp_Color = pColor;
        realp_Color = weakp_Color.lock();
        if(realp_Color)
        {
            realp_Color->fill();
        }
    }
    
    return 0;
}

实现2


#include <iostream>
#include <memory>
#include <vector>

class Product
{
public:
    Product(){}
    virtual void use()const{};
};


class Factory
{
public:
    const std::shared_ptr<Product> Create(const std::string& owner)
    {
        std::shared_ptr<Product> p = CreateProduct(owner);
        registerProduct(p);
        return p;
    };
    
protected:
    virtual std::shared_ptr<Product> CreateProduct(const std::string& owner)const = 0;
    virtual void registerProduct(std::shared_ptr<Product>& product) = 0;
};


class IDCard final: public Product
{
public:
    explicit IDCard(std::string owner):_owner(owner)
    {
        std::cout << "制作 " + _owner + "的ID卡。" << std::endl;
    }

    virtual void use()const override
    {
        std::cout << "使用 " + _owner + "的ID卡。" << std::endl;
    }

    std::string getOwner()const
    {
        return _owner;
    }

private:
    std::string _owner;
};

class IDCardFactory final: public Factory
{
public:
    explicit IDCardFactory(){}

    virtual std::shared_ptr<Product> CreateProduct(const std::string& owner)const override 
    {
        return std::make_shared<IDCard>(owner);
    }

    virtual void registerProduct(std::shared_ptr<Product>& product) override
    {
        owners.push_back( static_cast<IDCard*>(product.get())->getOwner() );
    }

private:
    std::vector<std::string> owners;
};


int main(void)
{

    std::shared_ptr<Factory> factory = std::make_shared<IDCardFactory>();
    
    std::shared_ptr<Product> p1 = factory->Create("123");
    std::shared_ptr<Product> p2 = factory->Create("456");
    std::shared_ptr<Product> p3 = factory->Create("789");
    
    p1->use();
    p2->use();
    p3->use();


    return 0;
}

转载来源:https://www.runoob.com/design-pattern/

posted @ 2019-11-08 15:33  sfdevs  阅读(77)  评论(0)    收藏  举报