Python3 is the simplest implementation template design pattern

During the project development process, the logic processing flow of certain sub-categories is roughly the same, but the template mode is used to reduce code redundancy and reduce coupling.

For example, when implementing a form or page startup, the first step needs to load the startup interface image resource, the second step loads personal information, the third step loads personal wallet information, and the fourth step closes or hides the previous window (interface). At this time, the class can be written as follows:

class LoadNewWindows:
    def load(self):
        print('启动界面...')
        self.loadImg()
        self.loadUserInfo()
        self.loadWalletInfo()
        
    def loadImg(self):
        print('记载图片...')
    def loadUserInfo(self):
        print('加载用户信息...')
    def loadWalletInfo(self):
        print('加载钱包信息...')

loadwin=LoadNewWindows()
loadwin.load()

The above class defines a load method, which calls internal methods to implement the process.

The operation is as follows:
Insert picture description here
If you need to load other windows at this time, the implementation process is similar to the above implementation, except that there is one additional loading information, for example, additional online user information needs to be loaded; or there is no need to load wallet information, this time you can use the above class as A base class, after inheriting, add new methods or call the steps again:

class loadOtherWindows(LoadNewWindows):
    def load(self):
        print('启动新界面...')
        self.loadImg()
        self.loadUserInfo()
        self.loadOtherUserInfo()
    def loadOtherUserInfo(self):
        print('加载其它用户信息...')

The above method is a subclass of LoadNewWindows, and there is a new method loadOtherUserInfo, which is used to load other user information. The other process is similar to the parent class, except that it does not load the wallet, because here, the window we set does not need to load the wallet information . The call of the process is implemented in load. Finally, two classes are called:

loadwin=LoadNewWindows()
loadwin.load()

print('\n--------------分界线------------\n')

loadOtherWin=loadOtherWindows()
loadOtherWin.load()

The results are as follows: The
Insert picture description here
template method reduces code redundancy and reduces coupling, but it has a certain impact on readability. However, there is no problem with documentation during development.
The complete code is as follows:

class LoadNewWindows:
    def load(self):
        print('启动界面...')
        self.loadImg()
        self.loadUserInfo()
        self.loadWalletInfo()
        
    def loadImg(self):
        print('记载图片...')
    def loadUserInfo(self):
        print('加载用户信息...')
    def loadWalletInfo(self):
        print('加载钱包信息...')

class loadOtherWindows(LoadNewWindows):
    def load(self):
        print('启动新界面...')
        self.loadImg()
        self.loadUserInfo()
        self.loadOtherUserInfo()
    def loadOtherUserInfo(self):
        print('加载其它用户信息...')

loadwin=LoadNewWindows()
loadwin.load()

print('\n--------------分界线------------\n')

loadOtherWin=loadOtherWindows()
loadOtherWin.load()

Guess you like

Origin blog.csdn.net/A757291228/article/details/107145681