QtExamples/Studio/DesignPatterns/AbstractFactory.h

141 lines
3.9 KiB
C
Raw Normal View History

/*******************************************************************
* Copyright (c) 2022~2023 XMuli All rights reserved.
* GitHub: https://github.com/XMuli
* Description: (Abstract Factory)
* Up产品 up1up2 Down down1down2
* Up + Down
* AFactory up1 + down1 BFactory up2 + down2
* CFactory up1 + down2
* See: https://blog.csdn.net/silangquan/article/details/20492293
******************************************************************/
#pragma once
#include <iostream>
using namespace std;
/***************************************************************************
Factory product 1 ()
***************************************************************************/
class IUpProduct
{
public:
virtual ~IUpProduct() = default;
virtual void doSomeThing() = 0;
};
class Up1Product : public IUpProduct
{
public:
virtual ~Up1Product() = default;
virtual void doSomeThing() override {
cout << "this is 1 UpProduct, do some thing..." << endl;
}
};
class Up2Product : public IUpProduct
{
public:
virtual ~Up2Product() = default;
virtual void doSomeThing() override {
cout << "this is 2 UpProduct, do some thing..." << endl;
}
};
class IDownProduct
{
public:
virtual ~IDownProduct() = default;
virtual void doSomeThing() = 0;
};
class Down1Product : public IDownProduct
{
public:
virtual ~Down1Product() = default;
virtual void doSomeThing() override {
cout << "this is 1 DownProduct, do some thing..." << endl;
}
};
class Down2Product : public IDownProduct
{
public:
virtual ~Down2Product() = default;
virtual void doSomeThing() override {
cout << "this is 2 DownProduct, do some thing..." << endl;
}
};
class IFactory
{
public:
virtual ~IFactory() = default;
virtual IUpProduct* creatorUpProduct() = 0;
virtual IDownProduct* creatorDownProduct() = 0;
};
class AFactory : public IFactory
{
public:
virtual ~AFactory() = default;
virtual IUpProduct* creatorUpProduct() override {
return new Up1Product();
}
virtual IDownProduct* creatorDownProduct() override {
return new Down1Product();
}
};
class BFactory : public IFactory
{
public:
virtual ~BFactory() = default;
virtual IUpProduct* creatorUpProduct() override {
return new Up2Product();
}
virtual IDownProduct* creatorDownProduct() override {
return new Down2Product();
}
};
class CFactory : public IFactory
{
public:
virtual ~CFactory() = default;
virtual IUpProduct* creatorUpProduct() override {
return new Up1Product();
}
virtual IDownProduct* creatorDownProduct() override {
return new Down2Product();
}
};
//int main()
//{
// AFactory aFactory;
// aFactory.creatorUpProduct()->doSomeThing();
// aFactory.creatorDownProduct()->doSomeThing();
// cout << endl;
//
// BFactory bFactory;
// bFactory.creatorUpProduct()->doSomeThing();
// bFactory.creatorDownProduct()->doSomeThing();
// cout << endl;
//
// CFactory cFactory;
// cFactory.creatorUpProduct()->doSomeThing();
// cFactory.creatorDownProduct()->doSomeThing();
// cout << endl;
// return 0;
//}
/*****************************打印结果*******************************
this is 1 UpProduct, do some thing...
this is 1 DownProduct, do some thing...
this is 2 UpProduct, do some thing...
this is 2 DownProduct, do some thing...
this is 1 UpProduct, do some thing...
this is 2 DownProduct, do some thing...
******************************************************************/