Java design mode-policy mode

Source: Internet
Author: User

Defined

The policy pattern defines a series of algorithms, encapsulates each algorithm, and allows them to be replaced with each other. The policy pattern makes the algorithm independent of the customers who use it.

Recognize the center of gravity of strategy mode strategy mode

The focus of the strategy pattern is not how to implement the algorithm, but how to organize, call these algorithms, so that the program structure more flexible, with better maintainability and extensibility.

Equality of the algorithm

A big feature of the strategy model is the equality of each strategy algorithm. For a series of specific strategy algorithms, everyone's status is exactly the same, because of this equality, can be achieved between the algorithm to replace each other. All strategy algorithms are independent of each other in implementation, and are not dependent on each other.

So you can describe this series of strategy algorithms: Policy algorithms are different implementations of the same behavior.

Uniqueness of the run-time policy

During operation, the policy mode can only use one specific policy implementation object at a time, although it is possible to dynamically switch between different policy implementations, but only one can be used at a time.

The act of the public

It is common to see that all specific policy classes have some public behavior. At this time, the public behavior should be put into the common abstract strategy role strategy class inside. Of course, abstract policy roles must be implemented in Java abstract classes, not interfaces.

This is also a typical standard practice of centralizing the code to inherit hierarchy.
  

Composition

-Abstract policy role: A policy class, usually implemented by an interface or abstract class.
-Specific policy role: wrapping the relevant algorithms and behaviors.
-Environment role: holds a reference to a policy class that is eventually called to the client.

Concept

1, need to use the algorithm provided by Concretestrategy.
2, internal maintenance of a strategy instance.
3, is responsible for the dynamic set run time strategy concrete implementation algorithm.
4, responsible for the interaction and data transfer with strategy.

Strategy (Abstract policy Class):

1, defines a public interface, a variety of different algorithms to implement this interface in different ways, the context uses this interface to invoke different algorithms, generally using an interface or abstract class implementation.

Concretestrategy (Specific policy Class):

2, realizes the strategy definition interface, provides the concrete algorithm realization.

Application Scenarios

1, multiple classes only differ in performance behavior, you can use the Strategy mode, at run time to dynamically select the specific behavior to execute.
2. Different strategies (algorithms) need to be used in different situations, or strategies may be implemented in other ways in the future.
3, the customer hides the specific strategy (algorithm) Implementation details, each other completely independent.

Advantages and Disadvantages:

1. The strategy mode provides a way to manage related algorithm families. The hierarchy structure of a policy class defines an algorithm or a family of behaviors. Proper use of inheritance can transfer common code to the parent class, thus avoiding duplicate code.

2. The policy mode provides a way to replace the inheritance relationship. Inheritance can handle multiple algorithms or behaviors. If the policy mode is not used, then the environment class using the algorithm or behavior may have some subclasses, each of which provides a different algorithm or behavior. However, the user of the algorithm or behavior is mixed with the algorithm or the behavior itself. The logic that determines which algorithm to use or which behavior to take is mixed with the logic of the algorithm or behavior, so that it is impossible to evolve independently. Inheritance makes it impossible to dynamically change an algorithm or behavior.

3. Use the policy mode to avoid using multiple conditional transfer statements. Multiple transfer statements are difficult to maintain, and it mixes the logic of which algorithm or behavior is taken with the logic of the algorithm or the action, all in a multiple transfer statement, more primitive and backward than the method of using inheritance.

Disadvantages:

1. The client must know all of the policy classes and decide which policy class to use at its own discretion. 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.

2, the policy mode causes a lot of policy classes, each specific policy class will produce a new class. It is sometimes possible to design a policy class to be shareable by saving the environment-dependent state to the client, so that the policy class instance can be used by different clients. In other words, you can use the enjoy meta mode to reduce the number of objects.

Application Case UML Diagram

Code
<?php/*** Policy Mode * Defines a series of algorithms that encapsulate each algorithm and allow them to be replaced by each other. * This mode allows the algorithm to be independent of the customer changes that use it *//*** Travel Tour * *Interface travelstrategy{ PublicfunctionTravelalgorithm();}/*** Specific Strategy class (Concretestrategy) * 1: By plane */Class Airplanelstrategy implementstravelstrategy{ PublicfunctionTravelalgorithm() {Echo"Travelbyairplain","<br>\r\n"; }}/*** Specific Strategy class (Concretestrategy) * 2: Take the train * *Class Trainstrategy implementstravelstrategy{ PublicfunctionTravelalgorithm() {Echo"Travelbytrain","<br>\r\n"; }}/*** Specific Strategy class (Concretestrategy) * 3: Ride a bike */Class Bicyclestrategy implementstravelstrategy{ PublicfunctionTravelalgorithm() {Echo"Travelbybicycle","<br>\r\n"; }}The /**** Environment Class (context):* configured with a Concretestrategy object. * maintains a reference to the Strategy object. You can define an interface to let strategy access its data. * Algorithm solution class to provide the solution that the customer chooses to use: */Class personcontext{Private$_strategy =NULL; Publicfunction__construct(Travelstrategy$travel) {        $ This->_strategy= $travel; }/** * Travel * *     PublicFunctionsetTravelstrategy(Travelstrategy$travel) {        $ This->_strategy= $travel; }/** * Travel * *     Publicfunction Travel(){return$ This->_strategy->travelalgorithm (); }}//Train travel$person =newpersoncontext (Newtrainstrategy ()); $person->travel ();//Change bike$person->settravelstrategy (Newbicyclestrategy ()); $person->travel ();? >
My QR code is as follows, welcome to exchange discussion

You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:

Reference:
Http://baike.baidu.com/view/2141079.htm
Http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html

Java design mode-policy mode

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.