This article describes the definition and usage of Java policy patterns. Share it for your reference, as follows:
1. Definition:
Define a series of algorithms, encapsulate them one by one, and enable them to be replaced by each other.
2. Advantages:
(1) Context and specific strategy are loosely coupled, so the context only needs to know which instance it wants to use to implement the Strategy interface class, but does not need to know which class it is.
(2) The policy model meets the principle of opening and closing. When adding a new specific class, there is no need to modify the code of the context class. The context can refer to an instance of the new specific policy.
III. Example:
The following is a detailed explanation of the strategy model through a question.
Experimental requirements:
Xiao Ding is a programmer in Alibaba. This day the project manager gave him a class definition
public class Worker { int id; String name; int age;}Xiao Ding saw that this was not the definition of a worker, including number id, name, age, etc.
The manager said, I need you to write a function that can find the oldest among a group of workers. The method declaration is (or something like this):
Worker searchWorker(List<Worker> workers);
The parameter List<Worker> workers are the data of this batch of workers. If the oldest worker is found, the Worker object will be returned. If workers have no nodes, return null.
Answer:
1. Strategy
In the policy mode, this interface is named WorkStrategy (in specific questions, this name can be named according to specific questions), and the code is as follows:
WorkStrategy.java
import java.util.List;public interface WorkStrategy { public Worker AgeAverage(List<Worker> list);}2. Context
Context-oriented policy is both an interface-oriented class; the code is as follows:
MaxAge.java
import java.util.List;public class MaxAge { WorkStrategy workstrategy; public void SetWorkStrategy(WorkStrategy workstrategy){ this.workstrategy=workstrategy; } public Worker getFindMax(List<Worker> list){ if(workstrategy!=null) return workstrategy.AgeAverage(list); else{ return null; } }}3. Specific strategies
The specific strategy is to implement the WorkStrategy interface class, that is, the abstract Worker in the interface must be rewrite.
AgeAverage(List<Worker> list) method
The code is as follows:
StrategyMaxAge.java
import java.util.List;public class StrategyMaxAge implements WorkStrategy{ @Override public Worker AgeAverage(List<Worker> list) { int fs[] = new int[list.size()]; int maxage = 0; for (int j = 0; j < list.size(); j++) { fs[j]=list.get(j).getAge(); if (fs[maxage] <= fs[j]) { maxage = j; } } return list.get(maxage); }}4. Use of policy mode
Application.java
import java.util.ArrayList;import java.util.List;public class Application { public static void main(String[] args) { List<Worker> list = new ArrayList<>(); list.add(new Worker(1, "Zhang San", 30)); list.add(new Worker(2, "Li Si", 40)); list.add(new Worker(3, "Wang Wu", 33)); MaxAge findage = new MaxAge(); findage.SetWorkStrategy(new StrategyMaxAge()); Worker findw = findage.getFindMax(list); System.out.println("Wulin.com test results:"); System.out.println("The oldest worker:"+findw.getId()+" "+findw.getName()+" "+findw.getAge()); }}class Worker{ int id; String name; int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age =age; } public Worker(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public Worker() { super(); // TODO Auto-generated constructor stub }}5. Operation results:
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.