Python 实现设计模式之工厂模式

778 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第 10 天,点击查看活动详情

引言

设计模式是可重复使用的编程方案,已被用于各种现实世界的环境中,并被证明能产生预期的结果。在本文中,我们将学习最常见的设计模式之一:工厂模式

正如我们稍后将看到的,这种模式使我们更容易跟踪程序中创建的对象,从而将创建对象的代码与使用对象的代码分开。我们将研究工厂设计模式的两种形式:工厂方法抽象方法

设计模式在程序员之间共享,并随着时间的推移不断被改进。 这个话题的流行要归功于 Erich Gamma, Richard Helm, Ralph Johnson, 和 John Vlissides 写的《设计模式:可复用面向对象软件的基础》,根据他们的名字又把书称为 Gang of Four,GOF。

什么是设计模式

一般来说,设计模式帮助程序员创建一个常用的实现模式,特别是在面向对象编程(OOP)中。从设计模式的角度看应用程序的好处 从设计模式的角度看应用程序有很多好处。首先,它缩小了建立一个特定的应用程序的最有效的方法和必要的步骤。其次,你可以参考同一设计模式的现有例子来改进你的应用。

总的来说,设计模式是软件工程中非常有用的准则。

在 OOP 中使用的设计模式有几类,这取决于它们解决的问题的类型和/或它们帮助我们建立的解决方案的类型。在他们的书中,提出了23种设计模式,分为三类:创造模式结构模式行为模式

什么是工厂模式

工厂模式属于创造模式的一种。它提供了创建对象的最佳方法之一。在工厂模式中,对象的创建不向客户端公开逻辑,并使用通用接口引用新创建的对象。

工厂模式在 Python 中使用工厂方法实现的。当用户调用一个方法时,我们传入一个字符串,返回值作为一个新对象是通过工厂方法实现的。工厂方法中使用的对象类型由通过方法传递的字符串确定。

在下面的示例中,每个方法都包含对象作为参数,通过工厂方法实现。

如何实现工厂模式

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())

Button 类有助于创建 html 标记和关联的 html 页面。客户端将无法访问代码的逻辑,输出代表 html 页面的创建。

运行结果:

$ python factory.py 
<img></img>
<input></input>
<obj></obj>  

使用 class 关键字设计一个类工厂,无非是创建一个持有一个类的函数。让我们看看下面的代码:

def apple_function():
	"""Return an Apple class, built using the
	class keyword"""
	class Apple(object):
		def __init__(self, color):
			self.color = color

		def getColor(self):
			return self.color
	return Apple


# invoking class factory function
Apple = apple_function()
appleObj = Apple('red')
print(appleObj.getColor())

使用类型,我们可以动态地创建类。但是这样做会把函数和类一起留在命名空间中。让我们看看这段代码,以便更好地理解它:

def init(self, color):
	self.color = color


def getColor(self):
	return self.color


Apple = type('Apple', (object,), {
	'__init__': init,
	'getColor': getColor,
})

appleRed = Apple(color='red')
print(appleRed.getColor())

上面的代码显示了如何动态地创建类。但问题是,initgetColor 等函数会使命名空间变得杂乱无章,而且我们也无法重复使用这些功能。而通过使用类工厂,你可以最大限度地减少杂乱,并在需要时可以重用这些功能。让我们看一下下面的代码。

def create_apple_class():
	def init(self, color):
		self.color = color

	def getColor(self):
		return self.color

	return type('Apple', (object,), {
		'__init__': init,
		'getColor': getColor,
	})


Apple = create_apple_class()
appleObj = Apple('red')
print(appleObj.getColor())

参考链接: