The strategy mode of implementing design pattern in PHP

Source: Internet
Author: User

Strategy mode: A series of algorithms are defined to encapsulate each algorithm and make them interchangeable with each other. This mode allows the algorithm to be independent of the customers who use it. is a pattern of behavior.

Policy mode contains three roles

1 Abstract policy role: A policy class, usually implemented by an interface or abstract class. 2 Specific policy roles: the related algorithms and behaviors are packaged. 3 Environment role: holds a reference to a policy class that is eventually called to the client.

For example: There are various sorts of methods, I can write a sort of class, each sort algorithm write a method, when the client calls, know each method can. However, when a new algorithm is added, or an algorithm is rewritten, the algorithm class must be modified. When the algorithm class is large, it becomes difficult to maintain.

The policy pattern distinguishes between the object itself (the configuration Class) and the algorithm class (the specific algorithm class). So the modification of the algorithm class, new, not related to other classes of modification, but the user can replace the algorithm itself.

<?php/* * Strategy mode: Define a series of algorithms, and encapsulate each algorithm, and make them interchangeable with each other * policy mode allows the algorithm to be independent of the client using it *///abstract policy interface, to accomplish something interface category{ Public function dosomething ();} Specific algorithm class, to achieve the specific thing class Category_a implements Category{public function DoSomething () {echo ' do a ';}} Class Category_b implements Category{public function DoSomething () {echo ' do B ';}} Class Category_c implements Category{public function DoSomething () {echo ' do C ';}} Configuration class, using the abstract policy interface to configure Class Context{public $CG;p ublic function __construct (category $a) {$this->CG = $a;} Public Function Dodo () {return $this->cg->dosomething ();//The same method acts on different classes of objects, producing different results, which in PHP is a polymorphic}}//client call, It is up to the customer to decide which strategy to use, that is, the customer instantiates the algorithm class themselves. Different from the simple Factory mode//Simple Factory mode is the creation mode of the object, the client does not create the object, only give the parameters, by the factory method to decide which instance to create//that is, the simple Factory mode client only passes parameters, the policy mode client Pass algorithm instance $m = new Context (new Category_b ()); $m->dodo ();? >

The above implements the policy mode.

Now I'm going to add an algorithm, do D; I just need to write a new class

Class Category_d implements Category{public function DoSomething () {echo ' do d ';}}

Client call, replace with D.

$m = new Context (new Category_b ());

Distinguish it from the simple factory model (see Simple Factory mode article).

Policy Mode Disadvantages:

The client must know all the policy classes and decide for themselves which policy class to use. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows all the algorithms or behaviors.

Use strategy mode when the following conditions are present
1) • Many of the related classes are simply behavior-specific. Policy provides a way to configure a class with one behavior in multiple behaviors. That is, a system needs to dynamically select one of several algorithms.
2) • Need to use different variants of an algorithm. For example, you might define algorithms that reflect different spatial/temporal tradeoffs. When these variants are implemented as a class hierarchy of an algorithm, the policy mode can be used.
3) • The algorithm uses data that the customer should not know about. You can use the policy mode to avoid exposing complex, algorithmic-related data structures.
4) • A class defines a variety of behaviors, and these behaviors appear as multiple conditional statements in the operation of this class. The related conditional branches are moved into their respective strategy classes in place of these conditional statements.

The strategy mode of implementing design pattern in PHP

Related Article

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.