-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathabstract-factory.cpp
64 lines (56 loc) · 968 Bytes
/
abstract-factory.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
#include <iostream>
#include <string>
using namespace std;
// Default Classes
class Device
{
public:
virtual void send(string data) = 0;
};
class Wifi : public Device
{
public:
void send(string data)
{
cout<<"Sent By Wifi: "<<data<<endl;
}
};
class Bluetooth : public Device
{
public:
void send(string data)
{
cout<<"Sent By Bluetooth: "<<data<<endl;
}
};
// Abstract Factory Started
// Problem: Multiple if every function for handle the environment
// Solution: Build Abstract Factory and get method
class AbstractFactory
{
public:
virtual Device* get() = 0;
};
class DeviceAbstractFactory : public AbstractFactory
{
public:
Device* get()
{
if (true) // Confitions
{
return new Wifi;
}
else
{
return new Bluetooth;
}
}
};
// Main Usage
int main()
{
AbstractFactory *abstractFactory = new DeviceAbstractFactory;
Device* device;
device = abstractFactory->get();
device->send("Abstract Factory Design Pattern Worked.");
}