Policy-makers mode
/**
- Class Name: Tactics
Description: The strategist class, add a new data format as needed, only need to define a new class name (such as HTMLData), the method name (GetData) is not changed, the specific content of the method can be implemented
*/
Class Tactics {
Example Array
protected $demoList;
Public Function __construct () {
}
/*
- Add data
*/
Public Function AddData ($key, $value) {
$this->demolist[$key] = $value;
}
/*
- Output different data formats based on data-format objects
*/
Public Function Gettactics ($obj) {
return $obj->getdata ($this->demolist);
}
}
/**
- Class Name: Jsondata
- Description: Data in JSON format
*/
Class Jsondata {
Public Function GetData ($data) {
Return Json_encode ($data);
}
}
/**
- Class Name: XmlData
- Description: Data in XML format
*/
Class XmlData {
Public Function GetData ($data) {
$xml = ' <?xml version= ' 1.0 "encoding=" Utf-8 "?>";
$xml. = ' <data> ';
foreach ($data as $key = = $value) {
$xml. = ' <group> ';
$xml. = ' <key> ' $key. ' </key> ';
$xml. = ' <value> ' $value. ' </value> ';
$xml. = ' </group> ';
}
$xml. = ' </data> ';
return $xml;
}
}
Instantiating an object of a policy-person class
$TACTICSOBJ = new Tactics ();
Assigning a value to a property in a class
$TACTICSOBJ->adddata (' name ', ' Zhang San ');
$TACTICSOBJ->adddata (' Age ', ' 18 ');
Pass an object of type Jsondata, get the JSON data format
Echo $TACTICSOBJ->gettactics (new Jsondata);
Pass an object of type XMLData, get the XML data format
Echo $TACTICSOBJ->gettactics (new XmlData);
A small example of a policy-makers pattern