PHP three layer structure (top) simple three-layer structure _php tutorial

Source: Internet
Author: User
As shown in code 1:
Copy CodeThe code is as follows:
Code 1
Appearance Layer Class
Class Lwordhomepage {
Add message
Public function Append ($newLWord) {
Call the intermediate service layer
$serv = new Lwordservicecore ();
$serv->append ($newLWord);
}
};
Intermediate service Layer
Class Lwordservicecore {
Add message
Public function Append ($newLWord) {
Invoking the data access layer
$dbTask = new Lworddbtask ();
$dbTask->append ($newLWord);
}
};
Data Access Layer
Class Lworddbtask {
Add message
Public function Append ($newLWord) {
Data layer code (omitted)
}
};

The execution sequence diagram, 1 shows:

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

The order in which the three-layer structure is called can be visualized visually from the Code and time series diagrams. But the actual development of this simple three-layer structure does not meet the demand! Let's start with the two layers of code, the façade layer and the intermediate service layer. in the appearance layer Lwordhomepage class directly using the New keyword Create and invoke intermediate service class Lwordservicecore belongs to a hard-coded way. in the actual project development process, the appearance layer and the intermediary service layer may be developed by different people, that is, a function module is completed by many people together. and the appearance layer Lwordhomepage class development progress is impossible to wait until the Lwordservicecore class complete development completes only then starts (in other words, the appearance layer cannot wait until the intermediate service layer complete development completes only then starts), this kind of 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 intermediate service class to meet the development progress of the façade layer. When the intermediate service layer is fully developed, replace it ... 2 is shown below:

(Fig. 2), the appearance layer switches between different services

Obviously, to achieve such a requirement, it is very inflexible to use the New keyword directly in the façade layer to create and invoke the Lwordservicecore class! It is difficult to do flexible switching!! We can create a Tempservice class that acts as a temporary implementation of the intermediate service layer. We also need to analyze the two classes of Tempservice and Lwordservicecore, which have the same append function for adding messages, but one is temporary and the other is real. Since the two classes of Tempservice and lwordservicecore have public functions, they should be able to have a common parent class. Consider that there are no other members and attributes for this public ancestor class, so define this common ancestor class as an interface, i.e. ilwordservice! The UML class is shown in Figure 3:


(Figure 3) define and implement the Ilwordservice interface

Tempservice or Lwordservicecore class objects are not directly created in the Lwordhomepage class, and the creation process is given to a factory class Myservicefactory (simple Factory mode). in this way, the outer layers of the Lwordhomepage class only needs to know Ilwordservice interface, the appearance layer code is not concerned about the specific intermediary service code is what, it is very good to achieve the separation of the appearance layer and the specific service code.

What does it equal? Like two hardware engineers, one is the manufacture of computer graphics cards, one is the manufacture of computer motherboards. The engineer who makes the graphics card can plug the video card into a test circuit to test if the video card is working properly? Similarly, the engineer making the motherboard can also plug the motherboard into another test circuit to test if the motherboard is working properly? By the time the two engineers have finished their work, they will be able to connect the results of their work together. This is a parallel development approach that can save almost half the time. From the point of view of software engineering, we should also consider whether we need to support multiple people to develop at the same time when we design the interface code, so as to improve production efficiency.

According to the UML class diagram (3), we modify the PHP code as shown in code 2:
Copy CodeThe code is as follows:
Code 2, create a message service through the factory and call
Appearance Layer Class
Class Lwordhomepage {
Add 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) {
Back to intermediate service tier
return new Lwordservicecore ();
} else {
Returns a temporary implementation
return new Tempservice ();
}
}
}

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

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

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


The timing diagram is shown in Figure 4:

(Figure 4) Create a message service from the factory class

http://www.bkjia.com/PHPjc/322087.html www.bkjia.com true http://www.bkjia.com/PHPjc/322087.html techarticle as shown in code 1: The Copy Code code is as follows://code 1//Appearance layer class Lwordhomepage {//Add message Public function append ($newLWord) {//Call Intermediate service layer $serv =...

  • 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.