Observer ModeProgramI have heard about development. Today, I will also briefly summarize how I use the observer mode in the program development process. I hope to bring you some new ideas, if any expert sees it, don't laugh at it. Haha, the most technical person is always learning technology for the sake of technology, with the goal of only one of the highest technical realm;
The observer mode is often used in design patterns. What is the observer mode? Literally, one or more objects observe an object. This observation is purposeful, when an event occurs by the observer, it can accurately report to the outside world and inform everyone of what happened to you. Especially today, object-oriented programming is becoming increasingly mature, abstract: saves repetitive labor to the least, and method members encapsulate classes as containers. This is a good programming rule, just like the environment in which we live, cars and people are independent survival individuals, and they do not interfere with each other. I don't need to know the meaning of my existence. The meaning of existence is for others;
On the topic, I just talked about cars and people. For example, when we take a bus, I don't know the road. When I need a bus, I will inform the passengers of the station you are on, we get off the bus. This is often the case when we do development. I want a button to be clicked by the mouse and a text box will pop up, and so on. Let's continue the car-to-people analogy, when a car arrives at the station, how does it notify you to arrive at a certain station? Does it involve a physical car arrival event? If not, he will never listen, see Figure 1:
A physical diagram of a car and four passengers is shown in the figure. When a car arrives at a certain station, it should tell the passenger which station to arrive and let the passenger determine whether it is the station he wants to go down. See figure 2:
The figure shows that when the car arrives at the station 1, it notifies the passengers and transmits the information to the passengers to determine whether to get off the bus. If a passenger wants to get off the bus, it notifies the driver that I want to get off the bus. See figure 3:
Passengers determine whether the message is a station they want to get off. If the message is sent to the driver of the car, we refer to the car as a whole between the driver and the car; the car receives the passenger's get-off information and opens the door to let the passenger get off the bus. We only need to implement the information shown in the figureCodeThe above is OK, please refer to the Code diagram: