-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathprototype.cpp
90 lines (80 loc) · 1.66 KB
/
prototype.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
#include <iostream>
#include <string>
using namespace std;
// Default Classes
class Device
{
public:
virtual void send(string data) = 0;
virtual int setSpeed(int value) = 0;
virtual int getSpeed() = 0;
};
class Wifi : public Device
{
public:
int speed;
Wifi()
{
speed = 100;
}
int setSpeed(int value)
{
speed = value;
}
int getSpeed()
{
return speed;
}
void send(string data)
{
cout<<"Sent By Wifi: "<<data<<" Speed: "<<speed<<endl;
}
};
class Bluetooth : public Device
{
public:
int speed;
Bluetooth(){
speed = 8;
}
int setSpeed(int value)
{
speed = value;
}
int getSpeed()
{
return speed;
}
void send(string data)
{
cout<<"Sent By Bluetooth: "<<data<<" Speed: "<<speed<<endl;
}
};
// Prototype Started
// Problem: Clone the old object
// Solution: Clone function
class Prototype
{
public:
virtual Device* clone(Device *oldOneObject) = 0;
};
class BluetoothPrototype : public Prototype
{
public:
Device* clone(Device* oldOneObject)
{
Device* newOneObject = new Bluetooth;
newOneObject->setSpeed(oldOneObject->getSpeed());
return newOneObject;
}
};
// Main Usage
int main(){
Device* bluetoothObject = new Bluetooth;
bluetoothObject->setSpeed(20);
bluetoothObject->send("First Object");
Prototype* prototypeObject = new BluetoothPrototype;
Device* clonedBluetoothObject = prototypeObject->clone(bluetoothObject);
clonedBluetoothObject->send("Cloned Object creatd");
cout<<"If speed is same in two objects, it means clone worked successfully"<<endl;
}