A detailed description of the strategy model of PHP design mode

Source: Internet
Author: User
Policy Mode, encapsulating a specific set of behaviors and algorithms into classes to accommodate certain contexts, a pattern that Policy ModePolicy ModeFor the free switching and expansion of the algorithm, it is widely used Design PatternsOne.

<?php/* * Policy mode */interface flybehavior{public    function Fly (); Class Flywithwings implements flybehavior{public    function Fly ()    {        echo "duck flies with wings \ n";    }} Class Flywithno implements flybehavior{public    function Fly ()    {        echo "duck flies without wings \";    }} Class duck{    private $_flybehavior;    Public Function Performfly ()    {        $this->_flybehavior->fly ();    }    Public Function Setflybehavior (Flybehavior $behavior)//external call to set policy    {        $this->_flybehavior = $behavior;    } }class Rubberduck extends duck{}//Test case$duck = new Rubberduck ();  /* Want the Ducks to fly with their wings */$duck->setflybehavior (new Flywithwings ()); $duck->performfly ();/*  want the Ducks to fly without wings */$duck->setflybehavior (New Flywithno ()); $duck Performfly ();

The policy mode is used for the free switching and expansion of the algorithm, which is one of the most widely used design patterns. The policy pattern corresponds to an algorithm family that solves a problem, allows the user to choose an algorithm from the algorithm family to solve a problem, and can easily replace the algorithm or add a new algorithm. You can consider using policy mode as long as the encapsulation, multiplexing, and switching of the algorithms are involved.

Related recommendations:

PHP policy pattern definition and usage examples

Code Sharing for PHP policy mode

The PHP strategy mode of StarCraft

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.