PHP object-oriented policy mode and php object-oriented Policy

Source: Internet
Author: User

PHP object-oriented policy mode and php object-oriented Policy

In my personal understanding, the rule mode separates the code used repeatedly in the class to form a policy class. If other classes want to call it, first, the policy class is passed as a parameter during initialization to form a composite relationship, and then the logic in this Policy class can be directly called within the class.

To put it simply, let's look at the combination and aggregation relationships of classes (this relationship has been quite confusing for a long time, the Code is too much, and some materials are just a little conceptual, the combination of classes is similar to a person. If a person dies, all the organs in the body will die. This organ can be understood as a strategy. If the lifecycle of a policy class ends, the lifecycle of the Policy class ends and cannot be used independently. The aggregation relationship is similar to a computer. If the computer breaks down, some parts of the computer can be used again (this relationship will have a clear concept when you see the combination mode ).

Now let's look at the original code in the book (in-depth PHP object-oriented mode and practice). Although the book says it is associated with the code in the previous interpreter mode, you can ignore it, think of it as an independent part to understand it.

// An abstract base class can be imagined as a person, and a strategy class is an organ in the human body.

Abstract class Question {
Protected $ prompt;
Protected $ marker; // policy class

Function _ construct ($ prompt, Marker $ marker) {// inherit from his class and pass in the Policy class during initialization
$ This-> marker = $ marker;
$ This-> prompt = $ prompt;
}

Function mark ($ response ){
Return $ this-> marker-> mark ($ response); // here it executes the logic of the Policy class
}
}

// The following two classes inherit Question, which respectively have their own business logic. According to the original article, they will process the text information and multimedia information entered by users respectively.

Class TextQuestion extends Question {
// No sample code is provided in the original text here, which only describes the business logic that you imagine. In fact, you don't have to worry too much. After all, it is mainly to understand the concept of the model.
}

Class AVQuestion extends Question {

}

// The following class is the policy class. If the TextQuestion and AVQuestion classes mentioned above have repeated code, you can separate the code into the policy class.

// Why do you want to separate them? For example, if you want to extend BVQuestion, CVQuestion classes all use a method (with the same or similar business logic), such as function mark ($ response)

// Copy and paste the function mark ($ response) function for every extension of a class. You will have a headache when modifying this Mark method.

Abstract class Marker {
Protected $ test;

Function _ construct ($ test ){
$ This-> test = $ test;
}


// The abstract method mark is defined here. The inherited class must be implemented, that is, different implementations are required for different user input information.
Abstract function mark ($ response );
}

Class MarkLogicMarker extends Marker {
Private $ engine;
Function mark ($ response) {// process the marklogic Language
Return true;
}
}

Class MatchMarker extends Marker {
Function mark ($ response ){
Return ($ this-> test = $ response); // You can process a user input that is equal to a value.
}
}

Class RegexpMarker extends Marker {
Function mark ($ response ){
Return (preg_match ($ this-> test, $ response); // process user input that requires regular expressions
}
}

// Client code
$ Markers = array (
New RegexpMarker ("/f. ve/"), // defines the preceding policy classes. These policy classes will be used for testing in the following code.
New MatchMarker ("five "),
New MarkLogicMarker ('$ input equals "five "')
);
Foreach ($ markers as $ marker ){
Print get_class ($ marker). "<br/> ";
$ Question = new TextQuestion ("how many beans make five", $ marker );
Foreach (array ("five", "four") as $ response) {// The array ("five", "four") Here simulates user input information
Print "\ tresponse: $ response :";
If ($ question-> mark ($ response )){
Print "well done <br/> ";
} Else {
Print "never mind <br/> ";
}
}
}

Copy and paste the above Code and run it to see the result! All of the above may be nonsense. You can directly run the code to see if the learning effect is much better.

Finally: the road is long and the distance is long!

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.