-
Notifications
You must be signed in to change notification settings - Fork 11
/
GoF2.h
112 lines (86 loc) · 2.06 KB
/
GoF2.h
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
#pragma once
#include <iostream>
namespace ProxyPattern::GoF2
{
class Point {
public:
static Point Zero;
bool operator==(Point&) { return false; }
};
Point Point::Zero = Point();
class Event;
class Graphic {
public:
virtual ~Graphic() {}
virtual void Draw(const Point& at) = 0;
virtual void HandleMouse(Event& event) = 0;
virtual const Point& GetExtent() = 0;
virtual void Load(std::istream& from) = 0;
virtual void Save(std::ostream& to) = 0;
protected:
Graphic() {}
};
class Image : public Graphic {
public:
Image(const char* file) {} // loads image from a file
virtual ~Image() {}
virtual void Draw(const Point& at) {}
virtual void HandleMouse(Event& event) {}
virtual const Point& GetExtent() { return *(new Point()); }
virtual void Load(std::istream& from) {}
virtual void Save(std::ostream& to) {}
private:
// ...
};
class ImageProxy : public Graphic {
public:
ImageProxy(const char* imageFile);
virtual ~ImageProxy() {}
virtual void Draw(const Point& at);
virtual void HandleMouse(Event& event);
virtual const Point& GetExtent();
virtual void Load(std::istream& from);
virtual void Save(std::ostream& to);
protected:
Image* GetImage();
private:
Image* _image;
Point _extent;
char* _fileName;
};
ImageProxy::ImageProxy(const char* fileName) {
// _fileName = strdup(fileName);
_extent = Point::Zero; // don't know extent yet
_image = 0;
}
Image* ImageProxy::GetImage() {
if (_image == 0) {
_image = new Image(_fileName);
}
return _image;
}
const Point& ImageProxy::GetExtent() {
if (_extent == Point::Zero) {
_extent = GetImage()->GetExtent();
}
return _extent;
}
void ImageProxy::Draw(const Point& at) {
GetImage()->Draw(at);
}
void ImageProxy::HandleMouse(Event& event) {
GetImage()->HandleMouse(event);
}
void ImageProxy::Save(std::ostream& to) {
// to << _extent << _fileName;
}
void ImageProxy::Load(std::istream& from) {
// from >> _extent >> _fileName;
}
class TextDocument {
public:
TextDocument() {}
void Insert(Graphic*) {}
// ...
};
}