1. 抽象工厂针对的是产品族,而不是产品等级
  2. 产品族:同一产地或者同一厂商,功能不同
  3. 产品等级:功能相同,产地或者厂商不同
  4. 对于增加产品族,符合开闭原则
  5. 对于增加产品等级,不符合开闭原则

抽象工厂模式

// 抽象工厂模式
class AbstractApple{
public:
    virtual void showName() = 0;
};
class AbstractBanana{
public:
    virtual void showName() = 0;
};
class AbstractPear{
public:
    virtual void showName() = 0;
};
class ChinaApple : public AbstractApple{
public:
    void showName() override{
        cout << "我是中国苹果!" << endl;
    }
};
class ChinaBanana : public AbstractBanana{
public:
    void showName() override{
        cout << "我是中国香蕉!" << endl;
    }
};
class ChinaPear : public AbstractPear{
public:
    void showName() override{
        cout << "我是中国鸭梨!" << endl;
    }
};
class USAApple : public AbstractApple{
public:
    void showName() override{
        cout << "我是美国苹果!" << endl;
    }
};
class USABanana : public AbstractBanana{
public:
    void showName() override{
        cout << "我是美国香蕉!" << endl;
    }
};
class USAPear : public AbstractPear{
public:
    void showName() override{
        cout << "我是美国鸭梨!" << endl;
    }
};
class JapanApple : public AbstractApple{
public:
    void showName() override{
        cout << "我是日本苹果!" << endl;
    }
};
class JapanBanana : public AbstractBanana{
public:
    void showName() override{
        cout << "我是日本香蕉!" << endl;
    }
};
class JapanPear : public AbstractPear{
public:
    void showName() override{
        cout << "我是日本鸭梨!" << endl;
    }
};
class AbstractFactory{
public:
    virtual AbstractApple* CreateApple() = 0;
    virtual AbstractBanana* CreateBanana() = 0;
    virtual AbstractPear* CreatePear() = 0;
};
class ChinaFactory : public AbstractFactory{
public:
    AbstractApple* CreateApple() override{
        return new ChinaApple;
    }
    AbstractBanana* CreateBanana() override{
        return new ChinaBanana;
    }
    AbstractPear* CreatePear() override{
        return new ChinaPear;
    }
};
class USAFactory : public AbstractFactory{
public:
    AbstractApple* CreateApple() override{
        return new USAApple;
    }
    AbstractBanana* CreateBanana() override{
        return new USABanana;
    }
    AbstractPear* CreatePear() override{
        return new USAPear;
    }
};
class JapanFactory : public AbstractFactory{
public:
    AbstractApple* CreateApple() override{
        return new JapanApple;
    }
    AbstractBanana* CreateBanana() override{
        return new JapanBanana;
    }
    AbstractPear* CreatePear() override{
        return new JapanPear;
    }
};
int main()
{
    AbstractFactory* factory = nullptr;
    AbstractApple* apple = nullptr;
    AbstractBanana* banana = nullptr;
    AbstractPear* pear = nullptr;
    // 中国
    factory = new ChinaFactory;
    apple = factory->CreateApple();
    apple->showName();
    banana = factory->CreateBanana();
    banana->showName();
    pear = factory->CreatePear();
    pear->showName();
    delete pear;
    delete banana;
    delete apple;
    delete factory;
    // 美国
    factory = new USAFactory;
    apple = factory->CreateApple();
    apple->showName();
    banana = factory->CreateBanana();
    banana->showName();
    pear = factory->CreatePear();
    pear->showName();
    delete pear;
    delete banana;
    delete apple;
    delete factory;
    // 日本
    factory = new JapanFactory;
    apple = factory->CreateApple();
    apple->showName();
    banana = factory->CreateBanana();
    banana->showName();
    pear = factory->CreatePear();
    pear->showName();
    delete pear;
    delete banana;
    delete apple;
    delete factory;
    return 0;
}