标签:
策略模式意义在于,当我们有多种不同的算法时,程序能够动态的选择算法进行计算。
策略模式的另外一个英文名字是Policy Strategy。
策略模式最典型的里一个例子就是JAVA中复杂对象的排序(java.util.Arrays.sort,java.util.Collections.sort),用户可以自己定义各种排序算法(规则),然后在需要排序时将算法传给排序函数。从而实现排序方式的变化,获得不同的排序结果。参见:http://my.oschina.net/nox/blog/487478
下面是一个计算工资的策略模式例子,分别对不同的三种角色(普通员工,经理,销售人员)制定了不同的工资计算算法,最终得到不同的计算结果。
代码如下:
package dp.strategy; public class Account { private int userId; private String userType; private String userName; private int baseSalary; public Account(int userId, String userType, String userName, int baseSalary) { this.userId = userId; this.userType = userType; this.userName = userName; this.baseSalary = baseSalary; } public int getBaseSalary() { return baseSalary; } public void setBaseSalary(int baseSalary) { this.baseSalary = baseSalary; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserType() { return userType; } public void setUserType(String userType) { this.userType = userType; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
package dp.strategy; /* * 工资计算算法接口 */ public interface SalaryAlgorithm{ public int getSalary(Account account); }
package dp.strategy; /* * 普通员工的工资计算算法 */ public class EmployeeSalaryAlgorithm implements SalaryAlgorithm{ @Override public int getSalary(Account account) { return account.getBaseSalary(); } }
package dp.strategy; /* * 经理级人员的工资计算算法 */ public class ManagerSalaryAlgorithm implements SalaryAlgorithm{ @Override public int getSalary(Account account) { return account.getBaseSalary() * 2; } }
package dp.strategy; /* * 销售人员的工资计算算法 */ public class SalesmanSalaryAlgorithm implements SalaryAlgorithm { @Override public int getSalary(Account account) { return account.getBaseSalary() + 2000; } }
package dp.strategy; public class Main{ public static void main(String args[]){ Account employee1 = new Account(1,"普通员工","张三",2500); Account manager1 = new Account(2,"部门经理","李总",6000); Account manager2 = new Account(3,"部门经理","罗总",7500); Account salesman1 = new Account(4,"销售","老杨",3500); Account salesman2 = new Account(5,"销售","小吴",4200); SalaryAlgorithm employeeSalaryAlgorithm = new EmployeeSalaryAlgorithm(); SalaryAlgorithm managerSalaryAlgorithm = new ManagerSalaryAlgorithm(); SalaryAlgorithm salesmanSalaryAlgorithm = new SalesmanSalaryAlgorithm(); paySalary(employee1, employeeSalaryAlgorithm); paySalary(manager1, managerSalaryAlgorithm); paySalary(manager2, managerSalaryAlgorithm); paySalary(salesman1, salesmanSalaryAlgorithm); paySalary(salesman2, salesmanSalaryAlgorithm); } private static void paySalary(Account account, SalaryAlgorithm salaryAlgorithm){ System.out.println("员工:"+account.getUserName()+", 他的基本工资是"+account.getBaseSalary()+" ,计算之后的实发工资是"+salaryAlgorithm.getSalary(account)); } }
员工:张三, 他的基本工资是2500 ,计算之后的实发工资是2500 员工:李总, 他的基本工资是6000 ,计算之后的实发工资是12000 员工:罗总, 他的基本工资是7500 ,计算之后的实发工资是15000 员工:老杨, 他的基本工资是3500 ,计算之后的实发工资是5500 员工:小吴, 他的基本工资是4200 ,计算之后的实发工资是6200
标签:
原文地址:http://my.oschina.net/nox/blog/487803