Python 桥接模式

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 15 12:01:02 2018

@author: mz
"""

class Phone(object):  
    def setGame(self, rhs):
        self.game = rhs
    

class IOSPhone(Phone):
    def playGame(self):
        print("***IOS phone***")
        self.game.play()

class AndroidPhone(Phone):
    def playGame(self):
        print("***Android phone***")
        self.game.play()    

class IGame(object):
    def play(self):
        pass

class WYGame(IGame):
    def play(self):
        print("play game of wang yi \r\n")

class TXGame(IGame):
    def play(self):
        print("play game of teng xun \r\n")

if "__main__" == __name__:
    iphone = IOSPhone()
    aphone = AndroidPhone()
    
    iphone.setGame(WYGame())
    iphone.playGame();
    
    iphone.setGame(TXGame())
    iphone.playGame();
    
    aphone.setGame(WYGame())
    aphone.playGame();
    
    aphone.setGame(TXGame())
    aphone.playGame();     

运行结果:

***IOS phone***
play game of wang yi 

***IOS phone***
play game of teng xun 

***Android phone***
play game of wang yi 

***Android phone***
play game of teng xun 

猜你喜欢

转载自blog.csdn.net/mz5111089/article/details/79597217
今日推荐