Understandable design pattern python3 implementation

The appearance design pattern is often used in ordinary code writing. When writing ordinary code, even if the programmer has not recognized the appearance design pattern from the standard, but in the development process, he will consider the code from multiple angles, thus writing the code that conforms to the appearance design pattern.

Many programmers have this feeling. When learning some technologies that they think they don't understand, they find that they have already been exposed to a part of the content, and this part of the content has an accurate definition in writing.

Programmers with extensive project experience, after considering the overall code structure and performance optimization of the project, the programs they write will be similar to those defined by some standards.

Learning design patterns is to promote the growth of related technologies, but before they have a wealth of project experience, a large part of the developers who have learned design patterns will not know how to use design patterns into their own code writing. Therefore, most of the design pattern articles I write simply implement the design pattern without adding extra content to the implementation. Because my view is "Slowly progress, after having project experience, I will further consider the optimization iteration of the project".

Back to the topic, start to explain the appearance design mode.
The appearance design pattern, in simple terms, is to write a simple interface without exposing the complexity of the system, increasing the ease of use of the system, and shielding the internal implementation.
In daily development, the internal implementation of the shielding system is something that is often done, so for some developers, the appearance design pattern is a technology often used in projects. Next, let's take a look at the basic realization of the appearance design pattern.

Example: Control the start of the air conditioner through a button.
Analyzing the start of the air conditioner can be briefly divided into the following steps: click the button -> receive the signal of the click button -> turn on the refrigeration -> turn on the temperature detection and automatic temperature adjustment.

We first create a refrigerator and thermometer class to simulate the startup of the refrigerator and thermometer:

#制冷器
class Refrigerator():
    def start(self):
        print('制冷器启动...')
    def stop(self):
        print('制冷器停止...')
#温度计
class Thermometer():
    def start(self):
        print("温度计启动...")
    def stop(self):
        print("温度计停止...")

Under normal circumstances, we can directly call the refrigerator and then call the thermometer. At this time, it is more troublesome. The internal system is exposed and the use is extremely cumbersome.
Next, create a new air conditioner class to call these two classes uniformly to realize the beginning of refrigeration.

#空调类
class AirConditioner():
	#初始化
    def __init__(self):
        self.refrigerator=Refrigerator()
        self.thermometer=Thermometer()
    #开始键按下   
    def startBtnClick():
        print('空调启动键按下...')
        self.start()
    #停止键按下    
    def stopBtnClick():
        print('空调关闭键按下...')
        self.stop()
    #开始方法    
    def start(self):
        print('空调启动...')
        self.refrigerator.start()
        self.thermometer.start()
    #停止方法    
    def stop(self):
        print('空调停止...')
        self.refrigerator.stop()
        self.thermometer.stop()

In the above air conditioner class, the refrigerator class and the thermometer detection class are instantiated in the initialization. In the method of clicking the start button, the start method is activated, and the start method opens the refrigerator and temperature detection; the same is true for the stop method. The calling method is as follows:

ac=AirConditioner()
ac.start()

After creating a new air conditioner, just start to check the operation effect:
Insert picture description here
finally check the complete code:

#空调类
class AirConditioner():
	#初始化
    def __init__(self):
        self.refrigerator=Refrigerator()
        self.thermometer=Thermometer()
    #开始键按下   
    def startBtnClick():
        print('空调启动键按下...')
        self.start()
    #停止键按下    
    def stopBtnClick():
        print('空调关闭键按下...')
        self.stop()
    #开始方法    
    def start(self):
        print('空调启动...')
        self.refrigerator.start()
        self.thermometer.start()
    #停止方法    
    def stop(self):
        print('空调停止...')
        self.refrigerator.stop()
        self.thermometer.stop()
        
#制冷器
class Refrigerator():
    def start(self):
        print('制冷器启动...')
    def stop(self):
        print('制冷器停止...')
#温度计
class Thermometer():
    def start(self):
        print("温度计启动...")
    def stop(self):
        print("温度计停止...")

ac=AirConditioner()
ac.start()

Guess you like

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