The strategy mode of PHP design mode

Source: Internet
Author: User
Tags define abstract

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

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.