PHP three-tier architecture (next) PHP implements AOP 1th/2 page _php tips

Source: Internet
Author: User
Tags aop
This article source download address: Http://xiazai.jb51.net/201007/yuanma/TraceLWord.rar
Development environment for Eclipse (PDT)
Let's focus on the intermediate service layer. Intermediate Service layer code is simpler, just call the data Access layer code to save the message to the database. As shown in code 1:
Copy Code code as follows:

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

After seeing the message board demo, the company's product department and marketing department may come up with a variety of ideas and needs. For example, they want to judge 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:
Copy Code code as follows:

Code 2, increasing logon verification
Intermediate service Layer
Class Lwordservicecore implements Ilwordservice {
Add a message
Public function Append ($newLWord) {
if (!) ( $userLogin)) {
Prompt the user to log on
}
Calling the data access layer
$dbTask = new Lworddbtask ();
$dbTask->append ($newLWord);
}
};

Marketing department also want to add a message before the content of the message to check, if the message contains dirty words will not be saved. We continue to modify the code, as shown in code 3:
Copy Code code as follows:

Code 3, adding foul language 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")) {
Contain bad language, prompt message send failure
}
Calling the data access layer
$dbTask = new Lworddbtask ();
$dbTask->append ($newLWord);
}
};

The product department has also put forward new requirements that they want to add to the integration mechanism. In particular, the user after each message successfully give users +5 points. We continue to modify the code, as shown in code 4:
Copy Code code as follows:

Code 4, add the message integration 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")) {
Contain bad language, prompt message send failure
}
Calling the data access layer
$dbTask = new Lworddbtask ();
$dbTask->append ($newLWord);
Add points to the user
$score = Getuserscore ($userName);
$score = $score + 5;
Saveuserscore ($userName, $score);
}
};

Not long ago, the product department and the requirements of refinement, they want the user points each accumulated enough 1000 points after the upgrade to the user. We continue to modify the code, as shown in code 5:
Copy Code code as follows:

Code 5, adding user upgrade rules
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")) {
Contain bad language, prompt message send failure
}

Calling the data access layer
$dbTask = new Lworddbtask ();
$dbTask->append ($newLWord);
Add points to the user
$score = Getuserscore ($userName);
$score = $score + 5;
Saveuserscore ($userName, $score);
Upgrade to users
if (($score% 1000) = = 0) {
$level = Getuserlevel ($userName);
$level = $level + 1;
Saveuserlevel ($userName, $level);
}
}
};

With the increase in demand, we need to constantly modify the intermediary service layer code. But you should not be difficult to find, the more the need for intermediary service layer code is more and more huge! Finally, even if we use the three-tier structure of the development model, there is still no effective reduction of engineering difficulties! In addition to changing the intermediary service code in response to changes in requirements, you need to test all the code instead of the effective test new code ...

In fact, let's take a closer look at this message board code, first I want to put forward a main business logic and the concept of secondary business logic. In any case, the message content into the database, which is the backbone of the business logic! This is the main business logic! This part has not been modified as demand has increased. As to the database before the permission to check, to carry out content inspection, the database should be added to the user, and then to upgrade the user, these are the pre-order work and cleanup work, are the second business logic! The main business logic is almost static, and the secondary business logic changes very frequently. To improve the readability and maintainability of the code, we can consider putting these secondary business logic somewhere else and trying not to interfere with the main business logic. Main business logic concentrate on what you have to do. Well, as for anything else, the main business logic is indifferent! So our code can be written like this, as shown in code 6:
Copy Code code as follows:

Code 6, separating 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);
Calling the data access layer
$dbTask = new Lworddbtask ();
$dbTask->append ($newLWord);
After adding a message
Behindappend ($newLWord);
}
};

We can put the authorization code and message content text filter code into the Beforeappend function, the user integration code into the Behindappend function, so that the secondary business logic from the main business logic code to clean out. The main business logic knows there is a "prelude" function Beforeappend, there is a "end" function behindappend, but in the prelude and the end function of what the specific things, the main business logic does not know, also do not need to know! Of course, the actual coding work is not so simple, we have to take into account the product department and the market more demand changes, so it is best to implement a plug-in way to deal with this change, but only rely on two functions beforeappend and behindappend is not up to this goal ~

Want to implement plug-in mode, you can set up an interface! The advantage of using an interface is that it can isolate definitions and implementations, and in addition, to implement polymorphism. We set up a message extension interface ilwordextension, which has two functions beforeappend and behindappend. Permission checking, content checking, adding these functions can be considered as the implementation of the Ilwordextension interface of the three implementation classes, the main business logic in turn through the three implementation classes, to complete the secondary business logic. As shown in Figure 1:
Checkpowerextension extension classes are used as user rights checksums, Checkcontentextension extension classes are used as message content checks, Addscoreextension extension classes are used to add points and upgrades to users. The schematic code is shown in code 7:

(Figure 1), add the extension interface
Copy Code code as follows:

Code 7, adding 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) {
To judge user rights 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) {
Give the user points here
}
};

Intermediate service Layer
Class Lwordservicecore implements Ilwordservice {
Add a message
Public function Append ($newLWord) {
Before adding a message
$this->beforeappend ($newLWord);

Calling 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) {
Get an extended array
$extArray = $this->getextarray ();

foreach ($extArray as $ext) {
Iterate over each extension and call its beforeappend function
$ext->beforeappend ($newLWord);
}
}

After adding a message
Private Function Behindappend ($newLWord) {
Get an extended array
$extArray = $this->getextarray ();

foreach ($extArray as $ext) {
Iterate over each extension and call its behindappend function
$ext->behindappend ($newLWord);
}
}

Gets the extended array,
The return value of the function is actually an array of ilwordextension interfaces
Private Function Getextarray () {
Return Array (
Check Permissions
New Checkpowerextension (),
Check content
New Checkcontentextension (),
Bonus points
New Addscoreextension (),
);
}
};

If there are new requirements, we simply add the Ilwordextension implementation class and register it with the Getextarray function. The program has been organized and is scalable.

Current 1/2 page 12 Next read the full text

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.