Python適配器模式代碼實(shí)現(xiàn)解析
Python適配器模式,代碼,思考等
# -*- coding: utf-8 -*-
# author:baoshan
class Computer:
def __init__(self, name):
self.name = name
def __str__(self):
return 'the {} computer'.format(self.name)
def execute(self):
return 'executes a program'
class Synthesizer:
def __init__(self, name):
self.name = name
def __str__(self):
return 'the {} synthesizer'.format(self.name)
def play(self):
return 'is playing an electronic song'
class Human:
def __init__(self, name):
self.name = name
def __str__(self):
return '{} the human'.format(self.name)
def speak(self):
return 'says hello'
class Adapter:
def __init__(self, obj, adapted_methods):
self.obj = obj
self.__dict__.update(adapted_methods)
def __str__(self):
return str(self.obj)
def main():
objects = [Computer('Asus')]
synth = Synthesizer('moog')
objects.append(Adapter(synth, dict(execute=synth.play)))
human = Human('Bob')
objects.append(Adapter(human, dict(execute=human.speak)))
for i in objects:
print('{} {}'.format(str(i), i.execute()))
if __name__ == '__main__':
main()
代碼輸出:
the Asus computer executes a program the moog synthesizer is playing an electronic song Bob the human says hello
------------------------------------------------------------------------------------------
我們?cè)O(shè)法使得Human和Synthesizer類與客戶端所期望的接口兼容,且無需改變它們的源代碼。這太棒了!
這里有一個(gè)為你準(zhǔn)備的挑戰(zhàn)性練習(xí),當(dāng)前的實(shí)現(xiàn)有一個(gè)問題,當(dāng)所有類都有一個(gè)屬性name時(shí),以下代碼會(huì)運(yùn)行失敗。
for i in objects:
print('{}'.format(i.name))
首先想想這段代碼為什么會(huì)失敗?雖然從編碼的角度來看這是有意義的,但對(duì)于客戶端代碼來說毫無意義,客戶端不應(yīng)該關(guān)心“適配了什么”和“什么沒有被適配”這類細(xì)節(jié)。我們只是想提供一個(gè)統(tǒng)一的接口。該如何做才能讓這段代碼生效?
思考一下如何將未適配部分委托給包含在適配器類中的對(duì)象。
答案如下:
將適配器類更改如下,增加一行代碼
class Adapter:
def __init__(self, obj, adapted_methods):
self.obj = obj
self.__dict__.update(adapted_methods)
self.name = obj.name
def __str__(self):
return str(self.obj)
然后在main函數(shù)中獲取對(duì)應(yīng)的name,如下
def main():
objects = [Computer('Asus')]
synth = Synthesizer('moog')
objects.append(Adapter(synth, dict(execute=synth.play)))
human = Human('Bob')
objects.append(Adapter(human, dict(execute=human.speak)))
for i in objects:
print('{} {}'.format(str(i), i.execute()))
print('{}'.format(i.name))
if __name__ == '__main__':
main()
輸出結(jié)果如下:
the Asus computer executes a program Asus the moog synthesizer is playing an electronic song moog Bob the human says hello Bob
參考自:《精通Python設(shè)計(jì)模式》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)快遞價(jià)格查詢系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)快遞價(jià)格查詢系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Python讀寫文件模式和文件對(duì)象方法實(shí)例詳解
這篇文章主要介紹了Python讀寫文件模式和文件對(duì)象方法,結(jié)合實(shí)例形式詳細(xì)分析了Python文件操作常用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-09-09
Python pandas 列轉(zhuǎn)行操作詳解(類似hive中explode方法)
這篇文章主要介紹了Python pandas 列轉(zhuǎn)行操作詳解(類似hive中explode方法),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
檢測(cè)tensorflow是否使用gpu進(jìn)行計(jì)算的方式
今天小編就為大家分享一篇檢測(cè)tensorflow是否使用gpu進(jìn)行計(jì)算的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python中copy()與deepcopy()的區(qū)別小結(jié)
接觸python有一段時(shí)間了,一直沒有系統(tǒng)的學(xué)習(xí)過,也對(duì)copy,deepcoy傻傻的分不清,故抽出時(shí)間來理一下。 下面這篇文章主要給大家介紹了關(guān)于python中copy()與deepcopy()的區(qū)別的相關(guān)資料,需要的朋友可以參考下2018-08-08

