Python设计模式之策略模式(Strategy pattern)

策略模式学习链接

#!/usr/bin/python
# coding:utf8
"""
策略模式
"""
import abc


class Student(object):
	__metaclass__ = abc.ABCMeta

	def __init__(self, *args, **kwargs):
		self.name = args[0]
		self.hobby = args[1]
		self.hometown = args[2]

	@abc.abstractmethod
	def introduce(self):
		pass


class A_cs(Student):
	def __init__(self, *args, **kwargs):
		super(A_cs, self).__init__(*args, **kwargs)  # 复用父类方法。super(类名,self).方法名

	def introduce(self):
		print("I am %s, I like %s, and i like money very much. in the future, "
			  "we can make money together, by the way, I come from %s" % (self.name, self.hobby, self.hometown))


class B_se(Student):
	def __init__(self, *args, **kwargs):
		super(B_se, self).__init__(*args, **kwargs)

	def introduce(self):
		print("I am %s, I like %s and so on,I'm from %s,  it is a beautiful place"
			  % (self.name, self.hobby, self.hometown))


class Instructor(object):
	def __init__(self):
		pass

	def introduce(self, student):
		return student.introduce()


if __name__ == '__main__':
	xiaoming = A_cs('xiaoming', 'pretty girl', 'zhengzhou')
	xiaogang = B_se('xiaogang', 'money and pretty girl', 'suzhou')
	instructor = Instructor()
	instructor.introduce(xiaoming)
	instructor.introduce(xiaogang)

策略模式感觉跟其他模式有点相似

    原文作者:勿在浮沙筑高台LS
    原文地址: https://blog.csdn.net/baidu_15113429/article/details/86005884
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞