-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path8_proxy.py
More file actions
62 lines (43 loc) · 1.27 KB
/
Copy path8_proxy.py
File metadata and controls
62 lines (43 loc) · 1.27 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# coding : utf-8
# create by ctz on 2017/5/26
from abc import ABCMeta, abstractmethod
class Subject(metaclass=ABCMeta):
@abstractmethod
def get_content(self):
pass
class RealSubject(Subject):
def __init__(self, filename):
self.filename = filename
print("读取%s文件内容"%filename)
f = open(filename)
self.content = f.read()
f.close()
def get_content(self):
return self.content
def set_content(self, content):
f = open(self.filename, 'w')
f.write(content)
f.close()
class ProxyA(Subject):
def __init__(self, filename):
self.subj = RealSubject(filename)
def get_content(self):
return self.subj.get_content()
class ProxyB(Subject):
def __init__(self, filename):
self.filename = filename
self.subj = None
def get_content(self):
if not self.subj:
self.subj = RealSubject(self.filename)
return self.subj.get_content()
class ProxyC(Subject):
def __init__(self, filename):
self.subj = RealSubject(filename)
def get_content(self):
return self.get_content()
def set_content(self):
raise PermissionError
# 写一个set_content
b = ProxyB("abc.txt")
#print(b.get_content())