PHP three-tier structure (I) simple three-tier structure _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP three-tier structure (top) simple three-tier structure. As shown in code 1: Copy the code as follows: Code 1 appearance layer class classLWordHomePage {add a message publicfunctionappend ($ newLWord) {call the intermediate service layer $ serv, as shown in code 1:

The code is as follows:


// Code 1
// Appearance layer class
Class LWordHomePage {
// Add a message
Public function append ($ newLWord ){
// Call the intermediate service layer
$ Serv = new LWordServiceCore ();
$ Serv-> append ($ newLWord );
}
};
// Intermediate service layer
Class LWordServiceCore {
// Add a message
Public function append ($ newLWord ){
// Call 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)
}
};


Execution sequence diagram, as shown in 1:

(), Simple three-layer structure Time sequence diagram

From the code and sequence diagram, we can intuitively see the call sequence of the three-layer structure. However, in actual development, this simple three-tier structure cannot meet the requirements! Let's start with the code at the appearance layer and intermediate service layer.On the appearance layerLWordHomePageClassNewCreate and call intermediate service class with keywordsLWordServiceCoreIt is a hard-coded method.In the actual project development process, the appearance layer and intermediate service layer may be developed by different personnel, that is, a function module is completed by multiple people. The development progress of the LWordHomePage class in the appearance layer cannot be started after the LWordServiceCore class is fully developed (in other words,The appearance layer cannot be started after the intermediate service layer is fully developed), so the collaboration efficiency is very low!To enable the project to be developed by multiple people at the same time, we need 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, you can replace it ...... 2:

(), The appearance layer switches between different services

Obviously, it is not flexible to directly use the new keyword in the appearance layer to create and call the LWordServiceCore class! This is hard to achieve flexible and random switching !! We can create a TempService class to act as a temporary implementation of the intermediate service layer. We also need to analyze the TempService and LWordServiceCore classes. they both have the same append function for adding messages, but one is temporary while the other is real. Since both TempService and LWordServiceCore have public functions, there should be a public parent class. Considering that this common parent class has no other members or attributes, this common parent class is defined as an interface, that is, ILWordService! UML class:


() Define and implement the ILWordService interface

In the LWordHomePage class, you do not directly create TempService or LWordServiceCore class objects. the creation process is handed over to a factory class MyServiceFactory (simple factory mode ).In this way,LWordHomePageClass only needs to knowILWordServiceInterface, the appearance layer code does not care about the specific intermediate service code, so that the appearance layer and the specific service code are well separated.

What does this mean? Like two hardware engineers, one for computer graphics and the other for computer motherboard. The engineer who makes the video card can insert the video card into a test circuit to test whether the video card works normally? Likewise, engineers who make the motherboard can insert the motherboard into another test circuit to test whether the motherboard works properly? After the two engineers finish their work, they can integrate their work results. This is a parallel development method that saves almost half of the time. From the perspective of software engineering, when designing interface code, we should also consider whether to support simultaneous development by multiple people to improve production efficiency.

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

The code is as follows:


// Code 2: Create and call the message service through the factory
// Appearance layer class
Class LWordHomePage {
// Add a message
Public function append ($ newLWord ){
// Call the intermediate service
$ Serv = MyServiceFactory: create ();
// Note that this operation is performed on the ILWordService interface instead of the LWordService class.
$ Serv-> append ($ newLWord );
}
};

// Message service interface
Interface ILWordService {
Public function append ($ newLWord );
};

// Service factory type
Class MyServiceFactory {
// Create a message service
Public static function create (){
If (1 ){
// Return to the intermediate service layer
Return new LWordServiceCore ();
} Else {
// Return 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 ){
// Call 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)
}
};


Time series:

() Create a message service through the factory class

The authorization code is as follows: // code 1 // appearance layer class LWordHomePage {// add a message public function append ($ newLWord) {// call the 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.