设计模式 简单工厂模式 C++

本文从WordPress迁移而来, 查看全部WordPress迁移文章

根据用户的输入,创建 身体受伤险,汽车损坏险,其他事故险

——————– 保险 ———————

Insurance.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef INSURANCE_H
#define INSURANCE_H

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

//保险,抽象类
class Insurance{
public:
Insurance();
virtual ~Insurance();
virtual string showInfo()const = 0;
};

#endif

BodyInjuryInsurance.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef BODYINJURYINSURANCE
#define BODYINJURYINSURANCE

#include "Insurance.h"

//身体受伤险,具体类,实现了纯虚函数 showInfo()
class BodyInjuryInsurance : public Insurance{
public:
BodyInjuryInsurance();
virtual ~BodyInjuryInsurance();
virtual string showInfo()const;
};

#endif

CarDamageInsurance.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef CARDAMAGEINSURANCE_H
#define CARDAMAGEINSURANCE_H

#include "Insurance.h"

//汽车损坏险,具体类,实现了纯虚函数 showInfo()
class CarDamageInsurance : public Insurance {
public:
CarDamageInsurance();
virtual ~CarDamageInsurance();
virtual string showInfo()const;
};

#endif

OtherAccidentsInsurance.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef OTHERACCIDENTSINSURANCE_H
#define OTHERACCIDENTSINSURANCE_H

#include "Insurance.h"

//其他事故险,具体类,实现了纯虚函数 showInfo()
class OtherAccidentsInsurance : public Insurance {
public:
OtherAccidentsInsurance();
virtual ~OtherAccidentsInsurance();
virtual string showInfo()const;
};

#endif

Insurance.cpp

1
2
3
4
#include "Insurance.h"

Insurance::Insurance(){}
Insurance::~Insurance(){}

BodyInjuryInsurance.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "BodyInjuryInsurance.h"

BodyInjuryInsurance::BodyInjuryInsurance(){
std::cout << "创建了一个身体受伤险" << std::endl;
}
BodyInjuryInsurance::~BodyInjuryInsurance(){
std::cout << "销毁了一个身体受伤险" << std::endl;
}
string BodyInjuryInsurance::showInfo()const{
return "这是一个身体受伤险!";
}

CarDamageInsurance.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "CarDamageInsurance.h"

CarDamageInsurance::CarDamageInsurance(){
cout << "创建了一个汽车损坏险" << endl;
}
CarDamageInsurance::~CarDamageInsurance(){
cout << "销毁了一个汽车损坏险" << endl;
}
string CarDamageInsurance::showInfo()const{
return "这是一个汽车损坏险!";
}

OtherAccidentsInsurance.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "OtherAccidentsInsurance.h"

OtherAccidentsInsurance::OtherAccidentsInsurance(){
cout << "创建了一个其他事故险" << endl;
}
OtherAccidentsInsurance::~OtherAccidentsInsurance(){
cout << "销毁了一个其他事故险" << endl;
}
string OtherAccidentsInsurance::showInfo()const{
return "这是一个其他事故险";
}

——————– 保险 ———————

——————– 简单工厂 ——————–

InsuranceSimpleFactory.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef INSURANCESIMPLEFACTORY_H
#define INSURANCESIMPLEFACTORY_H


#include "BodyInjuryInsurance.h"
#include "CarDamageInsurance.h"
#include "OtherAccidentsInsurance.h"

//简单工厂,createInsurance(int) 设计为 静态成员函数,不必实例化一个工厂对象,直接由 类名 调用即可
class InsuranceSimpleFactory{
public:
InsuranceSimpleFactory();
~InsuranceSimpleFactory();
static Insurance* createInsurance(int);
};

#endif

InsuranceSimpleFactory.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "InsuranceSimpleFactory.h"

//InsuranceSimpleFactory::InsuranceSimpleFactory(){}
//InsuranceSimpleFactory::~InsuranceSimpleFactory(){}

Insurance* InsuranceSimpleFactory::createInsurance(int choose){
if(choose == 1) return new BodyInjuryInsurance();
else if(choose == 2) return new CarDamageInsurance();
else return new OtherAccidentsInsurance();
//else cout << "错误的选择" << endl;
}

——————– 简单工厂 ——————–

MyApp.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef MYAPP_H
#define MYAPP_H

#include "InsuranceSimpleFactory.h"

//客户端
class MyApp{
public:
MyApp();
~MyApp();
void start();
};

#endif

MyApp.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "MyApp.h"

MyApp::MyApp(){}
MyApp::~MyApp(){}

void MyApp::start(){
string str;
int choose;
while(true){
cout << "输入1:身体受伤险" << endl;
cout << "输入2:汽车受损险" << endl;
cout << "输入3:其他事故险" << endl;
cout << "输入end:退出程序" << endl;
cin >> str;
if(str == "1") choose = 1;
else if(str == "2") choose = 2;
else if(str == "3") choose = 3;
else if(str == "end") break;
else { cout << "输入有误" << endl; continue; }
Insurance* obj = InsuranceSimpleFactory::createInsurance(choose);
cout << obj->showInfo() << endl;
delete obj;
}
}