{"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 <\/head>
\n <\/p>\n

\n

Python\u539f\u578b\u6a21\u5f0f<\/h1>\n

Python\u539f\u578b\u6a21\u5f0f\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u539f\u578b\u8bbe\u8ba1\u6a21\u5f0f\u6709\u52a9\u4e8e\u9690\u85cf\u7c7b\u521b\u5efa\u7684\u5b9e\u4f8b\u7684\u590d\u6742\u6027\u3002\u73b0\u6709\u5bf9\u8c61\u7684\u6982\u5ff5\u4e0e\u4ece\u5934\u521b\u5efa\u7684\u65b0\u5bf9\u8c61\u7684\u6982\u5ff5\u4e0d\u540c\u3002\n <\/div>\n
\n \u5982\u679c\u9700\u8981\uff0c\u65b0\u590d\u5236\u7684\u5bf9\u8c61\u7684\u5c5e\u6027\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u66f4\u6539\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8282\u7701\u5f00\u53d1\u4ea7\u54c1\u6240\u9700\u7684\u65f6\u95f4\u548c\u8d44\u6e90\u3002\n <\/div>\n

\u5982\u4f55\u5b9e\u73b0\u539f\u578b\u6a21\u5f0f\uff1f<\/h2>\n
\n \u73b0\u5728\u8ba9\u6211\u4eec\u770b\u770b\u5982\u4f55\u5b9e\u73b0\u539f\u578b\u6a21\u5f0f\u3002\n <\/div>\n
\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
\n \u4e0a\u9762\u7684\u7a0b\u5e8f\u5c06\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa-\n <\/div>\n
\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 \u8f93\u51fa\u6709\u52a9\u4e8e\u4f7f\u7528\u73b0\u6709\u5bf9\u8c61\u521b\u5efa\u65b0\u5bf9\u8c61\uff0c\u5e76\u4e14\u5728\u4e0a\u8ff0\u8f93\u51fa\u4e2d\u6e05\u6670\u53ef\u89c1\u3002\n <\/div>\n

<\/body>
\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}]}}