As shown in code 1:
(Figure 1), simple three-layer structure timing diagram
The calling sequence of the three-layer structure can be intuitively seen from the code and timing diagram. But in actual development, this simple three-layer structure cannot meet the needs! Let's start with the code of the appearance layer and the intermediate service layer. Directly use the new keyword in the appearance layer LWordHomePage class to create and call the intermediate service class LWordServiceCore is a hard-coding method. In the actual project development process, the appearance layer and the intermediate service layer may be developed by different people, that is, a functional module is completed by multiple people. The development progress of the appearance layer LWordHomePage class cannot wait until the LWordServiceCore class is fully developed (in other words, the appearance layer cannot wait until the middle service layer is fully developed before starting). Such collaboration Very inefficient! In order to enable the project to be developed by multiple people at the same time, we have to cut the code design. We can organize a temporary intermediate service class to meet the development progress of the appearance layer. After the intermediate service layer is fully developed, just replace it... As shown in Figure 2:
(Figure 2), the appearance layer switches between different services
Obviously, to achieve such a requirement, it is very inflexible to directly use the new keyword to create and call the LWordServiceCore class in the appearance layer! It is difficult to switch flexibly and at will! ! We can create a TempService class to act as a temporary implementation of the intermediate service layer. We also need to analyze the two classes TempService and LWordServiceCore. They both have the same append function for adding messages, but one is temporary and the other is real. Since both classes TempService and LWordServiceCore have public functions, they should be able to have a common parent class. Considering that there are no other members and attributes for this public superior class, this public superior class is defined as an interface, namely ILWordService! The UML class diagram is shown in Figure 3:
(Figure 3) Define and implement the ILWordService interface
TempService or LWordServiceCore class objects are not created directly in the LWordHomePage class. The creation process is handed over to a factory class MyServiceFactory (simple factory mode). In this way, the LWordHomePage class in the appearance layer only needs to know the ILWordService interface, and the appearance layer code does not Pay attention to what the specific intermediate service code is, so as to achieve an excellent separation of the appearance layer and the specific service code.
What does this equate to? Just like two hardware engineers, one makes computer graphics cards and the other makes computer motherboards. Engineers who manufacture graphics cards can insert the graphics card into a test circuit to test whether the graphics card can work properly? Similarly, the engineer who manufactures the motherboard can also insert the motherboard into another test circuit to test whether the motherboard can work properly? After the two engineers have completed their work, they can connect their work results together. This is a parallel development method that can save almost half the time. From a software engineering perspective, when designing interface code, we should also consider whether we need to support simultaneous development by multiple people to improve production efficiency.
According to the UML class diagram (shown in Figure 3), we modify the PHP code as shown in Code 2:
The timing diagram is shown in Figure 4:
(Figure 4) Create message service through factory class