Home > Backend Development > PHP Tutorial > PHP three-tier structure (Part 1) Simple three-tier structure_PHP tutorial

PHP three-tier structure (Part 1) Simple three-tier structure_PHP tutorial

WBOY
Release: 2016-07-21 15:36:27
Original
968 people have browsed it

As shown in code 1:

Copy code 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 message
public function append($newLWord) {
// Call 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)
}
};

Execution timing diagram, as shown in Figure 1:

PHP three-tier structure (Part 1) Simple three-tier structure_PHP tutorial

(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:

PHP three-tier structure (Part 1) Simple three-tier structure_PHP tutorial

(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:


PHP three-tier structure (Part 1) Simple three-tier structure_PHP tutorial

(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:

Copy the code The code is as follows:

// Code 2, create message service through factory and call
// Appearance layer class
class LWordHomePage {
// Add message
public function append($newLWord) {
// Call the intermediate service
$serv = MyServiceFactory::create();
// Note that this is operating 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 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 message
public function append($newLWord) {
//Temporary code (omitted)
}
};

// Intermediate service layer
class LWordServiceCore implements ILWordService {
// Add message
public function append($newLWord) {
// Call 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:

PHP three-tier structure (Part 1) Simple three-tier structure_PHP tutorial

(Figure 4) Create message service through factory class

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322087.htmlTechArticleAs shown in code 1: Copy the code 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 =...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template