How to implement Factory pattern?

孤人 提交于 2019-12-12 02:47:14

问题


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

回答1:


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




回答2:


You just had to declare the 2 classes that derive from TaxInterface above the TaxFactory class so they would be visible to it. I've also made a few recommendations inside main()...

#include<iostream>
#include<vector>
using namespace std;

class TaxInterface
{
public:
    virtual int calculate_tax(int price, int quantity) = 0;
};

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;
    }
};

class TaxFactory
{
public:
    // Factory Method
    static TaxInterface *callManager(int imported)
    {
        if (imported == 0)
            return new NormalTaxManager;
        else
            return new ImportedTaxManager;
    }
};

int main()
{
    vector<TaxInterface*> interfaces;
    vector<int> taxTypes = { 0, 0, 1, 0, 1, 1, 1, 0, 1 };

    for (vector<int>::iterator i = taxTypes.begin(); i != taxTypes.end(); i++) {
        interfaces.push_back(TaxFactory::callManager(*i));
    }

    for (int i = 0; i < interfaces.size(); i++) {
        interfaces[i]->calculate_tax(100, 2);
    }   

    // system("pause");
    return 0;
}


来源:https://stackoverflow.com/questions/33462867/how-to-implement-factory-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!