工厂使用从文件加载时

问题描述 投票:0回答:1

我是相对新的C ++,虽然我有在Java中的一点经验。试图接近到下面的问题(这是一个更大的任务的一部分,从我的大学)

class myCustomer :public Customer {/.../}
class myFlight:public Flight {/.../}
class myEmployee :public Employee {/.../}
class myPlane :public Plane {/.../}

所以,客户是一个接口,并myCustomer与实现派生类中,这同样适用于其他地区。(我必须使用这种方式)

随后此(排序的),用于飞行公司数据的基础上的API;

class MyImplementation{
   //I want to link id's to an object so i can call object pointer using a given id
   map <string,myCustomer*> customers_map;
   map <string,myEmployee*> employees_map;
   map <string,myReservation*> reservations_map;
   map <string,myPlane*> planes_map;
   map <string,myFlight*> flights_map;
...
   //must implement this function
   virtual Customer* addCustomer(string full_name, int priority){
    if(!customers_loaded){
        load_customers();
    }
    auto curr_customer = new myCustomer(id,full_name,priority);
    customers_map.insert(pair<string,myCustomer*>(id,curr_customer));
    }



    void load_customers(){
        vector<string> parameters;
        string line;
        fstream customers_file;
        customers_file.open(CUSTOMER_INFO_PATH,fstream::out);
        //check if null is returned
        if (!customers_file.is_open()) {
            throw "Error:cannot open employees file";
        }
        //iterate over lines
        while(getline(customers_file,line)){
            //just a function to split a line from file to a vector               
            parameters=splitLine(line,SPLITTER);
            try{
                customer_from_string(parameters);
            }catch(...){
                //could not load current object 
                cout << "Could not load customer "<< parameters[0] <<
                "(line number:"<< lineNumber << ")" <<endl;
                continue;
            }
        }
        customers_file.close();
    }


    void customer_from_string(vector<string> parameters){
        string id = parameters[0];
        string name = parameters[1];
        int priority = stoi(parameters [2];
        auto curr_customer = new myCustomer(id,name,priority);
        customers_map.insert(pair<string,myCustomer*>(id,curr_customer));
    }


    void employee_from_string(vector<string>& parameters,
                                map<string, string>& toLink) {
        //get parameters
        string employee_id = parameters[0];
        Jobs job = string_to_job(parameters[1]);
        int seniority = stoi(parameters[2]);
        int year = stoi(parameters[3]);
        string employer_id = parameters[4];
        //check if employee_id is legal
        if (employer_id.empty()) {
            throw "Employer ID cannot be an empty argument";
        }
        auto curr_employee = new myEmployee(employee_id, job, seniority,year);
    //add to employee map
    employees_map.insert(pair<string, myEmployee *>(employee_id, curr_employee));


    }
        /** more code**/

所以我试着从一个文本文件中读取参数,并创建使用匹配像customer_from_string功能指定的对象,employee_from_string,我必须这样做对其他类(飞机,预订机票...)每个人都有自己的构造不同参数个数。这些类之间的唯一联系就是它们都实现具有功能的ID接口:

virtual string getID() = 0;

试图避免下面的代码重复(所以我不会创建为每个对象负荷功能):

void load_employees() {
    /** same code as load_customers**/
    //iterate over lines
    while (getline(employees_file, line)) {
        //as before
        parameters = splitLine(line, SPLITTER);
        try {
            /*the only change - it has different implementation than customer_from_string*/
            employee_from_string(parameters); 
        } catch (...) {
            /** print error **/
        }
    }
    /** as before **/ 
}

我了解我必须解决这个使用工厂设计模式(因为我需要动态地创建在文本文件中列出的对象,他们每个我打开程序时加载到数据库),所以我想过实现以下:

class Factory {
virtual void create(vector<string>& parameters,
                    map<string,Factory*>&id_map,/*..more parameters*/)=0;
 }

而让负载方法决定哪些对象来创建(也许发送焦炭用作标识符),使用上述方法创建,让每类实现工厂接口和比使用创建函数,如下所示:

//Implementation for customer
    void create(vector<string>& parameters,map<Factory *, string>&
                 toLink,map<string,Factory*>& id_map){
        string id = parameters[0];
         string name = parameters[1];
         int priority = stoi(parameters [parameters.size()-1]);
         auto curr_customer = new myCustomer(id,name,priority);
         id_map.insert(pair<string,Factory *>(id,curr_customer));
    }

所以,我想我需要改变映射到

 map <string,Factory *> objectName_map;

现在即时通讯有点卡住因为地图holdes一个指向工厂对象,我想使用的地图ID的做到以下几点:

virtual Customer* getCustomer(string id){   //must implement this function
    if(!customers_loaded){
        load('Customers');
    }
    return customers_map.find(id)->second;
}

我不能调用任何内部函数为每个对象后,可以说:

void setEmployer(Employee* employer){//...//}

由于地图是拿着工厂*。我试图保持的地图,因为它们是不可能,但使用它们的创造功能,因为它不能在myCustomer *对象转换为厂*。林有点失去了,试图找到解决方案在线,但没有运气,但我觉得实现这个错误IM。预先感谢任何帮助!

*编辑后使问题更清晰

c++ design-patterns factory
1个回答
0
投票

所以,你真正需要的是一个“工厂”模板:

template<typename T>
pair<string, shared_ptr<T>>
from_string_vector(const vector<string>& parameters);

有明确的专业化喜欢:

template<>
pair<string, shared_ptr<myCustomer>>
from_string_vector(const vector<string>& parameters) {
    string id = parameters[0];
    string name = parameters[1];
    int priority = stoi(parameters [2]);
    return make_pair(id, make_shared<myCustomer>(
         id, name, priority));
}

和通用功能,如:

template<typename T>
void load_database(map<string, shared_ptr<T>>& objectMap,
                   const string& filename) {
    vector<string> parameters;
    string line;
    ifstream file(filename);
    while (getline(file, line)) {
        parameters = splitLine(line, SPLITTER);
        objectMap.insert(from_string_vector<T>(parameters));
    }
}

然后调用这个函数6次,所有的地图和文件名?

© www.soinside.com 2019 - 2024. All rights reserved.