学习版本3.5.2
为对象提供代理,用来控制对该对象的访问
比如说火车票代购业务,你只需要告诉代购买什么样的车票并且付钱,然后代购就会去买好火车票给你,你不需要直接去火车站或者12306买车票。
class TrainStation(object):
def buyTicket(self):
return "ticket"
class ProxyBuyTicket(object):
def __init__(self, tStation):
self.train_station = tStation
def buyTicket(self):
print("Proxy go to buy ticket")
return self.train_station.buyTicket()
if __name__ == "__main__":
ts = TrainStation()
pro = ProxyBuyTicket(ts)
print(pro.buyTicket())
运行结果
Proxy go to buy ticket
ticket