Python Design Patterns 简明教程
Python Design Patterns - Introduction
设计模式用于表示开发人员创建软件或 Web 应用程序所用的模式。这些模式基于需求分析进行选择。模式描述了问题的解决方案、何时何地应用解决方案以及实现的后果。
Why Python?
Python 是一种开源脚本语言。它拥有支持各种设计模式的库。Python 的语法易于理解并使用英语关键字。
Python 提供对下面列出的设计模式的支持。这些设计模式将在本教程中用到 -
-
Model View Controller Pattern
-
Singleton pattern
-
Factory pattern
-
Builder Pattern
-
Prototype Pattern
-
Facade Pattern
-
Command Pattern
-
Adapter Pattern
-
Prototype Pattern
-
Decorator Pattern
-
Proxy Pattern
-
Chain of Responsibility Pattern
-
Observer Pattern
-
State Pattern
-
Strategy Pattern
-
Template Pattern
-
Flyweight Pattern
-
Abstract Factory Pattern
-
Object Oriented Pattern
Python Design Patterns - Gist
Python 是一种开源脚本语言,具有高级、解释性、交互性和面向对象。它的设计高度可读。Python 语言的语法易于理解,并且经常使用英语关键字。
Important Points
考虑与 Python 编程语言相关的以下重要要点:
-
它包括函数式、结构化编程方法以及面向对象编程方法。
-
它可用作脚本语言或编程语言。
-
它包括自动垃圾回收。
-
它包括高级动态数据类型并支持各种动态类型检查。
-
Python 包括与 C、C++ 和 Java 等语言集成的功能。
How to download python language in your system?
要下载系统中的 Python 语言,请访问此链接:
它包括适用于 Windows、MacOS 和 Linux 发行版等多种操作系统的包。
Model View Controller Pattern
模型视图控制器是最常使用的设计模式。开发者发现实现这种设计模式非常容易。
以下是模型视图控制器的基本架构 −
现在我们看看该结构如何工作。
Python code
我们考虑一个称为“人”的基本对象并创建一个 MVC 设计模式。
Model.py
import json
class Person(object):
def __init__(self, first_name = None, last_name = None):
self.first_name = first_name
self.last_name = last_name
#returns Person name, ex: John Doe
def name(self):
return ("%s %s" % (self.first_name,self.last_name))
@classmethod
#returns all people inside db.txt as list of Person objects
def getAll(self):
database = open('db.txt', 'r')
result = []
json_list = json.loads(database.read())
for item in json_list:
item = json.loads(item)
person = Person(item['first_name'], item['last_name'])
result.append(person)
return result
它调用一个方法,该方法获取数据库中“人”表的全部记录。记录以 JSON 格式表示。
View
它显示模型中获取的所有记录。视图从不与模型交互;控制器完成这项工作(与模型和视图通信)。
from model import Person
def showAllView(list):
print 'In our db we have %i users. Here they are:' % len(list)
for item in list:
print item.name()
def startView():
print 'MVC - the simplest example'
print 'Do you want to see everyone in my db?[y/n]'
def endView():
print 'Goodbye!'
Controller
控制器通过 getAll() 方法与模型交互,该方法获取显示给最终用户的所有记录。
from model import Person
import view
def showAll():
#gets list of all Person objects
people_in_db = Person.getAll()
#calls view
return view.showAllView(people_in_db)
def start():
view.startView()
input = raw_input()
if input == 'y':
return showAll()
else:
return view.endView()
if __name__ == "__main__":
#running controller function
start()
Python Design Patterns - Singleton
此模式将类的实例化限制为一个对象。这是一种创建模式,只涉及一个类来创建方法和指定对象。
它提供了一个全局访问点来访问所创建的实例。
How to implement a singleton class?
下面的程序演示了单例类的实现,其中打印出多次创建的实例。
class Singleton:
__instance = None
@staticmethod
def getInstance():
""" Static access method. """
if Singleton.__instance == None:
Singleton()
return Singleton.__instance
def __init__(self):
""" Virtually private constructor. """
if Singleton.__instance != None:
raise Exception("This class is a singleton!")
else:
Singleton.__instance = self
s = Singleton()
print s
s = Singleton.getInstance()
print s
s = Singleton.getInstance()
print s
Python Design Patterns - Factory
工厂模式属于创建模式列表类别。它提供了创建对象的一种最佳方法。在工厂模式中,通过不向客户端公开逻辑并将新创建的对象称为公共接口,来创建对象。
在 Python 中使用工厂方法来实现工厂模式。当用户调用一个方法以便我们传入一个字符串,并且返回值作为通过工厂方法实现的新对象时。在工厂方法中使用的对象类型由通过方法传递的字符串决定。
在下面的示例中,每个方法都包含作为参数的对象,它是通过工厂方法实现的。
How to implement a factory pattern?
现在让我们看看如何实现工厂模式。
class Button(object):
html = ""
def get_html(self):
return self.html
class Image(Button):
html = "<img></img>"
class Input(Button):
html = "<input></input>"
class Flash(Button):
html = "<obj></obj>"
class ButtonFactory():
def create_button(self, typ):
targetclass = typ.capitalize()
return globals()[targetclass]()
button_obj = ButtonFactory()
button = ['image', 'input', 'flash']
for b in button:
print button_obj.create_button(b).get_html()
按钮类有助于创建 HTML 标记和关联的 HTML 页面。客户端将无法访问代码的逻辑,并且输出表示 HTML 页面的创建。
Python Design Patterns - Builder
Builder 模式是一种独特的观察模式,它有助于使用简单的对象构建复杂的对象并使用算法方法。该设计模式属于创建模式类别。在此设计模式中,生成器类分步构建最终对象。该生成器独立于其他对象。
Advantages of Builder Pattern
-
它提供了明确的分离和一个独特的层,用于由类创建的指定对象的构建和表示。
-
它提供了对所创建模式的构建过程更好的控制。
-
它提供了改变对象的内部表示的完美场景。
How to implement builder pattern?
在本节中,我们将学习如何实现生成器模式。
class Director:
__builder = None
def setBuilder(self, builder):
self.__builder = builder
def getCar(self):
car = Car()
# First goes the body
body = self.__builder.getBody()
car.setBody(body)
# Then engine
engine = self.__builder.getEngine()
car.setEngine(engine)
# And four wheels
i = 0
while i < 4:
wheel = self.__builder.getWheel()
car.attachWheel(wheel)
i += 1
return car
# The whole product
class Car:
def __init__(self):
self.__wheels = list()
self.__engine = None
self.__body = None
def setBody(self, body):
self.__body = body
def attachWheel(self, wheel):
self.__wheels.append(wheel)
def setEngine(self, engine):
self.__engine = engine
def specification(self):
print "body: %s" % self.__body.shape
print "engine horsepower: %d" % self.__engine.horsepower
print "tire size: %d\'" % self.__wheels[0].size
class Builder:
def getWheel(self): pass
def getEngine(self): pass
def getBody(self): pass
class JeepBuilder(Builder):
def getWheel(self):
wheel = Wheel()
wheel.size = 22
return wheel
def getEngine(self):
engine = Engine()
engine.horsepower = 400
return engine
def getBody(self):
body = Body()
body.shape = "SUV"
return body
# Car parts
class Wheel:
size = None
class Engine:
horsepower = None
class Body:
shape = None
def main():
jeepBuilder = JeepBuilder() # initializing the class
director = Director()
# Build Jeep
print "Jeep"
director.setBuilder(jeepBuilder)
jeep = director.getCar()
jeep.specification()
print ""
if __name__ == "__main__":
main()
Python Design Patterns - Prototype
原型设计模式有助于隐藏由类创建的实例的复杂性。现有对象的概念将与从头创建的新对象的有所不同。
如果需要,新复制的对象的属性可能会发生一些更改。这种方法节省了用于产品开发的时间和资源。
How to implement a prototype pattern?
现在让我们看看如何实现原型模式。
import copy
class Prototype:
_type = None
_value = None
def clone(self):
pass
def getType(self):
return self._type
def getValue(self):
return self._value
class Type1(Prototype):
def __init__(self, number):
self._type = "Type1"
self._value = number
def clone(self):
return copy.copy(self)
class Type2(Prototype):
""" Concrete prototype. """
def __init__(self, number):
self._type = "Type2"
self._value = number
def clone(self):
return copy.copy(self)
class ObjectFactory:
""" Manages prototypes.
Static factory, that encapsulates prototype
initialization and then allows instatiation
of the classes from these prototypes.
"""
__type1Value1 = None
__type1Value2 = None
__type2Value1 = None
__type2Value2 = None
@staticmethod
def initialize():
ObjectFactory.__type1Value1 = Type1(1)
ObjectFactory.__type1Value2 = Type1(2)
ObjectFactory.__type2Value1 = Type2(1)
ObjectFactory.__type2Value2 = Type2(2)
@staticmethod
def getType1Value1():
return ObjectFactory.__type1Value1.clone()
@staticmethod
def getType1Value2():
return ObjectFactory.__type1Value2.clone()
@staticmethod
def getType2Value1():
return ObjectFactory.__type2Value1.clone()
@staticmethod
def getType2Value2():
return ObjectFactory.__type2Value2.clone()
def main():
ObjectFactory.initialize()
instance = ObjectFactory.getType1Value1()
print "%s: %s" % (instance.getType(), instance.getValue())
instance = ObjectFactory.getType1Value2()
print "%s: %s" % (instance.getType(), instance.getValue())
instance = ObjectFactory.getType2Value1()
print "%s: %s" % (instance.getType(), instance.getValue())
instance = ObjectFactory.getType2Value2()
print "%s: %s" % (instance.getType(), instance.getValue())
if __name__ == "__main__":
main()
Python Design Patterns - Facade
外观设计模式为子系统中的一组接口提供了一个统一接口。它定义了一个任何子系统都可以使用的更高级别接口。
门面类知道哪个子系统对请求负责。
How to design a facade pattern?
让我们现在看看如何设计门面模式。
class _IgnitionSystem(object):
@staticmethod
def produce_spark():
return True
class _Engine(object):
def __init__(self):
self.revs_per_minute = 0
def turnon(self):
self.revs_per_minute = 2000
def turnoff(self):
self.revs_per_minute = 0
class _FuelTank(object):
def __init__(self, level=30):
self._level = level
@property
def level(self):
return self._level
@level.setter
def level(self, level):
self._level = level
class _DashBoardLight(object):
def __init__(self, is_on=False):
self._is_on = is_on
def __str__(self):
return self.__class__.__name__
@property
def is_on(self):
return self._is_on
@is_on.setter
def is_on(self, status):
self._is_on = status
def status_check(self):
if self._is_on:
print("{}: ON".format(str(self)))
else:
print("{}: OFF".format(str(self)))
class _HandBrakeLight(_DashBoardLight):
pass
class _FogLampLight(_DashBoardLight):
pass
class _Dashboard(object):
def __init__(self):
self.lights = {"handbreak": _HandBrakeLight(), "fog": _FogLampLight()}
def show(self):
for light in self.lights.values():
light.status_check()
# Facade
class Car(object):
def __init__(self):
self.ignition_system = _IgnitionSystem()
self.engine = _Engine()
self.fuel_tank = _FuelTank()
self.dashboard = _Dashboard()
@property
def km_per_litre(self):
return 17.0
def consume_fuel(self, km):
litres = min(self.fuel_tank.level, km / self.km_per_litre)
self.fuel_tank.level -= litres
def start(self):
print("\nStarting...")
self.dashboard.show()
if self.ignition_system.produce_spark():
self.engine.turnon()
else:
print("Can't start. Faulty ignition system")
def has_enough_fuel(self, km, km_per_litre):
litres_needed = km / km_per_litre
if self.fuel_tank.level > litres_needed:
return True
else:
return False
def drive(self, km = 100):
print("\n")
if self.engine.revs_per_minute > 0:
while self.has_enough_fuel(km, self.km_per_litre):
self.consume_fuel(km)
print("Drove {}km".format(km))
print("{:.2f}l of fuel still left".format(self.fuel_tank.level))
else:
print("Can't drive. The Engine is turned off!")
def park(self):
print("\nParking...")
self.dashboard.lights["handbreak"].is_on = True
self.dashboard.show()
self.engine.turnoff()
def switch_fog_lights(self, status):
print("\nSwitching {} fog lights...".format(status))
boolean = True if status == "ON" else False
self.dashboard.lights["fog"].is_on = boolean
self.dashboard.show()
def fill_up_tank(self):
print("\nFuel tank filled up!")
self.fuel_tank.level = 100
# the main function is the Client
def main():
car = Car()
car.start()
car.drive()
car.switch_fog_lights("ON")
car.switch_fog_lights("OFF")
car.park()
car.fill_up_tank()
car.drive()
car.start()
car.drive()
if __name__ == "__main__":
main()
Python Design Patterns - Command
命令模式在操作之间增加了一个抽象级别,并且包含一个调用这些操作的对象。
在这个设计模式中,客户端创建一个命令对象,其中包含要执行的命令列表。创建的命令对象实现了特定接口。
以下是命令模式的基本架构−
How to implement the command pattern?
我们现在将看看如何实现设计模式。
def demo(a,b,c):
print 'a:',a
print 'b:',b
print 'c:',c
class Command:
def __init__(self, cmd, *args):
self._cmd=cmd
self._args=args
def __call__(self, *args):
return apply(self._cmd, self._args+args)
cmd = Command(dir,__builtins__)
print cmd()
cmd = Command(demo,1,2)
cmd(3)
Python Design Patterns - Adapter
适配器模式充当两个不兼容接口之间的桥梁。此类设计模式属于结构模式,因为此模式结合了两个独立接口的功能。
该模式涉及单个类,该类负责合并独立或不兼容接口的功能。真实生活中的一个例子是读卡器,它充当存储卡和笔记本电脑之间的适配器。你将存储卡插入读卡器,再将读卡器插入笔记本电脑,这样就可以通过笔记本电脑读取存储卡了。
适配器设计模式有助于让类协同工作。它将类的接口转换为基于需求的另一个接口。该模式包含一项命名为一个名称和多个形式的多态性规范。例如用于形状的类,它可以根据收集到的需求使用。
有两种类型的适配器模式−
How to implement the adapter pattern?
让我们现在看看如何实现适配器模式。
class EuropeanSocketInterface:
def voltage(self): pass
def live(self): pass
def neutral(self): pass
def earth(self): pass
# Adaptee
class Socket(EuropeanSocketInterface):
def voltage(self):
return 230
def live(self):
return 1
def neutral(self):
return -1
def earth(self):
return 0
# Target interface
class USASocketInterface:
def voltage(self): pass
def live(self): pass
def neutral(self): pass
# The Adapter
class Adapter(USASocketInterface):
__socket = None
def __init__(self, socket):
self.__socket = socket
def voltage(self):
return 110
def live(self):
return self.__socket.live()
def neutral(self):
return self.__socket.neutral()
# Client
class ElectricKettle:
__power = None
def __init__(self, power):
self.__power = power
def boil(self):
if self.__power.voltage() > 110:
print "Kettle on fire!"
else:
if self.__power.live() == 1 and \
self.__power.neutral() == -1:
print "Coffee time!"
else:
print "No power."
def main():
# Plug in
socket = Socket()
adapter = Adapter(socket)
kettle = ElectricKettle(adapter)
# Make coffee
kettle.boil()
return 0
if __name__ == "__main__":
main()
Python Design Patterns - Decorator
装饰器模式允许用户向现有对象添加新功能,而不改变其结构。该类型的设计模式属于结构模式,因为该模式充当现有类的包装。
这个模式创建一个装饰器类,该类包装原始类并提供附加功能,同时保持类方法签名不变。
装饰器模式的目的是为一个对象动态添加附加责任。
How to implement decorator design pattern
下面提到的代码是一个简单的演示,说明如何在 Python 中实现装饰器设计模式。说明涉及以类的格式演示一家咖啡店。创建的咖啡类是一个抽象类,这意味着它不能被实例化。
import six
from abc import ABCMeta
@six.add_metaclass(ABCMeta)
class Abstract_Coffee(object):
def get_cost(self):
pass
def get_ingredients(self):
pass
def get_tax(self):
return 0.1*self.get_cost()
class Concrete_Coffee(Abstract_Coffee):
def get_cost(self):
return 1.00
def get_ingredients(self):
return 'coffee'
@six.add_metaclass(ABCMeta)
class Abstract_Coffee_Decorator(Abstract_Coffee):
def __init__(self,decorated_coffee):
self.decorated_coffee = decorated_coffee
def get_cost(self):
return self.decorated_coffee.get_cost()
def get_ingredients(self):
return self.decorated_coffee.get_ingredients()
class Sugar(Abstract_Coffee_Decorator):
def __init__(self,decorated_coffee):
Abstract_Coffee_Decorator.__init__(self,decorated_coffee)
def get_cost(self):
return self.decorated_coffee.get_cost()
def get_ingredients(self):
return self.decorated_coffee.get_ingredients() + ', sugar'
class Milk(Abstract_Coffee_Decorator):
def __init__(self,decorated_coffee):
Abstract_Coffee_Decorator.__init__(self,decorated_coffee)
def get_cost(self):
return self.decorated_coffee.get_cost() + 0.25
def get_ingredients(self):
return self.decorated_coffee.get_ingredients() + ', milk'
class Vanilla(Abstract_Coffee_Decorator):
def __init__(self,decorated_coffee):
Abstract_Coffee_Decorator.__init__(self,decorated_coffee)
def get_cost(self):
return self.decorated_coffee.get_cost() + 0.75
def get_ingredients(self):
return self.decorated_coffee.get_ingredients() + ', vanilla'
咖啡店的抽象类的实现是通过一个单独的文件完成的,如下所示−
import coffeeshop
myCoffee = coffeeshop.Concrete_Coffee()
print('Ingredients: '+myCoffee.get_ingredients()+
'; Cost: '+str(myCoffee.get_cost())+'; sales tax = '+str(myCoffee.get_tax()))
myCoffee = coffeeshop.Milk(myCoffee)
print('Ingredients: '+myCoffee.get_ingredients()+
'; Cost: '+str(myCoffee.get_cost())+'; sales tax = '+str(myCoffee.get_tax()))
myCoffee = coffeeshop.Vanilla(myCoffee)
print('Ingredients: '+myCoffee.get_ingredients()+
'; Cost: '+str(myCoffee.get_cost())+'; sales tax = '+str(myCoffee.get_tax()))
myCoffee = coffeeshop.Sugar(myCoffee)
print('Ingredients: '+myCoffee.get_ingredients()+
'; Cost: '+str(myCoffee.get_cost())+'; sales tax = '+str(myCoffee.get_tax()))
Python Design Patterns - Proxy
代理设计模式包括一个新对象,该对象代替现有的称为“真实主题”的对象,称为“代理”。创建真实主题的代理对象必须在同一个界面中,以至于客户端不应该有任何想法,即代理用于代替真实对象。客户端向代理发出的请求将通过真实主题传递。
代理模式的 UML 表示如下 -
How to implement the proxy pattern?
现在让我们看看如何实现代理模式。
class Image:
def __init__( self, filename ):
self._filename = filename
def load_image_from_disk( self ):
print("loading " + self._filename )
def display_image( self ):
print("display " + self._filename)
class Proxy:
def __init__( self, subject ):
self._subject = subject
self._proxystate = None
class ProxyImage( Proxy ):
def display_image( self ):
if self._proxystate == None:
self._subject.load_image_from_disk()
self._proxystate = 1
print("display " + self._subject._filename )
proxy_image1 = ProxyImage ( Image("HiRes_10Mb_Photo1") )
proxy_image2 = ProxyImage ( Image("HiRes_10Mb_Photo2") )
proxy_image1.display_image() # loading necessary
proxy_image1.display_image() # loading unnecessary
proxy_image2.display_image() # loading necessary
proxy_image2.display_image() # loading unnecessary
proxy_image1.display_image() # loading unnecessary
Chain of Responsibility
责任链模式用于在软件中实现松耦合,其中客户端的指定请求通过其中的对象链传递。它有助于构建对象链。请求从一端进入,从一个对象移动到另一个对象。
此模式允许对象发送命令,而不了解哪个对象将处理请求。
How to implement the chain of responsibility pattern?
现在我们将看到如何实现责任链模式。
class ReportFormat(object):
PDF = 0
TEXT = 1
class Report(object):
def __init__(self, format_):
self.title = 'Monthly report'
self.text = ['Things are going', 'really, really well.']
self.format_ = format_
class Handler(object):
def __init__(self):
self.nextHandler = None
def handle(self, request):
self.nextHandler.handle(request)
class PDFHandler(Handler):
def handle(self, request):
if request.format_ == ReportFormat.PDF:
self.output_report(request.title, request.text)
else:
super(PDFHandler, self).handle(request)
def output_report(self, title, text):
print '<html>'
print ' <head>'
print ' <title>%s</title>' % title
print ' </head>'
print ' <body>'
for line in text:
print ' <p>%s' % line
print ' </body>'
print '</html>'
class TextHandler(Handler):
def handle(self, request):
if request.format_ == ReportFormat.TEXT:
self.output_report(request.title, request.text)
else:
super(TextHandler, self).handle(request)
def output_report(self, title, text):
print 5*'*' + title + 5*'*'
for line in text:
print line
class ErrorHandler(Handler):
def handle(self, request):
print "Invalid request"
if __name__ == '__main__':
report = Report(ReportFormat.TEXT)
pdf_handler = PDFHandler()
text_handler = TextHandler()
pdf_handler.nextHandler = text_handler
text_handler.nextHandler = ErrorHandler()
pdf_handler.handle(report)
Python Design Patterns - Observer
在此模式中,对象表示为等待事件触发的观察程序。一旦发生指定事件,观察程序将附加到主题上。当事件发生时,主题会告诉观察程序已经发生。
以下 UML 图表示观察程序模式 -
How to implement the observer pattern?
现在让我们看看如何实现观察者模式。
import threading
import time
import pdb
class Downloader(threading.Thread):
def run(self):
print 'downloading'
for i in range(1,5):
self.i = i
time.sleep(2)
print 'unfunf'
return 'hello world'
class Worker(threading.Thread):
def run(self):
for i in range(1,5):
print 'worker running: %i (%i)' % (i, t.i)
time.sleep(1)
t.join()
print 'done'
t = Downloader()
t.start()
time.sleep(1)
t1 = Worker()
t1.start()
t2 = Worker()
t2.start()
t3 = Worker()
t3.start()
Python Design Patterns - State
它提供了一个状态机模块,该模块使用从指定状态机类派生的子类实现。这些方法与状态无关,并导致使用修饰符声明的转换。
How to implement the state pattern?
状态模式的基本实现如下 -
class ComputerState(object):
name = "state"
allowed = []
def switch(self, state):
""" Switch to new state """
if state.name in self.allowed:
print 'Current:',self,' => switched to new state',state.name
self.__class__ = state
else:
print 'Current:',self,' => switching to',state.name,'not possible.'
def __str__(self):
return self.name
class Off(ComputerState):
name = "off"
allowed = ['on']
class On(ComputerState):
""" State of being powered on and working """
name = "on"
allowed = ['off','suspend','hibernate']
class Suspend(ComputerState):
""" State of being in suspended mode after switched on """
name = "suspend"
allowed = ['on']
class Hibernate(ComputerState):
""" State of being in hibernation after powered on """
name = "hibernate"
allowed = ['on']
class Computer(object):
""" A class representing a computer """
def __init__(self, model='HP'):
self.model = model
# State of the computer - default is off.
self.state = Off()
def change(self, state):
""" Change state """
self.state.switch(state)
if __name__ == "__main__":
comp = Computer()
comp.change(On)
comp.change(Off)
comp.change(On)
comp.change(Suspend)
comp.change(Hibernate)
comp.change(On)
comp.change(Off)
Python Design Patterns - Strategy
策略模式是一种行为模式。策略模式的主要目标是使客户端能够从不同的算法或过程选择以完成指定的任务。可以在没有任何并发症的情况下为指定的任务换入和换出不同的算法。
当访问外部资源时,可以使用此模式提高灵活性。
How to implement the strategy pattern?
下面显示的程序有助于实现策略模式。
import types
class StrategyExample:
def __init__(self, func = None):
self.name = 'Strategy Example 0'
if func is not None:
self.execute = types.MethodType(func, self)
def execute(self):
print(self.name)
def execute_replacement1(self):
print(self.name + 'from execute 1')
def execute_replacement2(self):
print(self.name + 'from execute 2')
if __name__ == '__main__':
strat0 = StrategyExample()
strat1 = StrategyExample(execute_replacement1)
strat1.name = 'Strategy Example 1'
strat2 = StrategyExample(execute_replacement2)
strat2.name = 'Strategy Example 2'
strat0.execute()
strat1.execute()
strat2.execute()
Python Design Patterns - Template
模板模式在一个基类中使用抽象操作定义一个基本算法,其中子类覆盖具体行为。模板模式在单独的方法中保留算法的轮廓。该方法被称为模板方法。
以下是模板模式的不同特点 −
-
它定义了算法在操作中的骨架
-
它包括子类,重新定义一个算法的某些步骤。
class MakeMeal:
def prepare(self): pass
def cook(self): pass
def eat(self): pass
def go(self):
self.prepare()
self.cook()
self.eat()
class MakePizza(MakeMeal):
def prepare(self):
print "Prepare Pizza"
def cook(self):
print "Cook Pizza"
def eat(self):
print "Eat Pizza"
class MakeTea(MakeMeal):
def prepare(self):
print "Prepare Tea"
def cook(self):
print "Cook Tea"
def eat(self):
print "Eat Tea"
makePizza = MakePizza()
makePizza.go()
print 25*"+"
makeTea = MakeTea()
makeTea.go()
Python Design Patterns - Flyweight
享元模式属于结构设计模式类别。它提供了一种减少对象数量的方法。它包括有助于改进应用程序结构的各种功能。享元对象最重要的特点是不变的。这意味着创建后它们不能被修改。该模式使用 HashMap 来存储引用对象。
How to implement the flyweight pattern?
以下程序有助于实现享元模式 −
class ComplexGenetics(object):
def __init__(self):
pass
def 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] = id
return id
def 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()
Python Design Patterns - Abstract Factory
抽象工厂模式也被称为工厂的工厂。这个设计模式属于创建型设计模式类别。它提供了创建对象的最佳方式之一。
它包括一个负责创建与工厂相关的对象的接口。
How to implement the abstract factory pattern?
下面的程序有助于实现抽象工厂模式。
class Window:
__toolkit = ""
__purpose = ""
def __init__(self, toolkit, purpose):
self.__toolkit = toolkit
self.__purpose = purpose
def getToolkit(self):
return self.__toolkit
def getType(self):
return self.__purpose
class 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): pass
def getLayersWindow(self): pass
def getMainWindow(self): pass
class 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 = True
kde = not gnome
if 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())
Python Design Patterns - Object Oriented
面向对象模式是最常用的模式。几乎每个编程语言中都可以找到这个模式。
How to implement the object oriented pattern?
现在让我们来看看如何实施面向对象模式。
class Parrot:
# class attribute
species = "bird"
# instance attribute
def __init__(self, name, age):
self.name = name
self.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))
Object Oriented Concepts Implementation
在本章中,我们将重点关注使用面向对象概念的模式及其在 Python 中的实现。当我们围绕语句块设计我们的程序时,这些语句块围绕函数处理数据,这称为过程化编程。在面向对象编程中,有两个称为类和对象的主实例。
How to implement classes and object variables?
类和对象变量的实现如下 −
class Robot:
population = 0
def __init__(self, name):
self.name = name
print("(Initializing {})".format(self.name))
Robot.population += 1
def die(self):
print("{} is being destroyed!".format(self.name))
Robot.population -= 1
if Robot.population == 0:
print("{} was the last one.".format(self.name))
else:
print("There are still {:d} robots working.".format(
Robot.population))
def say_hi(self):
print("Greetings, my masters call me {}.".format(self.name))
@classmethod
def how_many(cls):
print("We have {:d} robots.".format(cls.population))
droid1 = Robot("R2-D2")
droid1.say_hi()
Robot.how_many()
droid2 = Robot("C-3PO")
droid2.say_hi()
Robot.how_many()
print("\nRobots can do some work here.\n")
print("Robots have finished their work. So let's destroy them.")
droid1.die()
droid2.die()
Robot.how_many()
Python Design Patterns - Iterator
迭代器设计模式属于行为设计模式类别。开发人员几乎在每种编程语言中都会碰到迭代器模式。此模式以这种方式使用,它有助于按顺序访问集合(类)的元素,而无需理解底层设计。
Python Design Patterns - Dictionaries
字典是包含键值组合的数据结构。这些已广泛用于 JSON - JavaScript 对象表示法。字典用于 API(应用程序编程接口)编程。字典将一组对象映射到另一组对象。字典是可变的;这意味着可以根据需要随时更改它们。
How to implement dictionaries in Python?
以下程序展示了 Python 中字典的基本实现,从创建到实现。
# Create a new dictionary
d = dict() # or d = {}
# Add a key - value pairs to dictionary
d['xyz'] = 123
d['abc'] = 345
# print the whole dictionary
print(d)
# print only the keys
print(d.keys())
# print only values
print(d.values())
# iterate over dictionary
for i in d :
print("%s %d" %(i, d[i]))
# another method of iteration
for index, value in enumerate(d):
print (index, value , d[value])
# check if key exist 23. Python Data Structure –print('xyz' in d)
# delete the key-value pair
del d['xyz']
# check again
print("xyz" in d)
Lists Data Structure
Lists 数据结构是 Python 中一个通用的数据类型,它可以写成方括号中逗号分隔的值列表。
Syntax
以下是结构的基本语法 -
List_name = [ elements ];
如果您观察,语法声明为数组,唯一区别在于列表可以包含不同数据类型元素。数组包含相同数据类型的元素。列表可以包含字符串、整数和对象的组合。列表可用于堆栈和队列的实现。
列表是可变的。这些可以根据需要随时更改。
How to implement lists?
以下程序显示了列表的实现 -
my_list = ['p','r','o','b','e']
# Output: p
print(my_list[0])
# Output: o
print(my_list[2])
# Output: e
print(my_list[4])
# Error! Only integer can be used for indexing
# my_list[4.0]
# Nested List
n_list = ["Happy", [2,0,1,5]]
# Nested indexing
# Output: a
print(n_list[0][1])
# Output: 5
print(n_list[1][3])
Python Design Patterns - Sets
集合可以定义为可迭代、可变且不包含重复元素的无序集合。在 Python 中,集合类是数学集合的表示形式。使用集合的主要优点是它包括检查特定元素的高度优化方法。
Python 包含一个称为冻结集合的单独类别。这些集合是不可变对象,只支持产生所需结果的方法和运算符。
How to implement sets?
以下程序有助于实现集合 −
# Set in Python
# Creating two sets
set1 = set()
set2 = set()
# Adding elements to set1
for i in range(1, 6):
set1.add(i)
# Adding elements to set2
for i in range(3, 8):
set2.add(i)
print("Set1 = ", set1)
print("Set2 = ", set2)
print("\n")
# Union of set1 and set2
set3 = set1 | set2# set1.union(set2)
print("Union of Set1 & Set2: Set3 = ", set3)
# Intersection of set1 and set2
set4 = set1 & set2# set1.intersection(set2)
print("Intersection of Set1 & Set2: Set4 = ", set4)
print("\n")
# Checking relation between set3 and set4
if set3 > set4: # set3.issuperset(set4)
print("Set3 is superset of Set4")
elif set3 < set4: # set3.issubset(set4)
print("Set3 is subset of Set4")
else : # set3 == set4
print("Set3 is same as Set4")
# displaying relation between set4 and set3
if set4 < set3: # set4.issubset(set3)
print("Set4 is subset of Set3")
print("\n")
# difference between set3 and set4
set5 = set3 - set4
print("Elements in Set3 and not in Set4: Set5 = ", set5)
print("\n")
# checkv if set4 and set5 are disjoint sets
if set4.isdisjoint(set5):
print("Set4 and Set5 have nothing in common\n")
# Removing all the values of set5
set5.clear()
print("After applying clear on sets Set5: ")
print("Set5 = ", set5)
Python Design Patterns - Queues
队列是一组对象,它定义一个遵循 FIFO(快速进先出)和 LIFO(后进先出)程序的简单数据结构。插入和删除操作分别称为 enqueue 和 dequeue 操作。
队列不允许随机访问它们包含的对象。
How to implement the FIFO procedure?
以下程序有助于实现 FIFO−
import Queue
q = Queue.Queue()
#put items at the end of the queue
for x in range(4):
q.put("item-" + str(x))
#remove items from the head of the queue
while not q.empty():
print q.get()
How to implement the LIFO procedure?
以下程序有助于实现 LIFO 程序−
import Queue
q = Queue.LifoQueue()
#add items at the head of the queue
for x in range(4):
q.put("item-" + str(x))
#remove items from the head of the queue
while not q.empty():
print q.get()
What is a Priority Queue?
优先级队列是一种容器数据结构,其管理一组具有已排序键的记录,以快速访问指定数据结构中键值最小或最大的记录。
How to implement a priority queue?
优先级队列的实现如下−
import Queue
class Task(object):
def __init__(self, priority, name):
self.priority = priority
self.name = name
def __cmp__(self, other):
return cmp(self.priority, other.priority)
q = Queue.PriorityQueue()
q.put( Task(100, 'a not agent task') )
q.put( Task(5, 'a highly agent task') )
q.put( Task(10, 'an important task') )
while not q.empty():
cur_task = q.get()
print 'process task:', cur_task.name
Strings and Serialization
字符串序列化是将对象的状态写入字节流的过程。在 Python 中,“pickle”库用于启用序列化。该模块包括用于序列化和反序列化 Python 对象结构的强大算法。“酸洗”是将 Python 对象层次结构转换为字节流的过程,“去酸洗”是相反的过程。
pickle 模块的演示如下−
import pickle
#Here's an example dict
grades = { 'Alice': 89, 'Bob': 72, 'Charles': 87 }
#Use dumps to convert the object to a serialized string
serial_grades = pickle.dumps( grades )
print(serial_grades)
#Use loads to de-serialize an object
received_grades = pickle.loads( serial_grades )
print(received_grades)
Concurrency in Python
并发经常被误解为并行。并发意味着以系统化的方式计划执行独立的代码。本章重点介绍使用 Python 为操作系统执行并发性。
以下程序有助于为操作系统执行并发性−
import os
import time
import threading
import multiprocessing
NUM_WORKERS = 4
def only_sleep():
print("PID: %s, Process Name: %s, Thread Name: %s" % (
os.getpid(),
multiprocessing.current_process().name,
threading.current_thread().name)
)
time.sleep(1)
def crunch_numbers():
print("PID: %s, Process Name: %s, Thread Name: %s" % (
os.getpid(),
multiprocessing.current_process().name,
threading.current_thread().name)
)
x = 0
while x < 10000000:
x += 1
for _ in range(NUM_WORKERS):
only_sleep()
end_time = time.time()
print("Serial time=", end_time - start_time)
# Run tasks using threads
start_time = time.time()
threads = [threading.Thread(target=only_sleep) for _ in range(NUM_WORKERS)]
[thread.start() for thread in threads]
[thread.join() for thread in threads]
end_time = time.time()
print("Threads time=", end_time - start_time)
# Run tasks using processes
start_time = time.time()
processes = [multiprocessing.Process(target=only_sleep()) for _ in range(NUM_WORKERS)]
[process.start() for process in processes]
[process.join() for process in processes]
end_time = time.time()
print("Parallel time=", end_time - start_time)
Python Design Patterns - Anti
反模式遵循与预定义设计模式相反的策略。该策略包括针对常见问题的常见方法,这些方法可以被形式化,并且通常可以被认为是一种良好的开发实践。通常,反模式是相反的和不受欢迎的。反模式是软件开发中使用的某些模式,被认为是不好的编程实践。
Important features of anti-patterns
现在让我们看看反模式的一些重要特性。
Correctness
这些模式从根本上破坏了你的代码,让你做错事。以下对此有一个简单的说明−
class Rectangle(object):
def __init__(self, width, height):
self._width = width
self._height = height
r = Rectangle(5, 6)
# direct access of protected member
print("Width: {:d}".format(r._width))
Maintainability
如果一个程序易于理解和修改以满足要求,则称该程序是可维护的。导入模块可以被认为是可维护性的一个例子。
import math
x = math.ceil(y)
# or
import multiprocessing as mp
pool = mp.pool(8)
Example of anti-pattern
以下示例有助于演示反模式 −
#Bad
def filter_for_foo(l):
r = [e for e in l if e.find("foo") != -1]
if not check_some_critical_condition(r):
return None
return r
res = filter_for_foo(["bar","foo","faz"])
if res is not None:
#continue processing
pass
#Good
def filter_for_foo(l):
r = [e for e in l if e.find("foo") != -1]
if not check_some_critical_condition(r):
raise SomeException("critical condition unmet!")
return r
try:
res = filter_for_foo(["bar","foo","faz"])
#continue processing
except SomeException:
i = 0
while i < 10:
do_something()
#we forget to increment i
Python Design Patterns - Exception Handling
处理异常也是设计模式的一个主要标准。异常是在程序执行过程中发生的错误。当发生特定错误时,生成一个异常非常重要。这有助于减少程序崩溃。
Why use exceptions?
异常是处理程序中错误和特殊情况的便捷方式。当用户认为指定的代码可能产生错误时,使用异常处理非常重要。