PHP design mode Series-Strategy mode

Source: Internet
Author: User

    • Policy mode:

The policy pattern design helps to build an object that does not have to contain logic on its own, but can leverage the algorithms in other objects as needed.

    • Usage scenarios:
      1. For example, there is a CD class, and our class stores the information for the CD.
      2. Originally, we called the Getcd method directly in the CD class to give the results of the XML
      3. As the business expands, the demand side presents the need for JSON data format output
      4. This time we introduced a strategy model that allows the user to freely choose whether to export XML or JSON based on demand.
    • code example:
<?PHP//Policy mode//CD class    classCD {protected $CDARR;  Public function__construct ($title,$info) {               $this->cdarr[' title '] =$title; $this->cdarr[' info '] =$info; }                     Public functionGETCD ($TYPEOBJ) {              return $TYPEOBJ->get ($this-Cdarr); }       }            classJSON { Public functionGet$return _data) {              returnJson_encode ($return _data); }      }            classXML { Public functionGet$return _data) {                  $xml= ' <?xml version= ' 1.0 ' encoding= ' utf-8 '?> '; $xml. = ' <return> '; $xml. = ' <data> '.Serialize($return _data). ' </data> '; $xml. = ' </return> '; return $xml; }      }            $CD=NewCD (' Cd_1 ', ' cd_1 '); Echo $CD-&GT;GETCD (NewJSON); Echo $CD-&GT;GETCD (NewXML);

Transferred from: http://blog.csdn.net/initphp/article/details/7760383

PHP design mode Series-Strategy mode

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.