Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Code Analysis of python3 dependency inversion principle

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the python3 dependency inversion principle example code analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this python3 dependency inversion principle example code analysis article will have a harvest, let's take a look at it.

Scene

For parking information in the park, the parking data provided by various companies need to be integrated and entered into their own company's big data platform.

The input of data is nothing more than the addition, deletion, modification and inquiry of the data.

The following is a conventional way of writing (not in line with dependency inversion) to integrate parking data from Changan and Toyota

Class Changan (object): def _ init__ (self): self.type = 'changan' def ca_add (self): print ('% s added'% self.type) def ca_del (self): print ('% s delete'% self.type) def ca_modify (self): print ('% s modified'% self.type) def Ca_get (self): print ('% s query'% self.type) class Toyota (object): def _ _ init__ (self): self.type = 'fengtian' def tyt_add (self): print ('% s add'% self.type) def tyt_del (self): print ('% s delete'% self.type) def tyt_modify (self): Print ('% s modified'% self.type) def tyt_get (self): print ('% s query'% 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 ()

The result of running is as follows

New to changan

Changan deletion

Changan modification

Changan query

* * *

New to fengtian

Fengtian deletion

Fengtian modification

Fengtian query

You can see that the final Data class is a simple factory, through the process-oriented way to add, delete, change and check the data, the upper Data class will always rely on the lower Changan class and Toyota class, assuming that the future Changan class and Toyota class implementation will change due to changes in requirements, then the Data class will also change, or in the future there will be a new manufacturer Suzuki Suzuki, then in Data to write a lot of if else. Such a change is fatal for programmers, each change requires a lot of changes, the problem arises spontaneously.

How to solve

Follow the principle of dependency inversion, according to

"programs rely on abstract interfaces, not on concrete implementations."

Through the commonality of classes such as changan and toyota, the methods of dealing with data are abstracted through interfaces.

Import abcclass 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): passclass Changan (DataProcessing): def _ _ init__ (self): self.type = 'changan' def data_add (self): print ('% s added'% self.type) def data_del (self): print ('% s delete'% self.type) def data_modify (self): print ('% s modified'% self') .type) def data_get (self): print ('% s query'% self.type) class Toyota (DataProcessing): def _ _ init__ (self): self.type = 'fengtian' def data_add (self): print ('% s add'% self.type) def data_del (self): print ('% s delete'% self.type) Def data_modify (self): print ('% s modified'% self.type) def data_get (self): print ('% s query'% 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_modify () autosystem.data_get ()

After running, the result is still

New to changan

Changan deletion

Changan modification

Changan query

* * *

New to fengtian

Fengtian deletion

Fengtian modification

Fengtian query

As can be seen above, additions, deletions, changes and queries have been abstracted into methods in DataProcessing. No matter how the Changan class and Toyota class change, or need to add a new Suzuki class, the upper Data class does not need to be changed, and the client call after if name = = 'main' does not need to be changed, and the code hierarchy is clearer to facilitate subsequent expansion.

This is the end of the article on "example Code Analysis of python3 dependency inversion principle". Thank you for reading! I believe you all have a certain understanding of the knowledge of "python3 dependency inversion principle example code analysis". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report