网站建设建网站2023新闻摘抄十条
享元设计模式
享元(flyweight)设计模式属于结构设计模式类别。 它提供了一种减少对象数的方法。 它包含各种有助于改进应用程序结构的功能。享元对象最重要的特性是不可变的。 这意味着一旦构建就不能修改它们。 该模式使用HashMap来存储引用对象
如何实现享元(flyweight)设计模式?
以下程序演示如何实现享元模式
class ComplexGenetics(object):def __init__(self):passdef genes(self, gene_code):return "ComplexPatter[%s]TooHugeinSize" % (gene_code)
class Families(object):family = {}def __new__(cls, name, family_id):try:id = cls.family[family_id]except KeyError:id = object.__new__(cls)cls.family[family_id] = idreturn iddef set_genetic_info(self, genetic_info):cg = ComplexGenetics()self.genetic_info = cg.genes(genetic_info)def get_genetic_info(self):return (self.genetic_info)def test():data = (('a', 1, 'ATAG'), ('a', 2, 'AAGT'), ('b', 1, 'ATAG'))family_objects = []for i in data:obj = Families(i[0], i[1])obj.set_genetic_info(i[2])family_objects.append(obj)for i in family_objects:print "id = " + str(id(i))print i.get_genetic_info()print "similar id's says that they are same objects "if __name__ == '__main__':test()
执行上述程序生成以下输出
抽象工厂设计模式
抽象工厂模式也被称为工厂的工厂。 此设计模式属于创建设计模式类别。 它提供了创建对象的最佳方法之一。
它包含一个接口,负责创建与工厂相关的对象。
如何实现抽象工厂模式?
参考以下程序,演示如何实现抽象工厂模式。
class Window:__toolkit = ""__purpose = ""def __init__(self, toolkit, purpose):self.__toolkit = toolkitself.__purpose = purposedef getToolkit(self):return self.__toolkitdef getType(self):return self.__purposeclass GtkToolboxWindow(Window):def __init__(self):Window.__init__(self, "Gtk", "ToolboxWindow")class GtkLayersWindow(Window):def __init__(self):Window.__init__(self, "Gtk", "LayersWindow")class GtkMainWindow(Window):def __init__(self):Window.__init__(self, "Gtk", "MainWindow")class QtToolboxWindow(Window):def __init__(self):Window.__init__(self, "Qt", "ToolboxWindow")class QtLayersWindow(Window):def __init__(self):Window.__init__(self, "Qt", "LayersWindow")class QtMainWindow(Window):def __init__(self):Window.__init__(self, "Qt", "MainWindow")# Abstract factory class
class UIFactory:def getToolboxWindow(self): passdef getLayersWindow(self): passdef getMainWindow(self): passclass GtkUIFactory(UIFactory):def getToolboxWindow(self):return GtkToolboxWindow()def getLayersWindow(self):return GtkLayersWindow()def getMainWindow(self):return GtkMainWindow()class QtUIFactory(UIFactory):def getToolboxWindow(self):return QtToolboxWindow()def getLayersWindow(self):return QtLayersWindow()def getMainWindow(self):return QtMainWindow()if __name__ == "__main__":gnome = Truekde = not gnomeif gnome:ui = GtkUIFactory()elif kde:ui = QtUIFactory()toolbox = ui.getToolboxWindow()layers = ui.getLayersWindow()main = ui.getMainWindow()print "%s:%s" % (toolbox.getToolkit(), toolbox.getType())print "%s:%s" % (layers.getToolkit(), layers.getType())print "%s:%s" % (main.getToolkit(), main.getType())
执行上面示例代码,得到以下结果 -
说明
在上面的程序中,抽象工厂为每个窗口创建对象。 它调用每个方法,按预期执行输出。
面向对象设计模式
面向对象的模式是最常用的模式。 几乎所有的编程语言都可以找到这种模式。
如何实现面向对象的模式?
下面让我们看看如何实现面向对象的模式。
参考以下实现代码 -
class Parrot:# class attributespecies = "bird"# instance attributedef __init__(self, name, age):self.name = nameself.age = age# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
执行上面示例代码,得到以下输出结果 -
说明
代码包括类属性和实例属性,它们按照输出的要求打印。有各种功能构成面向对象模式的一部分。 这些功能在下一章中介绍。