Python
設計模式
代理模式
class IPlayer:
def __init__(self):
self.name = ''
self.pwd = ''
def login(self):
pass
def Move(self):
pass
def Attack(self):
pass
class Player(IPlayer):
def login(self):
if self.name =='user1' and self.pwd == 'user1':
print('login success')
else:
print('login fail')
def Move(self):
print('Moving')
def Attack(self):
print('Attack the Monster')
class PlayerProxy:
def __init__(self):
self.Player = Player()
def ProxyProcess(self):
self.Player.name = 'user1'
self.Player.pwd = 'user1'
self.Player.login()
self.Player.Move()
self.Player.Move()
self.Player.Attack()
user1 = Player()
user1.name = 'user'
user1.pwd = 'user1'
user1.login()
user1.name = 'user1'
user1.login()
user1.Move()
user1.Attack()
print('-----------------')
Proxy = PlayerProxy()
Proxy.ProxyProcess()
程式碼說明
IPlayer定義玩家的的介面、有登入、移動、跟攻擊在用IPlayer去設計Player
最後再PlayerProxy裡面,把player應具備的動作寫完
底下是比較兩者的執行起來的差異
留言
張貼留言