A small example of a policy-makers pattern

Source: Internet
Author: User

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

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.