Understanding rule Modes

Source: Internet
Author: User

I. Overview
The rule mode is used to process algorithms.Different variantsThe policy mode encapsulates the identity of an algorithm through an interface or abstract class, that isDefine an abstract method in the interface. classes that implement this interface will implement the abstract methods in the interface..
In policy mode,The interface that encapsulates the algorithm identity is called a policy, and the class that implements this interface is called a specific policy.
[Ps: different variants of processing algorithms refer to different variants of an algorithm, that is, there are multiple specific actions for a behavior]

For example, if an action is a fly object and an object with a fly behavior has a variety of flying methods, the fly is an interface as a policy, and how to fly is a Class-specific policy that implements this interface.



Ii. Structure and use of policy Modes
The rule mode structure includes three roles.
1. Strategy)
2. Specific Policy (ConcreteStrategy)
3. Context)


Analyze the uml class diagram
ConcreteStrategyA and B generalize the Strategy interface Context using the Strategy interface [PS: Correlation] to hold a reference of the Strategy interface, using polymorphism, you can call the methods of one of the classes A and B and encapsulate their member methods. It is safer to call the referenced Methods internally.


1. Strategy: Computable. java

public interface ComputableStrategy{  public abstract double computeScore(double [] a);} 

2. Specific Policy (ConcreteStrategy): StrategyOne. java
Public class StrategyOne implements ComputableStrategy {public double computeScore (double [] a) {double score = 0, sum = 0; for (int I = 0; I
2. Specific Policy (ConcreteStrategy): StrategyTwo. java
Public class StrategyTwo implements ComputableStrategy {public double computeScore (double [] a) {double score = 0, multi = 1; int n = a. length; for (int I = 0; I
2. Specific Policy (ConcreteStrategy): StrategyThree. java
Import java. util. arrays; public class StrategyThree implements ComputableStrategy {public double computeScore (double [] a) {if (. length <= 2) return 0; double score = 0, sum = 0; Arrays. sort (a); for (int I = 1; i3. context: GymnasticsGame. java
public class  GymnasticsGame{     ComputableStrategy strategy;     public void setStrategy(ComputableStrategy strategy){         this.strategy=strategy;     }      public double getPersonScore(double [] a){        if(strategy!=null)          return strategy.computeScore(a);         else          return 0;     }}
4. Application: Application. java_1
Public class Application {public static void main (String args []) {GymnasticsGame game = new GymnasticsGame (); game. setStrategy (new StrategyOne (); Person zhang = new Person (); zhang. setName ("Zhang San"); double [] a = {9.12, 9.25, 8.87, 9.99, 6.99}; Person li = new Person (); li. setName ("Li Si"); double [] B = {9.15, 9.26, 8.97, 9.89, 6.97}; zhang. setScore (game. getPersonScore (a); li. setScore (game. getPersonScore (B); System. out. println ("solutions using arithmetic mean:"); System. out. printf ("% s last score: % 5.3f % n", zhang. getName (), zhang. getScore (); System. out. printf ("% s last score: % 5.3f % n", li. getName (), li. getScore (); game. setStrategy (new StrategyTwo (); zhang. setScore (game. getPersonScore (a); li. setScore (game. getPersonScore (B); System. out. println ("solutions using geometric mean:"); System. out. printf ("% s last score: % 5.3f % n", zhang. getName (), zhang. getScore (); System. out. printf ("% s last score: % 5.3f % n", li. getName (), li. getScore (); game. setStrategy (new StrategyThree (); zhang. setScore (game. getPersonScore (a); li. setScore (game. getPersonScore (B); System. out. println ("use (remove the highest and lowest) arithmetic mean scheme:"); System. out. printf ("% s last score: % 5.3f % n", zhang. getName (), zhang. getScore (); System. out. printf ("% s last score: % 5.3f % n", li. getName (), li. getScore () ;}} class Person {String name; double score; public void setScore (double t) {score = t;} public void setName (String s) {name = s;} public double getScore () {return score;} public String getName () {return name ;}}

3. Context and ConcreteStrategy are loosely coupled. Therefore, ContextYou only know that you want to use an instance that implements the Strategy interface class, but you do not need to know which class is specific.
The policy mode meets the "On-Off" principle ". When a new policy is added, the context can reference instances of the new policy without modifying the code of the context class.

4. The design pattern around us: java. util. Comparator interface [Comparator]

public interface Comparator
    
      {      int compare(T o1, T o2);       boolean equals(Object obj);}
    

We use different comparison methods for different objects. This interface is a policy, and our implementation is a specific policy. The context holds a reference of the policy for convenient calls.


If you have any questions, please instruct and promote mutual learning!

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.