概述
<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=10022283">
<h1 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">一.理解单例模式
<blockquote style="margin: 1.2em 0px; border-left: 4px solid #dddddd; padding: 0px 1em; color: #777777; quotes: none;">
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">单例模式是一种创建型设计模式,它确保一个类有且只有一个特定类型的对象,并提供全局访问点。其意图为:
一个对象,当然同一个对象的属性都是相同的,方法也是相同的,地址也是相同的,这样给我们带来的好处就是可以避免消耗过多的内存或cpu资源,例如数据库类,我们希望每次都使用同一个数据库对象来对数据库进行操作,以维护数据的一致性。又如模块的导入,如果没有导入该模块,则导入该模块并实例化,如果已经导入,则返回该模块的对象
:
__new__kwargs):
hasattr(cls,):
cls.instance=super(Singleton,cls).__new__(cls)
cls.instance
a=Singleton()
b=Singleton()
print(a)<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x00000220866EF400>
print(b)<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x00000220866EF400>
a=Singleton()
b=Singleton()
print(a)<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x00000220866EF400>
print(b)<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x00000220866EF400>
a=Singleton()
b=Singleton()
print(a)<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x00000220866EF400>
print(b)<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x00000220866EF400>
:
__instance=
__init__:
:
cls.__instance:
cls.__instance = Singleton()
cls.__instance
a=Singleton.getInstance()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x000001C85C114B38>
b=Singleton.getInstance()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x000001C85C114B38>
print(a)
print(b)
a=Singleton.getInstance()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x000001C85C114B38>
b=Singleton.getInstance()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x000001C85C114B38>
print(a)
print(b)
a=Singleton.getInstance()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x000001C85C114B38>
b=Singleton.getInstance()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x000001C85C114B38>
print(a)
print(b)
MetaSingleton:
__instance={}
kwargs):
self MetaSingleton.__instance:
MetaSingleton.__instance[self] = super(MetaSingleton,self).__call__()
MetaSingleton.__instance[self]
<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Singleton<span class="hljs-params" style="color: #ffc58f;">(Metaclass=MetaSingleton):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">init<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass
a=Singleton()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x0000025103984CC0>
b=Singleton()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x0000025103984CC0>
print(a)
print(b)
<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Singleton<span class="hljs-params" style="color: #ffc58f;">(Metaclass=MetaSingleton):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">init<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass
a=Singleton()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x0000025103984CC0>
b=Singleton()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x0000025103984CC0>
print(a)
print(b)
<span class="hljs-class"><span class="hljs-keyword" style="color: #ebbbff;">class <span class="hljs-title" style="color: #7285b7;">Singleton<span class="hljs-params" style="color: #ffc58f;">(Metaclass=MetaSingleton):
<span class="hljs-function" style="color: #bbdaff;"><span class="hljs-keyword" style="color: #ebbbff;">def <span class="hljs-title" style="color: #7285b7;">init<span class="hljs-params" style="color: #ffc58f;">(self):
<span class="hljs-keyword" style="color: #ebbbff;">pass
a=Singleton()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x0000025103984CC0>
b=Singleton()<span class="hljs-comment" style="color: #7285b7;">#<main.Singleton object at 0x0000025103984CC0>
print(a)
print(b)
属性是相同的,也就是单态模式。
:
_shared_state={}
__init__:
self.x=
self.__dict__=self._shared_state
a=Monostate()
b=Monostate()
b.x=<span class="hljs-number" style="color: #ffc58f;">5
print(a)<span class="hljs-comment" style="color: #7285b7;">#<main.Monostate object at 0x000001C267714B38>
print(b)<span class="hljs-comment" style="color: #7285b7;">#<main.Monostate object at 0x000001C267714B00>
print(a.x)<span class="hljs-comment" style="color: #7285b7;">#5
a.c=<span class="hljs-number" style="color: #ffc58f;">4
print(b.c)<span class="hljs-comment" style="color: #7285b7;">#4
a=Monostate()
b=Monostate()
b.x=<span class="hljs-number" style="color: #ffc58f;">5
print(a)<span class="hljs-comment" style="color: #7285b7;">#<main.Monostate object at 0x000001C267714B38>
print(b)<span class="hljs-comment" style="color: #7285b7;">#<main.Monostate object at 0x000001C267714B00>
print(a.x)<span class="hljs-comment" style="color: #7285b7;">#5
a.c=<span class="hljs-number" style="color: #ffc58f;">4
print(b.c)<span class="hljs-comment" style="color: #7285b7;">#4
a=Monostate()
b=Monostate()
b.x=<span class="hljs-number" style="color: #ffc58f;">5
print(a)<span class="hljs-comment" style="color: #7285b7;">#<main.Monostate object at 0x000001C267714B38>
print(b)<span class="hljs-comment" style="color: #7285b7;">#<main.Monostate object at 0x000001C267714B00>
print(a.x)<span class="hljs-comment" style="color: #7285b7;">#5
a.c=<span class="hljs-number" style="color: #ffc58f;">4
print(b.c)<span class="hljs-comment" style="color: #7285b7;">#4
如果您也喜欢它,动动您的小指点个赞吧