forked from preytaren/DesignPatternInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
46 lines (36 loc) · 1.29 KB
/
proxy.py
File metadata and controls
46 lines (36 loc) · 1.29 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
# -*- encoding=utf-8 -*-
"""
proxy 模式
一个常见的用法就是延迟实际的求值操作
"""
class Rectangle(object):
def __init__(self, height, width):
print 'Create a Rectangle'
self._height = height
self._width = width
def draw(self):
print 'A rectangle of size: {height}x{width} = {size}'.format(height=self._height,
width=self._width,
size=self._width*self._height)
class RectangleProxy(object):
def __init__(self, height, width):
print 'Create a Proxy'
self.height = height
self.width = width
self._last_height = height
self._last_width = width
self._instance = None
def draw(self):
if not self._instance \
or self._last_width != self.width \
or self._last_height != self.height:
self._instance = Rectangle(self.height, self.width)
self._last_height = self.height
self._last_width = self.width
self._instance.draw()
if __name__ == '__main__':
height, width = 10, 20
rectangle = RectangleProxy(height, width)
rectangle.draw()
rectangle.width = 30
rectangle.draw()