PHP layer-3 structure (bottom) PHP implementation of AOP page 1/2

Source: Internet
Author: User
Let's focus on the intermediate service layer. The code at the intermediate service layer is relatively simple, but the code at the data access layer is called to save the message to the database. Source: http://xiazai.jb51.net/201007/yuanma/TraceLWord.rar
The development environment is eclipse (pdt)
Let's focus on the intermediate service layer. The code at the intermediate service layer is relatively simple, but the code at the data access layer is called to save the message to the database. As shown in code 1:

The code is as follows:


// Code 1
// 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 );
}
};


After seeing the demonstration of the message board, the company's product department and marketing department may put forward a variety of ideas and needs. For example, they want to determine the user's permissions before adding a message! Only registered users can leave a message! We need to modify the code, as shown in Code 2:

The code is as follows:


// Code 2: Add logon verification
// Intermediate service layer
Class LWordServiceCore implements ILWordService {
// Add a message
Public function append ($ newLWord ){
If (! ($ UserLogin )){
// Prompt the user to log on
}
// Call the data access layer
$ DbTask = new LWordDBTask ();
$ DbTask-> append ($ newLWord );
}
};


The marketing department also wants to check the message content before adding a message. if the message contains foul words, it will not be saved. We continue to modify the code, as shown in Code 3:

The code is as follows:


// Code 3: Add dirty filtering
// Intermediate service layer
Class LWordServiceCore implements ILWordService {
// Add a message
Public function append ($ newLWord ){
If (! ($ UserLogin )){
// Prompt the user to log on
}
If (stristr ($ newLWord, "SB ")){
// Indicates that the message failed to be sent because it contains dirty words.
}
// Call the data access layer
$ DbTask = new LWordDBTask ();
$ DbTask-> append ($ newLWord );
}
};


The product Department also raised new requirements and they wanted to add the points mechanism. Specifically, the user is given an additional 5 points after each successful message. We continue to modify the code, as shown in code 4:

The code is as follows:


// Code 4: add the message points mechanism
// Intermediate service layer
Class LWordServiceCore implements ILWordService {
// Add a message
Public function append ($ newLWord ){
If (! ($ UserLogin )){
// Prompt the user to log on
}
If (stristr ($ newLWord, "SB ")){
// Indicates that the message failed to be sent because it contains dirty words.
}
// Call the data access layer
$ DbTask = new LWordDBTask ();
$ DbTask-> append ($ newLWord );
// Add extra points to the user
$ Score = getUserScore ($ userName );
$ Score = $ score + 5;
SaveUserScore ($ userName, $ score );
}
};


It didn't take long before the product Department refined the requirements. they hoped that users could be upgraded after each accumulated 1000 points. We continue to modify the code, as shown in code 5:

The code is as follows:


// Code 5: add a user upgrade rule
// Intermediate service layer
Class LWordServiceCore implements ILWordService {
// Add a message
Public function append ($ newLWord ){
If (! ($ UserLogin )){
// Prompt the user to log on
}
If (stristr ($ newLWord, "fuck ")){
// Indicates that the message failed to be sent because it contains dirty words.
}

// Call the data access layer
$ DbTask = new LWordDBTask ();
$ DbTask-> append ($ newLWord );
// Add extra points to the user
$ Score = getUserScore ($ userName );
$ Score = $ score + 5;
SaveUserScore ($ userName, $ score );
// Upgrade a user
If ($ score % 1000) = 0 ){
$ Level = getUserLevel ($ userName );
$ Level = $ level + 1;
SaveUserLevel ($ userName, $ level );
}
}
};


As demand increases, we need to constantly modify the intermediate service layer code. However, it is not difficult to find that the larger the requirement, the larger the code at the intermediate service layer! In the end, even if we use a three-tier development model, it still does not effectively reduce the engineering difficulty! In addition, after modifying the intermediate service code as needed, you need to test all the code again, instead of effectively testing the new code ......

In fact, let's analyze the message board code carefully. first, I need to propose a concept of primary business logic and secondary business logic. In any case, the message content is stored in the database, which is the backbone of the business logic! This is the main business logic! This part is not modified as demand increases. As for permission verification before database storage, content check is required. After database storage, extra points are required for users and upgrade users. these tasks are pre-ordered and scanned, all are secondary business logic! The main business logic is almost unchanged, but the secondary business logic changes frequently. To improve code readability and maintainability, we can consider putting these business logic elsewhere and try not to disturb the main business logic. The main business logic focuses on what you should do. as for anything else, the main business logic will never be heard! The code can be written as follows, as shown in Code 6:

The code is as follows:


// Code 6: separate the main business logic from the secondary business logic
// Intermediate service layer
Class LWordServiceCore implements ILWordService {
// Add a message
Public function append ($ newLWord ){
// Before adding a message
BeforeAppend ($ newLWord );
// Call the data access layer
$ DbTask = new LWordDBTask ();
$ DbTask-> append ($ newLWord );
// After adding a message
BehindAppend ($ newLWord );
}
};


We can insert all the permission judgment code and message content text filtering code into the beforeAppend function and the user credit code into the behindAppend function, in this way, the secondary business logic is cleared from the primary business logic code. The main business logic knows that there is a "sequent" function beforeAppend and a "final" function behindAppend. However, the main business logic does not know what is done in the sequent and final functions, you do not need to know! Of course, the actual coding work is not that simple. we also need to take into account more requirements and changes in the product department and marketing department. Therefore, we 'd better implement a plug-in method to deal with such changes, however, beforeAppend and behindAppend functions alone cannot achieve this goal ~

To implement the plug-in method, you can create an interface! The advantage of using interfaces is that you can isolate definitions and implementations, and implement polymorphism. We create a message extension interface ILWordExtension, which has two functions: beforeAppend and behindAppend. Permission verification, content check, and extra points can be seen as three implementation classes implementing the ILWordExtension interface. the main business logic traverses these three implementation classes in sequence to complete the secondary business logic. 1:
The CheckPowerExtension extension class is used for user permission verification, the CheckContentExtension extension class is used for message content check, and the AddScoreExtension extension class is used to add and upgrade users. Code 7 shows the sample code:

(), Add extended interfaces

The code is as follows:


// Code 7: add an extension interface
// Extended interface
Interface ILWordExtension {
// Before adding a message
Public function beforeAppend ($ newLWord );
// After adding a message
Public function behindAppend ($ newLWord );
};

// Check permissions
Class CheckPowerExtension implements ILWordExtension {
// Before adding a message
Public function beforeAppend ($ newLWord ){
// Determine the user permission here
}

// After adding a message
Public function behindAppend ($ newLWord ){
}
};

// Check the message text
Class CheckContentExtension implements ILWordExtension {
// Before adding a message
Public function beforeAppend ($ newLWord ){
If (stristr ($ newLWord, "SB ")){
Throw new Exception ();
}
}

// After adding a message
Public function behindAppend ($ newLWord ){
}
};

// User points
Class AddScoreExtension implements ILWordExtension {
// Before adding a message
Public function beforeAppend ($ newLWord ){
}

// After adding a message
Public function behindAppend ($ newLWord ){
// Here you are given points
}
};

// Intermediate service layer
Class LWordServiceCore implements ILWordService {
// Add a message
Public function append ($ newLWord ){
// Before adding a message
$ This-> beforeAppend ($ newLWord );

// Call the data access layer
$ DbTask = new LWordDBTask ();
$ DbTask-> append ($ newLWord );

// After adding a message
$ This-> behindAppend ($ newLWord );
}

// Before adding a message
Private function beforeAppend ($ newLWord ){
// Obtain the extended array
$ ExtArray = $ this-> getExtArray ();

Foreach ($ extArray as $ ext ){
// Traverse every extension and call its beforeAppend function
$ Ext-> beforeAppend ($ newLWord );
}
}

// After adding a message
Private function behindAppend ($ newLWord ){
// Obtain the extended array
$ ExtArray = $ this-> getExtArray ();

Foreach ($ extArray as $ ext ){
// Traverse every extension and call its behindAppend function
$ Ext-> behindAppend ($ newLWord );
}
}

// Obtain the extended array,
// The returned value of this function is actually an array of ILWordExtension interfaces.
Private function getExtArray (){
Return array (
// Check permissions
New CheckPowerExtension (),
// Check content
New CheckContentExtension (),
// Add points
New AddScoreExtension (),
);
}
};


If there are new requirements, we only need to add the ILWordExtension implementation class and register it to the getExtArray function. The program is organized and scalable.

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.