{"id":2109,"date":"2023-11-10T15:10:41","date_gmt":"2023-11-10T07:10:41","guid":{"rendered":""},"modified":"2023-11-10T15:10:41","modified_gmt":"2023-11-10T07:10:41","slug":"Python\u539f\u578b\u6a21\u5f0f","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/2109.html","title":{"rendered":"Python\u539f\u578b\u6a21\u5f0f"},"content":{"rendered":"
\n
Python\u539f\u578b\u6a21\u5f0f\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n <\/body>\u5982\u4f55\u5b9e\u73b0\u539f\u578b\u6a21\u5f0f\uff1f<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>copy
class <\/span>Prototype:
_type = None
_value = None
def <\/span>clone(self):
pass
def <\/span>getType(self):
return <\/span>self._type
def <\/span>getValue(self):
return <\/span>self._value
class <\/span>Type1(Prototype):
def <\/span>__init__(self, number):
self._type = \"Type1\"<\/span>
self._value = number
def <\/span>clone(self):
return <\/span>copy.copy<\/span>(self)
class <\/span>Type2(Prototype):
\"\"<\/span><\/span><\/span><\/span>\" Concrete prototype. \"<\/span>\"\"
def <\/span>__init__(self, number):
self._type = \"Type2\"<\/span>
self._value = number
def <\/span>clone(self):
return <\/span>copy.copy<\/span>(self)
class <\/span>ObjectFactory:
\"\"\" Manages prototypes.
Static factory, that encapsulates prototype
initialization and <\/span>then allows instatiation
of the classes from <\/span>these prototypes.
\"\"\"
__type1Value1 = None
__type1Value2 = None
__type2Value1 = None
__type2Value2 = None
@staticmethod
def <\/span>initialize():
ObjectFactory.__type1Value1 = Type1<\/span>(1)
ObjectFactory.__type1Value2 = Type1<\/span>(2)
ObjectFactory.__type2Value1 = Type2<\/span>(1)
ObjectFactory.__type2Value2 = Type2<\/span>(2)
@staticmethod
def <\/span>getType1Value1():
return <\/span>ObjectFactory.__type1Value1.clone<\/span>()
@staticmethod
def <\/span>getType1Value2():
return <\/span>ObjectFactory.__type1Value2.clone<\/span>()
@staticmethod
def <\/span>getType2Value1():
return <\/span>ObjectFactory.__type2Value1.clone<\/span>()
@staticmethod
def <\/span>getType2Value2():
return <\/span>ObjectFactory.__type2Value2.clone<\/span>()
def <\/span>main():
ObjectFactory.initialize<\/span>()
instance = ObjectFactory.getType1Value1<\/span>()
print(\"%s: %s\"<\/span><\/span><\/span><\/span> % (instance.getType<\/span>(), instance.getValue<\/span>()))
instance = ObjectFactory.getType1Value2<\/span>()
print(\"%s: %s\" % (instance.getType<\/span>(), instance.getValue<\/span>()))
instance = ObjectFactory.getType2Value1<\/span>()
print(\"%s: %s\" % (instance.getType<\/span>(), instance.getValue<\/span>()))
instance = ObjectFactory.getType2Value2<\/span>()
print(\"%s: %s\" % (instance.getType<\/span>(), instance.getValue<\/span>()))
if <\/span>__name__ == \"__main__\"<\/span>:
main()
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
Type1: 1
Type1: 2
Type2: 1
Type2: 2
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u539f\u578b\u6a21\u5f0fzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[164],"tags":[],"class_list":["post-2109","post","type-post","status-publish","format-standard","hentry","category-python-sjms"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2109"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=2109"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/2109\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=2109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=2109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=2109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}