当前位置: 首页 > news >正文

江苏城乡建设河北seo网络优化师

江苏城乡建设,河北seo网络优化师,江西软件app开发公司,旅游网站开发的意义1. 写在前面 在本文中,我们将介绍一种常用的软件设计模式 —— 单例模式。 通过示例,演示单例创建,并确保该实例在整个应用程序生命周期中保持一致。同时探讨它在 Python 中的目的、益处和实际应用。 关键点: 1、单例模式只有…

1. 写在前面

在本文中,我们将介绍一种常用的软件设计模式 —— 单例模式。

通过示例,演示单例创建,并确保该实例在整个应用程序生命周期中保持一致。同时探讨它在 Python 中的目的、益处和实际应用。

关键点:

1、单例模式只有一个实例存在;
2、单例模式必须自己创建自己的唯一实例;
3、单例模式是一种软件设计模式,而不是专属于某种编程语言的语法;

公众号: 滑翔的纸飞机

现在让我们开始吧!

2. Python 单例模式

那什么是单例模式?

这是个非常简单的问题,基本上就是当我们有一个类时,我们只能实例化该类的一个实例对象,无论你是要优化资源使用、配置数据,还是要为我们的程序优化某个全局只读数据,该模式都提供了一个清晰有效的解决方案。

2.1 实现

因为有不同的方式可以创建。这里我将向你展示几种简单的方法。

2.1.1 使用 __init__

在这里,我将定义一个 Singleton 类并定义一个方法getInstance()。我们的想法是,无论我在哪里调用,它都会返回 Singleton.getInstance()的特定实例。

考虑,无论是否已创建类实例,都将返回特定实例对象。因此,在这里使用类变量,跟踪实例是否已创建。

class Singleton:# 类变量 __instance 将跟踪唯一的对象实例__instance = None@staticmethoddef getInstance():if Singleton.__instance == None:Singleton()return Singleton.__instance

现在看起来当然还不是一个单例,接下去通过__init__()方法(类似于构造函数),在Singleton对象实例化时被调用。该对象会将类变量设置为对象实例。__init__()全局只应被调用一次,并且由该类中的方法调用(getInstance()),保证__instance类变量赋值之后不允许再此赋值。

def __init__(self):if Singleton.__instance != None:raise Exception("Singleton object already created!")else:Singleton.__instance = self

现在,让我们创建实例验证:
在s1 情况下会调用__init__()创建实例;
在s2 情况下返回与s1相同实例;

s1 = Singleton.getInstance()
print(s1)
s2 = Singleton.getInstance()
print(s2)

而且如果我们在 s1 上设置属性,s2 也会有相同的值,因为它们都指向同一个对象。

s1.x = 5
print(s2.x)

它将打印以下输出

<__main__.Singleton object at 0x10e24fdf0>
<__main__.Singleton object at 0x10e24fdf0>
5

完整代码示例:

"""
@Time:2023/9/15 01:18
@Author:'jpzhang.ht@gmail.com'
@Describe:
"""class Singleton:# 类变量 __instance 将跟踪唯一的对象实例__instance = Nonedef __init__(self):if Singleton.__instance != None:raise Exception("Singleton object already created!")else:Singleton.__instance = self@staticmethoddef getInstance():if Singleton.__instance == None:Singleton()return Singleton.__instanceif __name__ == '__main__':s1 = Singleton.getInstance()print(s1)s2 = Singleton.getInstance()print(s2)s1.x = 5print(s2.x)

2.1.2 使用 __call__ & metaclass

__call__():Python中,只要在创建类时定义了__call__()方法,这个类就是可调用对象。

针对__call__()使用参考示例:

class Father:def __call__(self):print('My call() function')fat=Father()
fat()输出:My call() function

很神奇不是,实例对象也可以像函数一样作为可调用对象来用。

接下去通过__call__实现单例,完整代码示例如下:

"""
@Time:2023/9/15 01:26
@Author:'jpzhang.ht@gmail.com'
@Describe:
"""class Singleton(type):_instances = {}def __call__(cls, *args, **kwargs):if cls not in cls._instances:cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)return cls._instances[cls]class Singleton1(metaclass=Singleton):passif __name__ == '__main__':s1 = Singleton1()print(s1)s2 = Singleton1()print(s2)s1.x = 5print(s2.x)

输出:

<__main__.Singleton1 object at 0x1120c8280>
<__main__.Singleton1 object at 0x1120c8280>
5

2.1.3 使用 __new__

简单介绍下__new__():只要是面向对象的编程语言,类的实例化都一定包含两个步骤:

(1)在内存中创建对象,即开辟一块内存空间来存放类的实例(Instance);
(2)初始化对象,即给实例的属性赋予初始值,例如全部填空;

在 python 中,第一步由 __new__ 函数负责,第二步由 __init__ 函数负责。

在这种方法中,我们将使用__new__(),让它检查类变量以查看实例是否已创建,如果没有,我们就创建它,无论是否需要创建,我们都会返回实例。

class Singleton:# 类变量 __instance 将跟踪唯一的对象实例__instance = Nonedef __new__(cls):if (cls.__instance is None):cls.__instance = super(Singleton, cls).__new__(cls)return cls.__instance

因此,当第一次创建单例对象时,它会运行__new__,当判断__instance为 None,则创建对象并赋值至__instance类变量。下次运行时,发现__instance不是 None 已被设置,于是将返回 Singleton.__instance

这里通过调用超类的方法来实际返回对象实例本身,然后再返回它的__new__。

s1 = Singleton()
print(s1)
s2 = Singleton()
print(s2)# 同样,如果在 s1 上设置一个属性,s2将具有相同的值,因为它们都引用同一个对象
s1.x = 5
print(s2.x)

它将打印以下输出

<__main__.Singleton object at 0x10607beb0>
<__main__.Singleton object at 0x10607beb0>
5

完整示例:

"""
@Time:2023/9/16 23:04
@Author:'jpzhang.ht@gmail.com'
@Describe:
"""class Singleton:# 类变量 __instance 将跟踪唯一的对象实例__instance = Nonedef __new__(cls):if (cls.__instance is None):cls.__instance = super(Singleton, cls).__new__(cls)return cls.__instanceif __name__ == '__main__':s1 = Singleton()print(s1)s2 = Singleton()print(s2)# 同样,如果在 s1 上设置一个属性,s2将具有相同的值,因为它们都引用同一个对象s1.x = 5print(s2.x)

2.1.4 装饰器

关于装饰器读者自行查阅其他博文;

本例照旧通过 __instance = {} 来跟踪实例对象。使用类地址作为键,实例作为值,每次创造实例时,检查该类是否存在实例,存在的话直接返回该实例即可,否则新建一个实例并存放在字典中。

函数装饰器示例:

"""
@Time:2023/9/16 23:29
@Author:'jpzhang.ht@gmail.com'
@Describe:
"""def singleton(cls):__instance = {}def inner():if cls not in __instance:__instance[cls] = cls()return __instance[cls]return inner@singleton
class Cls(object):def __init__(self):print('My name Cls')if __name__ == "__main__":cls1 = Cls()print(cls1)cls1.x = 5cls2 = Cls()print(cls2)print(cls2.x)

输出:

My name Cls
<__main__.Cls object at 0x10f284160>
<__main__.Cls object at 0x10f284160>
5

类装饰器示例:

"""
@Time:2023/9/16 23:39
@Author:'jpzhang.ht@gmail.com'
@Describe:
"""class Singleton(object):def __init__(self, cls):self._cls = clsself._instance = {}def __call__(self):if self._cls not in self._instance:self._instance[self._cls] = self._cls()return self._instance[self._cls]@Singleton
class Cls(object):def __init__(self):print('My name Cls')if __name__ == "__main__":# print('=======使用1=======')cls1 = Cls()print(cls1)cls1.x = 5cls2 = Cls()print(cls2)print(cls2.x)# print('=======使用2=======')# cls1 = Singleton(Cls)# cls2 = Singleton(Cls)# print(cls1)# print(cls2)

输出:

My name Cls
<__main__.Cls object at 0x10ede0850>
<__main__.Cls object at 0x10ede0850>
5

3. 使用案例

单例经常与设计模式结合使用,以解决最常见的问题,包括缓存、日志、配置设置和线程池。

案例1:

"""
@Time:2023/9/16 23:09
@Author:'jpzhang.ht@gmail.com'
@Describe:
"""class Logger:_instance = Nonedef __new__(cls):if cls._instance is None:cls._instance = super(Logger, cls).__new__(cls)cls._instance._initialize()return cls._instancedef _initialize(self):self.log_file = open("log.txt", "a")def log(self, message):self.log_file.write(message + "\n")self.log_file.flush()def close(self):self.log_file.close()class LoggerFactory:def create_logger(self):return Logger()# 使用方法
if __name__ == "__main__":logger_factory = LoggerFactory()logger1 = logger_factory.create_logger()logger2 = logger_factory.create_logger()logger1.log("This is a log message from logger1")logger2.log("This is a log message from logger2")logger1.close()logger2.close()

在本例中,创建了一个采用单例模式的日志类Logger。它确保只创建一个类实例。LoggerFactory类是一个工厂类,可创建Logger类的实例。

log.txt:This is a log message from logger1
This is a log message from logger2

运行代码后,可以看到两个对象(logger1、logger2),实际是Logger类的同一个实例。这样可以确保日志记录只使用一个日志文件。

案例2:

线程安全是实现单例模式时的一个重要考虑因素,因为多个线程可能会尝试同时访问或创建类的实例。如果没有适当的同步,这可能会导致创建多个实例,从而违反单例原则。

import threading
import randomclass Singleton:_instance = Nonedef __init__(self):self.value = random.randint(1, 10)def __new__(cls):if cls._instance is None:cls._instance = super(Singleton, cls).__new__(cls)return cls._instancedef create_singleton(index):s = Singleton()print(f"Singleton instance created by thread {threading.current_thread().name}: {s} and value: {s.value}\n")# 模拟多个线程同时创建单例
def problem_case():threads = []for i in range(5):thread = threading.Thread(target=create_singleton, args=(i,))threads.append(thread)thread.start()for thread in threads:thread.join()# 使用锁来确保线程安全
class ThreadSafeSingleton:__instance = None__lock = threading.Lock()def __init__(self):self.value = random.randint(1, 10)@classmethoddef get_instance(cls):if not cls.__instance:with cls.__lock:if not cls.__instance:cls.__instance = cls()return cls.__instancedef create_thread_safe_singleton(index):s = ThreadSafeSingleton.get_instance()print(f"Singleton instance created by thread {threading.current_thread().name}: {s} and value: {s.value}\n")def thread_safe_case():threads = []for i in range(5):thread = threading.Thread(target=create_thread_safe_singleton, args=(i,))threads.append(thread)thread.start()for thread in threads:thread.join()if __name__ == "__main__":print("Problem case (without thread safety):")problem_case()print("\nThread-safe case:")thread_safe_case()

在上例中,ThreadSafeSingleton 使用线程锁类创建了一个锁对象,我们可以用它来同步对类变量的访问。然后定义一个类方法,首先检查是否已经创建了该类的实例。如果没有,它会获取锁,并创建该类的新实例。

备注:获取锁后再次校验是否已经创建了该类的实例。

输出:

Problem case (without thread safety):
Singleton instance created by thread Thread-10: <__main__.Singleton object at 0x102881760> and value: 4Singleton instance created by thread Thread-7: <__main__.Singleton object at 0x102881760> and value: 3Singleton instance created by thread Thread-6: <__main__.Singleton object at 0x102881760> and value: 10Singleton instance created by thread Thread-8: <__main__.Singleton object at 0x102881760> and value: 9Singleton instance created by thread Thread-9: <__main__.Singleton object at 0x102881760> and value: 4Thread-safe case:
Singleton instance created by thread Thread-11: <__main__.ThreadSafeSingleton object at 0x102874a60> and value: 2Singleton instance created by thread Thread-12: <__main__.ThreadSafeSingleton object at 0x102874a60> and value: 2Singleton instance created by thread Thread-13: <__main__.ThreadSafeSingleton object at 0x102874a60> and value: 2Singleton instance created by thread Thread-14: <__main__.ThreadSafeSingleton object at 0x102874a60> and value: 2Singleton instance created by thread Thread-15: <__main__.ThreadSafeSingleton object at 0x102874a60> and value: 2

不难看出,上锁后符合单例原则。

3. 最后

实现该模式的每种方法都有自己的优缺点,选择哪种方法可能取决于具体的用例。虽然该模式在某些情况下很有用,但它也可能带来一些缺点,例如使代码更难测试和维护。

总之,单例设计模式是管理应用程序中类的单个实例的强大工具。不过,在使用时应谨慎,考虑系统的具体要求以及对可维护性、可测试性和并发性的潜在影响。

感谢您花时间阅读文章

关注公众号不迷路:)


文章转载自:
http://gunk.hwbf.cn
http://hematosis.hwbf.cn
http://methoxybenzene.hwbf.cn
http://yawey.hwbf.cn
http://lithofacies.hwbf.cn
http://desalination.hwbf.cn
http://gastronomical.hwbf.cn
http://melburnian.hwbf.cn
http://semitone.hwbf.cn
http://antidromic.hwbf.cn
http://chaw.hwbf.cn
http://eos.hwbf.cn
http://compassable.hwbf.cn
http://proteolysis.hwbf.cn
http://gamophyllous.hwbf.cn
http://catacombs.hwbf.cn
http://bobber.hwbf.cn
http://signori.hwbf.cn
http://pastorale.hwbf.cn
http://rightfully.hwbf.cn
http://epitomist.hwbf.cn
http://telecentric.hwbf.cn
http://kryptol.hwbf.cn
http://enthral.hwbf.cn
http://questor.hwbf.cn
http://pharyngectomy.hwbf.cn
http://salicional.hwbf.cn
http://pennon.hwbf.cn
http://toughie.hwbf.cn
http://editorial.hwbf.cn
http://cadence.hwbf.cn
http://visitation.hwbf.cn
http://overthrown.hwbf.cn
http://infundibuliform.hwbf.cn
http://guidable.hwbf.cn
http://curial.hwbf.cn
http://hypertrophy.hwbf.cn
http://epigene.hwbf.cn
http://mylohyoideus.hwbf.cn
http://symbololatry.hwbf.cn
http://adrenodoxin.hwbf.cn
http://cappelletti.hwbf.cn
http://elope.hwbf.cn
http://consortia.hwbf.cn
http://drossy.hwbf.cn
http://snorty.hwbf.cn
http://pstn.hwbf.cn
http://santana.hwbf.cn
http://yamasee.hwbf.cn
http://hindostan.hwbf.cn
http://garishly.hwbf.cn
http://zoopathology.hwbf.cn
http://biophile.hwbf.cn
http://dopa.hwbf.cn
http://caducous.hwbf.cn
http://foraminate.hwbf.cn
http://collard.hwbf.cn
http://detoxicate.hwbf.cn
http://adjudgement.hwbf.cn
http://erective.hwbf.cn
http://bevatron.hwbf.cn
http://comfort.hwbf.cn
http://absorptivity.hwbf.cn
http://bombast.hwbf.cn
http://gnatcatcher.hwbf.cn
http://cranialgia.hwbf.cn
http://stockbreeder.hwbf.cn
http://advertizer.hwbf.cn
http://festal.hwbf.cn
http://vaccinee.hwbf.cn
http://copperheadism.hwbf.cn
http://misprint.hwbf.cn
http://meistersinger.hwbf.cn
http://idiographic.hwbf.cn
http://duplicated.hwbf.cn
http://catling.hwbf.cn
http://acetylcholine.hwbf.cn
http://cacophony.hwbf.cn
http://placeholder.hwbf.cn
http://thinner.hwbf.cn
http://repp.hwbf.cn
http://dissimilarly.hwbf.cn
http://dneprodzerzhinsk.hwbf.cn
http://sialidan.hwbf.cn
http://thermalise.hwbf.cn
http://juberous.hwbf.cn
http://viole.hwbf.cn
http://wurley.hwbf.cn
http://covent.hwbf.cn
http://urbanite.hwbf.cn
http://column.hwbf.cn
http://pentandrous.hwbf.cn
http://abode.hwbf.cn
http://eclipse.hwbf.cn
http://magnetosheath.hwbf.cn
http://kure.hwbf.cn
http://freezingly.hwbf.cn
http://potshot.hwbf.cn
http://execute.hwbf.cn
http://mesenchymal.hwbf.cn
http://www.15wanjia.com/news/95666.html

相关文章:

  • 到国外做赌博网站是怎么回事网站推广工具有哪些
  • wordpress 页面 html代码seo网站关键词优化快速官网
  • 武汉网站建设S小蝌蚪互联网络推广营销技巧
  • 兰州微网站建设企业邮箱注册
  • 如何建设淘宝客网站百度市场应用官方app
  • 网站建设需求文档模板网络推广都有哪些方式
  • 想象力网站建设公司知识营销案例
  • 公司网站要怎么做网站流量来源
  • 移动应用开发专升本网站优化排名易下拉霸屏
  • 网站平台建设多少钱百度导航如何设置公司地址
  • 苏州市城市建设局网站百度信息流广告位置
  • 国内各大网站制作网站用什么软件
  • 汕头seo网站优化网站seo优化方案策划书
  • 汉子由来 外国人做的网站哈尔滨百度推广联系人
  • 邵阳做网站网络营销包括哪些
  • 广州一次做网站历下区百度seo
  • 有经验的顺德网站建设seo关键词优化提高网站排名
  • 网站开发设计比较好的公司四川旅游seo整站优化
  • 郑州网站建设报价推广联盟
  • 贵州安顺建设主管部门网站荆门网站seo
  • 做网站 就班级优化大师怎么用
  • 汽车销售管理系统重庆seo网络优化师
  • 网站制作软件名字线做手机网站百度关键词排名
  • js实现网站滚屏效果百度电脑版官网
  • 东莞网络app关键词排名优化
  • 自动化培训网站建设百度推广外推联系方式
  • 24小时学会网站建设 pdf下载百度seo关键词
  • 湘潭做网站广告的公司seo公司是做什么的
  • wordpress video html5上海百度seo点击软件
  • 如何网站建设注册域名后如何建立网站