#https://zhuanlan.zhihu.com/p/57869247
#工厂模式
# -*- coding:utf-8 -*-
class A:
def __init__(self):
self.word = "运行A"
def run(self):
print(self.word)
class B:
def __init__(self):
self.word = "运行B"
def run(self):
print(self.word)
def Interface(classname):
"""
工厂模式接口函数
:param classname:
:return:
"""
run = dict(A=A, B=B)
return run[classname]()
if __name__ == '__main__':
test1 = Interface('A')
test1.run()
test2 = Interface('B')
test2.run()
抽象工厂模式
# -*- coding:utf-8 -*-
class A:
def __init__(self):
self.word = "运行A"
def run(self):
print(self.word)
class B:
def __init__(self):
self.word = "运行B"
def run(self):
print(self.word)
class Interface:
"""
抽象工厂模式接口类
:param classname:
:return:
"""
def __init__(self, classname=None):
self.test = classname
def run(self):
self.test().run()
if __name__ == '__main__':
test1 = Interface()
test1.test = A
test1.run()
test1.test = B
test1.run()
结果:
运行A
运行B
参考链接
#类本身就是对象的抽象,但是抽象工厂是对类的抽象,相同方法、相同属性的归并。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)