forked from suhaib-saqib/btp600-w18
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.cpp
84 lines (77 loc) · 1.86 KB
/
strategy.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
#include <iostream>
#include <string>
using namespace std;
class Image;
//this is the abstract base class that forms
//the interface for the strategy
class Exporter{
public:
//this function is implemented in each
//of the different exporters
virtual void writeToFile(const char* fname, Image& s)=0;
};
class PNGExporter:public Exporter{
public:
virtual void writeToFile(const char* fname, Image& s);
};
class JPEGExporter:public Exporter{
public:
virtual void writeToFile(const char* fname, Image& s);
};
class GIFExporter:public Exporter{
public:
virtual void writeToFile(const char* fname, Image& s);
};
//this is an example of a context. It contains
//the information needed by the algorithm
class Image{
string pixels_;
Exporter* exp_;
public:
Image(){
pixels_="the picture";
}
string pixels() {return pixels_;}
void setExporter(Exporter* e){
exp_=e;
}
void exportImage(){
if(exp_)
exp_->writeToFile("myPicture", *this);
}
};
void PNGExporter::writeToFile(const char* fname, Image& s){
cout << "printing to: " << fname << ".png" << endl;
cout << s.pixels() << endl;
}
void JPEGExporter::writeToFile(const char* fname, Image& s){
cout << "printing to: " << fname << ".jpg" << endl;
cout << "compressing" << endl;
cout << s.pixels() << endl;
}
void GIFExporter::writeToFile(const char* fname, Image& s){
cout << "printing to: " << fname << ".gif" << endl;
cout << s.pixels() << endl;
}
//at run time, the appropriate stategy can be set (and even changed)
int main(void){
Image img;
Exporter* e;
int choice;
cout << "would you like to print to png, jpeg, or gif format?" << endl;
cout << "1) png" << endl;
cout << "2) jpeg" << endl;
cout << "3) gif" << endl;
cin >> choice;
if(choice == 1){
e=new PNGExporter();
}
else if(choice==2){
e=new JPEGExporter();
}
else if(choice==3){
e=new GIFExporter();
}
img.setExporter(e);
img.exportImage();
}