Policy mode:
The policy pattern is the behavior pattern of an object, which is intended to encapsulate a set of algorithms. Dynamic selection of the desired algorithm and use.
The strategy model refers to a pattern that involves decision control in a program. The strategy mode function is very powerful, because the core idea of the design pattern itself is the multi-shape idea of object-oriented programming .
Three roles for the policy mode:
1. Abstract policy Roles
2. Specific policy Roles
3. Environment role (reference to abstract policy role)
Implementation steps:
1. Define abstract role classes (common abstractions that define each implementation)
2. Define a specific policy class (a common way to implement the parent class specifically)
3. Define Environment role classes (Privatization declaration abstract role variables, overloaded construction methods, execution of abstract methods)
Code examples for policy mode:
< php
Abstract class Baseagent {// Abstraction policy class
Abstract function PrintPage ();
}
Class to invoke when the client is IE (Environment role)
Class Ieagent extends Baseagent {
function PrintPage () {
Return ' IE ';
}
}
Class to invoke when the client is not IE (Environment role)
Class Otheragent extends Baseagent {
function PrintPage () {
Return ' not IE ';
}
}
Class Browser {// specific policy roles
Public function call ($object) {
return $object->printpage ();
}
}
$bro = new Browser ();
Echo $bro->call (New Ieagent ());
>
Outside of the programming world, there are many examples of policy patterns. For example:
If I need to go to work from home in the morning, I can have a few strategies to consider: I can take the subway, take a bus, walk or other way. Each policy can get the same results, but different resources are used.
The strategy mode of PHP design mode