Python Design Patterns II(Chapter 9 of Python 3 Object Oriented Programming)
时间:2010-09-05 来源:Ray Z
flyweight
>>> import weakref
>>> class CarModel:
_models = weakref.WeakValueDictionary()
def __new__(cls, model_name, *args, **kwargs):
model = cls._models.get(model_name)
if not model:
model = super().__new__(cls)
cls._models[model_name] = model
return model
def __init__(self, model_name, air=False, tilt=False,
cruise_control=False, power_locks=False,
alloy_wheels=False, usb_charger=False):
if not hasattr(self, "initted"):
self.model_name = model_name
self.air = air
self.tilt = tilt
self.cruise_control = cruise_control
self.power_locks = power_locks
self.alloy_wheels = alloy_wheels
self.usb_charger = usb_charger
self.initted = True
def check_serial(self, serial_number):
print("Sorry, we are unable to check "
"the serial number {0} on the {1} "
"at this time".format(serial_number, self.model_name))
>>> class Car:
def __init__(self, model, color, serial):
self.model = model
self.color = color
self.serial = serial
def check_serial(self):
return self.model.check_serial(self.serial)
>>> dx = CarModel("FIT DX")
>>> lx = CarModel("FIT LX", air=True, cruise_control=True, power_locks=True, tilt=True)
>>> car1 = Car(dx, "blue", "12345")
>>> car2 = Car(dx, "black", "12346")
>>> car3 = Car(lx, "red", "12347")
>>> id(lx)
20634736
>>> del lx
>>> del car3
>>> import gc
>>> gc.collect()
0
>>> lx = CarModel("FIT LX", air=True, cruise_control=True, power_locks=True, tilt=True)
>>> id(lx)
20634224
2 def exit(self):
3 sys.exit(0)
4
5 >>> class Document:
6 def __init__(self, filename):
7 self.filename = filename
8 self.contents = "This file cannot be modified"
9 def save(self):
10 with open(self.filename, 'w') as file:
11 file.write(self.contents)
12
13
14 >>> class ToolbarButton:
15 def __init__(self, name, iconname):
16 self.name = name
17 self.iconname = iconname
18 def click(self):
19 self.command.execute()
20
21
22 >>> class MenuItem:
23 def __init__(self, menu_name, menuitem_name):
24 self.menu = menu_name
25 self.item = menuitem_name
26 def click(self):
27 self.command.execute()
28
29
30 >>> class KeyboardShortcut:
31 def __init__(self, key, modifier):
32 self.key = key
33 self.modifier = modifier
34 def keypress(self):
35 self.command.execute()
36
37
38 >>> class SaveCommand:
39 def __init__(self, document):
40 self.document = document
41 def execute(self):
42 self.document.save()
43
44
45 >>> class ExitCommand:
46 def __init__(self, window):
47 self.window = window
48 def execute(self):
49 self.window.exit()
50
51 >>> window = Window()
52 >>> document = Document("c:\\1.txt")
53 >>> save = SaveCommand(document)
54 >>> exit = ExitCommand(window)
55 >>> save_button = ToolbarButton('save', 'save.png')
56 >>> save_button.command = save
57 >>> save_keystroke = KeyboardShortcut('s', 'ctrl')
58 >>> save_keystroke.command = save
59 >>> exit_menu = MenuItem("File", "Exit")
60 >>> exit_menu.command = exit
>>> class CarModel:
_models = weakref.WeakValueDictionary()
def __new__(cls, model_name, *args, **kwargs):
model = cls._models.get(model_name)
if not model:
model = super().__new__(cls)
cls._models[model_name] = model
return model
def __init__(self, model_name, air=False, tilt=False,
cruise_control=False, power_locks=False,
alloy_wheels=False, usb_charger=False):
if not hasattr(self, "initted"):
self.model_name = model_name
self.air = air
self.tilt = tilt
self.cruise_control = cruise_control
self.power_locks = power_locks
self.alloy_wheels = alloy_wheels
self.usb_charger = usb_charger
self.initted = True
def check_serial(self, serial_number):
print("Sorry, we are unable to check "
"the serial number {0} on the {1} "
"at this time".format(serial_number, self.model_name))
>>> class Car:
def __init__(self, model, color, serial):
self.model = model
self.color = color
self.serial = serial
def check_serial(self):
return self.model.check_serial(self.serial)
>>> dx = CarModel("FIT DX")
>>> lx = CarModel("FIT LX", air=True, cruise_control=True, power_locks=True, tilt=True)
>>> car1 = Car(dx, "blue", "12345")
>>> car2 = Car(dx, "black", "12346")
>>> car3 = Car(lx, "red", "12347")
>>> id(lx)
20634736
>>> del lx
>>> del car3
>>> import gc
>>> gc.collect()
0
>>> lx = CarModel("FIT LX", air=True, cruise_control=True, power_locks=True, tilt=True)
>>> id(lx)
20634224
command 1 >>> class Window:
2 def exit(self):
3 sys.exit(0)
4
5 >>> class Document:
6 def __init__(self, filename):
7 self.filename = filename
8 self.contents = "This file cannot be modified"
9 def save(self):
10 with open(self.filename, 'w') as file:
11 file.write(self.contents)
12
13
14 >>> class ToolbarButton:
15 def __init__(self, name, iconname):
16 self.name = name
17 self.iconname = iconname
18 def click(self):
19 self.command.execute()
20
21
22 >>> class MenuItem:
23 def __init__(self, menu_name, menuitem_name):
24 self.menu = menu_name
25 self.item = menuitem_name
26 def click(self):
27 self.command.execute()
28
29
30 >>> class KeyboardShortcut:
31 def __init__(self, key, modifier):
32 self.key = key
33 self.modifier = modifier
34 def keypress(self):
35 self.command.execute()
36
37
38 >>> class SaveCommand:
39 def __init__(self, document):
40 self.document = document
41 def execute(self):
42 self.document.save()
43
44
45 >>> class ExitCommand:
46 def __init__(self, window):
47 self.window = window
48 def execute(self):
49 self.window.exit()
50
51 >>> window = Window()
52 >>> document = Document("c:\\1.txt")
53 >>> save = SaveCommand(document)
54 >>> exit = ExitCommand(window)
55 >>> save_button = ToolbarButton('save', 'save.png')
56 >>> save_button.command = save
57 >>> save_keystroke = KeyboardShortcut('s', 'ctrl')
58 >>> save_keystroke.command = save
59 >>> exit_menu = MenuItem("File", "Exit")
60 >>> exit_menu.command = exit
相关阅读 更多 +