-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactory-method.cpp
More file actions
60 lines (49 loc) · 1.44 KB
/
factory-method.cpp
File metadata and controls
60 lines (49 loc) · 1.44 KB
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
#include <iostream>
#include <string>
class Button {
public:
virtual void paint(void) = 0;
};
class OSXButton: public Button {
public:
void paint(void) { std::cout << "OSX button" << std::endl; }
};
class WindowsButton: public Button {
public:
void paint(void) {std::cout << "Windows button" << std::endl; }
};
class IPhoneButton: public Button {
public:
void paint(void) { std::cout << "IPhone button" << std::endl; }
};
class ButtonFactory {
public:
virtual Button* createButton(std::string&& p_type) {
if ( p_type.compare("Windows") == 0 ) { return new WindowsButton; }
else if ( p_type.compare("OSX" ) == 0 ) { return new OSXButton; }
return nullptr;
}
};
class Factory: public ButtonFactory {
virtual Button* createButton(std::string&& p_type) {
if ( p_type.compare("Windows") == 0 ) { return new WindowsButton; }
else if ( p_type.compare("OSX" ) == 0 ) { return new OSXButton; }
else if ( p_type.compare("Iphone" ) == 0 ) { return new IPhoneButton; }
return nullptr;
}
};
int main()
{
ButtonFactory* factory = new Factory;
Button* button = nullptr;
button = factory->createButton("OSX");
button -> paint();
delete button;
button = factory->createButton("Windows");
button -> paint();
delete button;
button = factory->createButton("Iphone");
button -> paint();
delete button;
return 0;
}