Skip to content

Commit 549dda9

Browse files
committed
fix syntax err
1 parent c2cd184 commit 549dda9

File tree

1 file changed

+58
-53
lines changed

1 file changed

+58
-53
lines changed

design/design.rst

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -249,59 +249,64 @@ Abstract Factory(抽象工厂: 解决复杂对象创建问题)
249249
- A deep copy constructs a new compound object and then, recursively,
250250
inserts copies into it of the objects found in the original."
251251

252-
import copy from collections import OrderedDict
253-
254-
class Book: def **init**\ (self, name, authors, price, \*\*rest):
255-
'''Examples of rest: publisher, length, tags, publication date'''
256-
self.name = name self.authors = authors self.price = price # in US
257-
dollars self.\ **dict**.update(rest)
258-
259-
::
260-
261-
def __str__(self):
262-
mylist = []
263-
ordered = OrderedDict(sorted(self.__dict__.items()))
264-
for i in ordered.keys():
265-
mylist.append('{}: {}'.format(i, ordered[i]))
266-
if i == 'price':
267-
mylist.append('$')
268-
mylist.append('\n')
269-
return ''.join(mylist)
270-
271-
class Prototype: def **init**\ (self): self.objects = {}
272-
273-
::
274-
275-
def register(self, identifier, obj):
276-
self.objects[identifier] = obj
277-
278-
def unregister(self, identifier):
279-
del self.objects[identifier]
280-
281-
def clone(self, identifier, **attr):
282-
""" 实现对象拷贝 """
283-
found = self.objects.get(identifier)
284-
if not found:
285-
raise ValueError('Incorrect object identifier: {}'.format(identifier))
286-
obj = copy.deepcopy(found)
287-
obj.__dict__.update(attr) # 实现拷贝时自定义更新
288-
return obj
289-
290-
def main(): b1 = Book('The C Programming Language', ('Brian W.
291-
Kernighan', 'Dennis M.Ritchie'), price=118, publisher='Prentice
292-
Hall', length=228, publication\_date='1978-02-22', tags=('C',
293-
'programming', 'algorithms', 'data structures'))
294-
295-
::
296-
297-
prototype = Prototype()
298-
cid = 'k&r-first'
299-
prototype.register(cid, b1)
300-
b2 = prototype.clone(cid, name='The C Programming Language (ANSI)', price=48.99, length=274,
301-
publication_date='1988-04-01', edition=2)
302-
for i in (b1, b2):
303-
print(i)
304-
print("ID b1 : {} != ID b2 : {}".format(id(b1), id(b2)))
252+
::
253+
import copy
254+
from collections import OrderedDict
255+
256+
class Book:
257+
def __init__(self, name, authors, price, **rest):
258+
'''Examples of rest: publisher, length, tags, publication
259+
date'''
260+
self.name = name
261+
self.authors = authors
262+
self.price = price # in US dollars
263+
self.__dict__.update(rest)
264+
265+
def __str__(self):
266+
mylist = []
267+
ordered = OrderedDict(sorted(self.__dict__.items()))
268+
for i in ordered.keys():
269+
mylist.append('{}: {}'.format(i, ordered[i]))
270+
if i == 'price':
271+
mylist.append('$')
272+
mylist.append('\n')
273+
return ''.join(mylist)
274+
275+
276+
class Prototype:
277+
def __init__(self):
278+
self.objects = {}
279+
280+
def register(self, identifier, obj):
281+
self.objects[identifier] = obj
282+
283+
def unregister(self, identifier):
284+
del self.objects[identifier]
285+
286+
def clone(self, identifier, **attr):
287+
""" 实现对象拷贝 """
288+
found = self.objects.get(identifier)
289+
if not found:
290+
raise ValueError('Incorrect object identifier: {}'.format(identifier))
291+
obj = copy.deepcopy(found)
292+
obj.__dict__.update(attr) # 实现拷贝时自定义更新
293+
return obj
294+
295+
296+
def main():
297+
b1 = Book('The C Programming Language', ('Brian W. Kernighan', 'Dennis M.Ritchie'),
298+
price=118, publisher='Prentice Hall', length=228, publication_date='1978-02-22',
299+
tags=('C', 'programming', 'algorithms', 'data structures'))
300+
301+
prototype = Prototype()
302+
cid = 'k&r-first'
303+
prototype.register(cid, b1)
304+
b2 = prototype.clone(cid, name='The C Programming Language (ANSI)', price=48.99, length=274,
305+
publication_date='1988-04-01', edition=2)
306+
for i in (b1, b2):
307+
print(i)
308+
print("ID b1 : {} != ID b2 : {}".format(id(b1), id(b2)))
309+
305310

306311
--------------
307312

0 commit comments

Comments
 (0)