欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入淺析python3 依賴倒置原則(示例代碼)

 更新時(shí)間:2021年07月09日 11:32:18   作者:蟬時(shí)雨  
今天通過園區(qū)停車信息這樣一個(gè)場(chǎng)景分析python3 依賴倒置原則,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

場(chǎng)景

針對(duì)園區(qū)停車信息,需要對(duì)各個(gè)公司提供的停車數(shù)據(jù)進(jìn)行整合并錄入自家公司的大數(shù)據(jù)平臺(tái)

數(shù)據(jù)的錄入無(wú)外乎就是對(duì)數(shù)據(jù)的增刪改查

下面上一個(gè)常規(guī)的寫法(未符合依賴倒置),整合來(lái)自 長(zhǎng)安和豐田 的停車數(shù)據(jù)

class Changan(object):
    def __init__(self):
        self.type = 'changan'

    def ca_add(self):
        print('%s 新增' % self.type)

    def ca_del(self):
        print('%s 刪除' % self.type)

    def ca_modify(self):
        print('%s 修改' % self.type)
        
    def ca_get(self):
        print('%s 查詢' % self.type)


class Toyota(object):
    def __init__(self):
        self.type = 'fengtian'

    def tyt_add(self):
        print('%s 新增' % self.type)

    def tyt_del(self):
        print('%s 刪除' % self.type)

    def tyt_modify(self):
        print('%s 修改' % self.type)

    def tyt_get(self):
        print('%s 查詢' % self.type)

class Data(object):

    def __init__(self, car):
        self.car = car

    def data_add(self):
        if self.car.type == 'changan':
            self.car.ca_add()
        else:
            self.car.tyt_add()

    def data_del(self):
        if self.car.type == 'changan':
            self.car.ca_del()
        else:
            self.car.tyt_del()

    def data_mofify(self):
        if self.car.type == 'changan':
            self.car.ca_modify()
        else:
            self.car.tyt_modify()

    def data_get(self):
        if self.car.type == 'changan':
            self.car.ca_get()
        else:
            self.car.tyt_get()

if __name__ == '__main__':
    ca = Changan()
    tyt = Toyota()
    autosystem = Data(ca)
    autosystem.data_add()
    autosystem.data_del()
    autosystem.data_modify()
    autosystem.data_get()
    autosystem.car = tyt
    print('*' * 50)
    autosystem.data_add()
    autosystem.data_del()
    autosystem.data_modify()
    autosystem.data_get()

運(yùn)行的結(jié)果如下

changan 新增
changan 刪除
changan 修改
changan 查詢
**************************************************
fengtian 新增
fengtian 刪除
fengtian 修改
fengtian 查詢

可以看到最后的Data類是一個(gè)簡(jiǎn)單工廠,通過面向流程的方式對(duì)數(shù)據(jù)進(jìn)行增刪改查,上層的Data類永遠(yuǎn)都要依賴下層的Changan類和Toyota類,假設(shè)未來(lái)Changan類和Toyota類因?yàn)樾枨笞兏鼘?dǎo)致實(shí)現(xiàn)方式變了,那么Data類也會(huì)跟著改,或者未來(lái)又來(lái)一家新的廠商鈴木Suzuki,那么在Data又要多寫很多if else。這樣的改動(dòng)對(duì)于程序員來(lái)說是致命的,每一次變動(dòng)需要改動(dòng)的地方都很多,問題油然而生。

如何解決

遵循依賴倒置原則,根據(jù)

"程序要依賴于抽象接口,不要依賴于具體實(shí)現(xiàn)。"

通過changan、toyota這些類的公共性,把處理數(shù)據(jù)的方法通過接口抽象出來(lái)

import abc

class DataProcessing(metaclass=abc.ABCMeta):
    """
    抽象類
    抽象新增改查
    """
    @abc.abstractmethod
    def data_add(self, *args, **kwargs):
        pass

    @abc.abstractmethod
    def data_del(self, *args, **kwargs):
        pass

    @abc.abstractmethod
    def data_modify(self, *args, **kwargs):
        pass

    @abc.abstractmethod
    def data_get(self, *args, **kwargs):
        pass

class Changan(DataProcessing):
    def __init__(self):
        self.type = 'changan'

    def data_add(self):
        print('%s 新增' % self.type)

    def data_del(self):
        print('%s 刪除' % self.type)

    def data_modify(self):
        print('%s 修改' % self.type)
        
    def data_get(self):
        print('%s 查詢' % self.type)

class Toyota(DataProcessing):
    def __init__(self):
        self.type = 'fengtian'

    def data_add(self):
        print('%s 新增' % self.type)

    def data_del(self):
        print('%s 刪除' % self.type)

    def data_modify(self):
        print('%s 修改' % self.type)

    def data_get(self):
        print('%s 查詢' % self.type)

class Data(object):

    def __init__(self, car):
        self.car = car

    def data_add(self):
        self.car.data_add()

    def data_del(self):
        self.car.data_del()

    def data_modify(self):
        self.car.data_modify()

    def data_get(self):
        self.car.data_get()

if __name__ == '__main__':
    ca = Changan()
    tyt = Toyota()
    autosystem = Data(ca)
    autosystem.data_add()
    autosystem.data_del()
    autosystem.data_modify()
    autosystem.data_get()
    autosystem.car = tyt
    print('*' * 50)
    autosystem.data_add()
    autosystem.data_del()
    autosystem.data_modify()
    autosystem.data_get()

運(yùn)行后結(jié)果依然為

changan 新增
changan 刪除
changan 修改
changan 查詢
**************************************************
fengtian 新增
fengtian 刪除
fengtian 修改
fengtian 查詢

以上可看出,增刪改查已經(jīng)抽象成DataProcessing里面的方法,以后不管Changan類和Toyota類怎么變動(dòng),或者需要新增一個(gè)Suzuki類什么的,上層的Data類都不用改變,if name == 'main' 后客戶端的調(diào)用也不需要怎么改動(dòng),代碼層次也更清晰,便于后續(xù)的擴(kuò)展。

到此這篇關(guān)于python3 依賴倒置原則示例的文章就介紹到這了,更多相關(guān)python依賴倒置原則內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論