-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassFactory.cpp
149 lines (131 loc) · 3.42 KB
/
ClassFactory.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* \file ClassFactory.cpp
* \brief Class factory pattern is an object/method for creating other objects
*
* The class factory creates and returns new objects by instantiating various classes
* (through the use of constructors). In most cases, a class factory has at most one method/function
* that is given some type of incoming data and returns a new object (or pointer) to what was created
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
class ICar
{
public:
ICar() = default;
virtual ~ICar() = default;
virtual void printName() = 0;
};
//-------------------------------------------------------------------------------------------------
class Ford :
public ICar
{
public:
void printName() override
{
std::cout << "Ford" << std::endl;
}
};
//-------------------------------------------------------------------------------------------------
class Toyota :
public ICar
{
public:
void printName() override
{
std::cout << "Toyota" << std::endl;
}
};
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
class IEngine
{
public:
IEngine() = default;
virtual ~IEngine() = default;
virtual void printPower() = 0;
};
//-------------------------------------------------------------------------------------------------
class FordEngine :
public IEngine
{
public:
void printPower() override
{
std::cout << "Ford Engine 4.4" << std::endl;
}
};
//-------------------------------------------------------------------------------------------------
class ToyotaEngine :
public IEngine
{
public:
void printPower() override
{
std::cout << "Toyota Engine 3.2" << std::endl;
}
};
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
class ICarFactory
{
public:
virtual ~ICarFactory() = default;
virtual ICar* createCar() = 0;
virtual IEngine* createEngine() = 0;
};
//-------------------------------------------------------------------------------------------------
class FordFactory :
public ICarFactory
{
public:
ICar* createCar() override
{
return new Ford();
}
IEngine* createEngine() override
{
return new FordEngine();
}
};
//-------------------------------------------------------------------------------------------------
class ToyotaFactory :
public ICarFactory
{
public:
ICar* createCar() override
{
return new Toyota();
}
IEngine* createEngine() override
{
return new ToyotaEngine();
}
};
//-------------------------------------------------------------------------------------------------
void use(ICarFactory *f)
{
ICar *myCar = f->createCar();
IEngine *myEngine = f->createEngine();
myCar->printName();
myEngine->printPower();
delete myCar;
delete myEngine;
}
//-------------------------------------------------------------------------------------------------
int main()
{
ToyotaFactory toyotaFactory;
FordFactory fordFactory;
use(&toyotaFactory);
use(&fordFactory);
return 0;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
Toyota
Toyota Engine 3.2
Ford
Ford Engine 4.4
#endif