PHP policy mode definition and usage example, php policy usage example

Source: Internet
Author: User

PHP policy mode definition and usage example, php policy usage example

This document describes the definition and usage of PHP policy patterns. We will share this with you for your reference. The details are as follows:

Overview

The rule mode is the behavior mode of the object. It is intended to encapsulate each algorithm into an independent class with a common interface for mutual replacement. The policy mode allows the algorithm to change without affecting the client.

UML diagram

Main roles in Rule Mode

This mode is designed for three roles:

Context: Hold a reference to Strategy
Abstract Policy (Strategy) Role: This is an abstract role, usually implemented by an interface or abstract class. This role provides all the interfaces required for specific policy classes
ConcreateStrategy role: encapsulates related algorithms or behaviors

Use Cases

Suppose we want to design a shopping cart system. The simplest case is to multiply the unit price of all goods by the quantity, but the actual situation is definitely more complicated than this. For example, there is no discount for ordinary members, a discount for intermediate members, and a discount for senior members.

According to the above description, the product price is based on one of the following algorithms:

No discounts for ordinary Members
Off for intermediate members
Off For senior members

UML diagram

Implementation Code:

<? Php/*** policy abstract class * @ author wzy **/interface Strategy {public function calPrice ($ price );} /*** common member Strategy ** @ author wzy **/class PrimaryStrategy implements Strategy {public function calPrice ($ price) {echo "Regular member no discount "; return $ price ;}/ *** intermediate member Strategy class ** @ author wzy **/class MiddleStrategy implements Strategy {public function calPrice ($ price) {echo "0.8 off for intermediate members"; return $ price * ;}}/*** advanced membership strategy class * * @ Author wzy **/class HighStrategy implements Strategy {public function calPrice ($ price) {echo "0.7 off discount for senior members"; return $ price ;}} /*** Context implementation class ** @ author wzy **/class Price {/*** specific policy class object ** @ var object */private $ strategyInstance; /*** constructor: input a specific policy object ** @ param object $ instance */public function _ construct ($ instance) {$ this-> strategyInstance = $ instance;}/*** calculate the price of the product **@ Param double $ price */public function quote ($ price) {return $ this-> strategyInstance-> calPrice ($ price );}} /*** client operation */$ high = new HighStrategy (); $ priceClass = new Price ($ high); $ price = $ priceClass-> quote (400 ); echo $ price;?>

Review Policy Mode

The focus of the policy mode is not how to implement algorithms, but how to organize and call these algorithms to make the program structure more flexible, with better scalability and maintainability.

Equality of Algorithms

A major feature of the policy model is the equality of each policy algorithm. For a series of specific policy algorithms, the status is the same, so that the algorithms can be converted to each other.

Uniqueness of runtime policies

During running, the policy mode can only use one specific policy to implement objects at a time. Although it can be dynamically switched in different policy implementations, there is only one

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.