# -*- coding: utf-8 -*-
"""
@author: Edgar
这是适配器模式的一个应用场景。(具体故事情节请见《大话设计模式》)
·适配器模式使得原本接口不统一的类能被统一调度。
"""
class Center(object):
#中锋类
def __init__(self, name):
self.name = name
def attack(self):
print '{0} attack!'.format(self.name)
def defend(self):
print '{0} defend!'.format(self.name)
class ForeignCenter(object):
#外籍中锋
def __init__(self, name):
self.name = name
def ForeignAttack(self):
print '{0} attack!'.format(self.name)
def ForeignDefend(self):
print '{0} defend!'.format(self.name)
class Translator(Center):
def __init__(self, player):
self.player = player
def attack(self):
self.player.ForeignAttack()
def defend(self):
self.player.ForeignDefend()
if __name__ == '__main__':
YaoMing = ForeignCenter('YaoMing')
Russell = Center('Russell')
YaoMing = Translator(YaoMing)
Russell.attack()
YaoMing.defend()
设计模式之适配器模式
最新推荐文章于 2023-12-21 15:39:19 发布