如何实现工厂模式? [英] How to implement Factory pattern?
问题描述
我正在尝试实现工厂类和接口。但我收到以下错误消息。我创建了一个工厂类,它决定返回NormalTaxManager或ImportedTaxManager的类。我使用接口提供了抽象。
#include< iostream>
#include< vector>
using namespace std;
class TaxInterface
{
public:
virtual int calculate_tax(int price,int quantity)= 0;
};
class TaxFactory
{
public:
//工厂方法
静态TaxInterface * callManager(int import)
{
if(imported == 0)
返回新的NormalTaxManager;
else
返回新的ImportedTaxManager;
}
};
class NormalTaxManager:public TaxInterface
{
public:
virtual int calculate_tax(int price,int quantity)
{
cout< < NormalTaxManager\\\
;
价格=数量*价格* 10/100;
退货价格;
}
};
class ImportedTaxManager:public TaxInterface
{
public:
virtual int calculate_tax(int price,int quantity)
{
cout< < ImportedTaxManager\\\
;
价格=数量*价格* 5/100;
退货价格;
}
};
int main()
{
TaxFactory f;
TaxInterface * a = f.callManager(1);
a-> calculate_tax(100,2);
// int price = TaxInterface :: callManager(1) - > calculate_tax(100,2);
}
问题:
错误:'NormalTaxManager'没有命名类型
错误:'ImportedTaxManager'没有命名类型
您需要声明 NormalTaxManager
和 ImportedTaxManager
在 TaxInterface
之前。
你还需要做相反的事情。
为了解决(经典)C ++循环引用问题,你需要在.cpp和.h文件之间拆分代码
将 TaxInterface
设为抽象的,并且在自己的文件中没有实现:TaxInterface.h。
例如,在两个文件中拆分 ImportedTaxManager
:
.h
#pragma一次
#includetaxinterface.h
class ImportedTaxManager:public TaxInterface
{
public:
virtual int calculate_tax(int price,int quantity);
};
.cpp
#includestdafx.h
#include< iostream>
using namespace std;
#includeImportedTaxManager.h
int ImportedTaxManager :: calculate_tax(int price,int quantity)
{
cout<< ImportedTaxManager\\\
;
价格=数量*价格* 5/100;
退货价格;
}
如果你聪明,你可以保存一些文件。
但是更容易维护在标题(.h)和实现(.cpp)之间拆分的代码。
因为C ++需要声明所使用的所有内容,所以通常可以通过在.h和.cpp之间进行拆分来解决循环引用。
完整的工作解决方案: http://1drv.ms/1Pe25SQ
问候
I am trying to implement factory class and interface. But i am getting the below error message. I have created a factory class which decides which class to return NormalTaxManager or ImportedTaxManager. I have provided the abstraction using interface.
#include<iostream>
#include<vector>
using namespace std;
class TaxInterface
{
public:
virtual int calculate_tax(int price,int quantity)=0;
};
class TaxFactory
{
public:
// Factory Method
static TaxInterface *callManager(int imported)
{
if (imported == 0)
return new NormalTaxManager;
else
return new ImportedTaxManager;
}
};
class NormalTaxManager: public TaxInterface
{
public:
virtual int calculate_tax(int price,int quantity)
{
cout << "NormalTaxManager\n";
price=quantity*price*10/100;
return price;
}
};
class ImportedTaxManager: public TaxInterface
{
public:
virtual int calculate_tax(int price,int quantity)
{
cout << "ImportedTaxManager\n";
price=quantity*price*5/100;
return price;
}
};
int main()
{
TaxFactory f;
TaxInterface *a = f.callManager(1);
a->calculate_tax(100,2);
// int price=TaxInterface::callManager(1)->calculate_tax(100,2);
}
Problem:
error: ‘NormalTaxManager’ does not name a type
error: ‘ImportedTaxManager’ does not name a type
You need to declare NormalTaxManager
and ImportedTaxManager
before TaxInterface
.
And you also need to do the reverse.
In order to fix that (classical) C++ circular reference problem, you need to split your code between .cpp and .h files
Put TaxInterface
that is abstract and has no implementation in a file of its own : TaxInterface.h.
For example, split ImportedTaxManager
in two files :
.h
#pragma once
#include "taxinterface.h"
class ImportedTaxManager : public TaxInterface
{
public:
virtual int calculate_tax(int price, int quantity);
};
.cpp
#include "stdafx.h"
#include<iostream>
using namespace std;
#include "ImportedTaxManager.h"
int ImportedTaxManager::calculate_tax(int price, int quantity)
{
cout << "ImportedTaxManager\n";
price = quantity*price * 5 / 100;
return price;
}
If you re "clever" you can "save" some files.
But it is more easy to maintain code that is split between headers (.h) and implementation (.cpp).
Because C++ needs declarations of everything that is used, you often get circular references that can be solved by spliting between .h and .cpp
Full working solution : http://1drv.ms/1Pe25SQ
Regards
这篇关于如何实现工厂模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!