PHP three-layer structure (upper) simple three-layer structure _php tips

Source: Internet
Author: User
As shown in code 1:
Copy Code code as follows:

Code 1
Appearance Layer Class
Class Lwordhomepage {
Add a message
Public function Append ($newLWord) {
Call Intermediate service Layer
$serv = new Lwordservicecore ();
$serv->append ($newLWord);
}
};
Intermediate service Layer
Class Lwordservicecore {
Add a message
Public function Append ($newLWord) {
Calling the data access layer
$dbTask = new Lworddbtask ();
$dbTask->append ($newLWord);
}
};
Data Access Layer
Class Lworddbtask {
Add a message
Public function Append ($newLWord) {
Data-layer code (omitted)
}
};

Execute the timeline diagram as shown in Figure 1:

(Fig. 1), simple three-layer structure sequence diagram

The sequence of calls to the three-tier structure can be seen visually from code and sequence diagrams. But the actual development of this simple three-tier structure does not meet the demand! Let's start with the two layers of code from the façade layer to the middle service layer. Creating and invoking intermediate service classes directly using the New keyword in the Lwordhomepage class in the appearance layer lwordservicecore is a hard-coded way. In the actual project development process, the appearance layer and intermediate service layer may be developed by different people, that is, a function module is completed by more than one person. But the development progress of the Lwordhomepage class of the appearance layer is impossible wait until Lwordservicecore class complete development completes only then starts (in other words, the appearance layer cannot wait until the intermediate service layer completely develops completes only then starts), such collaboration efficiency is very low! In order for the project to be developed by multiple people at the same time, we want to cut the code design. We can organize a temporary intermediary service class to meet the development progress of the facade layer. When the intermediate service layer is fully developed, 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 create and invoke the Lwordservicecore class directly using the New keyword in the façade layer! It's hard to be flexible and free to switch!! 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, all of which have the same append function for adding messages, but one is temporary and the other is real. Since both Tempservice and lwordservicecore have public functions, they should be able to have a common parent class. Given that there are no other members and attributes for this common ancestor class, this common ancestor class is defined as an interface, i.e. ilwordservice! The UML class diagram is shown in Figure 3:


(Figure 3) defining and implementing the Ilwordservice interface

Tempservice or Lwordservicecore class objects are not created directly in the Lwordhomepage class, and the creation process is given to a factory class Myservicefactory (simple Factory mode). as a result, the Lwordhomepage class in the appearance layer only needs to know the Ilwordservice interface, and the appearance layer code does not care what the specific intermediary service code is, This is very good to achieve the appearance layer and the specific service code separation.

What does this equal? It's like two hardware engineers, one for making computer graphics cards and one for making computer motherboards. The engineer who makes the graphics card can insert the video card into a test circuit to test whether the video card can work properly. Similarly, the engineer that makes the motherboard can also insert the motherboard into another test circuit to test the motherboard for proper operation. Once the two engineers have finished their work, they will be able to work together. This is a parallel development approach that can save half of the time. From the point of view of software engineering, we should also consider whether it is necessary to support multiple people to develop at the same time in designing the interface code to improve productivity.

According to the UML class diagram (shown in Figure 3), we modify the PHP code as shown in code 2:

Copy Code code as follows:

Code 2, create a message service from the factory and call
Appearance Layer Class
Class Lwordhomepage {
Add a message
Public function Append ($newLWord) {
Call Intermediate Service
$serv = Myservicefactory::create ();
Note that this is the operation of the Ilwordservice interface, not the Lwordservice class
$serv->append ($newLWord);
}
};

Message Service Interface
Interface Ilwordservice {
Public function append ($newLWord);
};

Service Factory class
Class Myservicefactory {
Create a message Service
public static function Create () {
if (1) {
Return to intermediate service layer
return new Lwordservicecore ();
} else {
Returns a temporary implementation
return new Tempservice ();
}
}
}

Temporary service class
Class Tempservice implements Ilwordservice {
Add a message
Public function Append ($newLWord) {
Temporary code (omitted)
}
};

Intermediate service Layer
Class Lwordservicecore implements Ilwordservice {
Add a message
Public function Append ($newLWord) {
Calling the data access layer
$dbTask = new Lworddbtask ();
$dbTask->append ($newLWord);
}
};

Data Access Layer
Class Lworddbtask {
Add a message
Public function Append ($newLWord) {
Data-layer code (omitted)
}
};


The sequence diagram is shown in Figure 4:

(Figure 4) Creating a message service from a factory class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.