QtExamples/Studio/DesignPatterns/FactoryMethod.h

94 lines
2.5 KiB
C
Raw Normal View History

/*******************************************************************
* Copyright (c) 2022~2023 XMuli All rights reserved.
* GitHub: https://github.com/XMuli
* Description: (Factory Method)
* IProduct IFactory
* Factory new Product
* See: https://blog.csdn.net/silangquan/article/details/20492293
******************************************************************/
#pragma once
#include <iostream>
using namespace std;
/********************************************************
Product Factory
********************************************************/
class IProduct
{
public:
virtual ~IProduct() = default;
virtual void doSomeThing() = 0;
};
class AProduct : public IProduct
{
public:
virtual ~AProduct() = default;
virtual void doSomeThing() override {
cout << "this is A Product, do some thing..." << endl;
};
};
class BProduct : public IProduct
{
public:
virtual ~BProduct() = default;
virtual void doSomeThing() override {
cout << "this is B Product, do some thing..." << endl;
};
};
class CProduct : public IProduct
{
public:
virtual ~CProduct() = default;
virtual void doSomeThing() override {
cout << "this is C Product, do some thing..." << endl;
};
};
class IFactory
{
public:
virtual IProduct* creatProduct() = 0;
};
class AFactory : public IFactory
{
public:
virtual ~AFactory() = default;
virtual IProduct* creatProduct() override { return new AProduct(); };
};
class BFactory : public IFactory
{
public:
virtual ~BFactory() = default;
virtual IProduct* creatProduct() override { return new BProduct(); };
};
class CFactory : public IFactory
{
public:
virtual ~CFactory() = default;
virtual IProduct* creatProduct() override { return new CProduct(); };
};
//int main()
//{
// AFactory aFactory;
// BFactory bFactory;
// CFactory cFactory;
// aFactory.creatProduct()->doSomeThing();
// bFactory.creatProduct()->doSomeThing();
// cFactory.creatProduct()->doSomeThing();
// return 0;
//}
/*****************************打印结果*******************************
this is A Product, do some thing...
this is B Product, do some thing...
this is C Product, do some thing...
******************************************************************/