设计模式 知识整理

1、关于23种设计模式的有趣见解

2、大话设计模式

3、http://design-patterns.readthedocs.io/zh_CN/latest/index.html 图文并茂!


  • 简单工厂、工厂方法、抽象工厂

using namespace std;

/* Simple Factory */
class AbstractProduct
{
public:
    AbstractProduct(){}
    virtual ~AbstractProduct(){}
    virtual void use() = 0;
};

class ProductA : public AbstractProduct
{
public:
    ProductA(){}
    ~ProductA(){}
    void use()
    {
        cout << "use A" << endl;
    }
};

class ProductB : public AbstractProduct
{
public:
    ProductB(){}
    ~ProductB(){}
    void use()
    {
        cout << "use B" << endl;
    }
};

class SimpleFactory
{
public:
    SimpleFactory(){}
    ~SimpleFactory(){}
    AbstractProduct *createProduct(const string &type)
    {
        if(type == "A")
        {
            return new ProductA();
        }
        else if(type == "B")
        {
            return new ProductB();
        }
        else
        {
            return nullptr;
        }
    }
};

/* Factory Method */
class IProduct
{
public:
    IProduct(){}
    virtual ~IProduct(){}

    virtual void use() = 0;
};

class ConcreteProductA : public IProduct
{
public:
    ConcreteProductA(){}
    ~ConcreteProductA(){}

    void use()
    {
        cout << "use A" << endl;
    }
};

class ConcreteProductB : public IProduct
{
public:
    ConcreteProductB(){}
    ~ConcreteProductB(){}

    void use()
    {
        cout << "use B" << endl;
    }
};

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

    virtual IProduct *createProduct() = 0;
};

class ConcreteFactoryA : public IFactory
{
public:
    IProduct *createProduct()
    {
        return new ConcreteProductA();
    }
};

class ConcreteFactoryB : public IFactory
{
public:
    IProduct *createProduct()
    {
        return new ConcreteProductB();
    }
};

/* Abstract Factory */
class AbstractProduct;

class ConcreteProductA1 : public AbstractProduct
{
public:
    ConcreteProductA1(){}
    ~ConcreteProductA1(){}
    void use()
    {
        cout << "use A1" << endl;
    }
};

class ConcreteProductB1 : public AbstractProduct
{
public:
    ConcreteProductB1(){}
    ~ConcreteProductB1(){}
    void use()
    {
        cout << "use B1" << endl;
    }
};

class ConcreteProductA2 : public AbstractProduct
{
public:
    ConcreteProductA2(){}
    ~ConcreteProductA2(){}
    void use()
    {
        cout << "use A2" << endl;
    }
};

class ConcreteProductB2 : public AbstractProduct
{
public:
    ConcreteProductB2(){}
    ~ConcreteProductB2(){}
    void use()
    {
        cout << "use B2" << endl;
    }
};

class AbstractFactory
{
public:
    virtual AbstractProduct *createProductA() = 0;
    virtual AbstractProduct *createProductB() = 0;
};

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

    AbstractProduct *createProductA()
    {
        return new ConcreteProductA1();
    }

    AbstractProduct *createProductB()
    {
        return new ConcreteProductB1();
    }
};

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

    AbstractProduct *createProductA()
    {
        return new ConcreteProductA2();
    }

    AbstractProduct *createProductB()
    {
        return new ConcreteProductB2();
    }
};

int main()
{
    /* Simple Factory test code */
    SimpleFactory *sfc = new SimpleFactory();
    AbstractProduct *abspd = sfc->createProduct("A");
    abspd->use();

    /* Factory Method test code */
    IFactory *faca = new ConcreteFactoryA();
    IProduct *proa = faca->createProduct();
    proa->use();
    delete faca;
    delete proa;

    IFactory *facb = new ConcreteFactoryB();
    IProduct *prob = facb->createProduct();
    prob->use();
    delete facb;
    delete prob;

    /* Abstract Factory test code */
    AbstractFactory *fc = new ConcreteFactory1();
    AbstractProduct *pda = fc->createProductA();
    AbstractProduct *pdb = fc->createProductB();

    pda->use();
    pdb->use();

    delete fc;
    delete pda;
    delete pdb;

    AbstractFactory *fc2 = new ConcreteFactory2();
    AbstractProduct *pda2 = fc2->createProductA();
    AbstractProduct *pdb2 = fc2->createProductB();

    pda2->use();
    pdb2->use();

    delete fc2;
    delete pda2;
    delete pdb2;

    return 0;
}



你可能感兴趣的:(工厂方法抽象工厂简单工厂)