标签:
1 package com.cnblogs.vicentzh.strategymodel; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.Iterator; 7 import java.util.List; 8 9 public class StrategyModel 10 { 11 /** 12 * @Describe 客户端程序 13 * @param args 14 */ 15 public static void main(String[] args) 16 { 17 Student p1 = new Student("Tom",1,20); 18 Student p2 = new Student("Tonny",2,50); 19 Student p3 = new Student("Tom",5,30); 20 Student p4 = new Student("John",8,10); 21 Student p5 = new Student("Susan",9,15); 22 23 List<Student> students = new ArrayList<Student>(); 24 students.add(p1); 25 students.add(p2); 26 students.add(p3); 27 students.add(p4); 28 students.add(p5); 29 30 Context env = new Context(); 31 32 //正序排列 33 UpNameSort uns = new UpNameSort(); 34 env.setSortStrategy(uns); 35 env.sort(students); 36 37 for (Iterator<Student> iter=students.iterator(); iter.hasNext();) 38 { 39 Student student = iter.next(); 40 System.out.println("id: " + student.getId() + ", name: " + student.getName() 41 + ", age:" + student.getAge()); 42 } 43 System.out.println("-----------------------"); 44 45 //倒序排列 46 DownNameSort dns = new DownNameSort(); 47 env.setSortStrategy(dns); 48 env.sort(students); 49 50 for (Iterator<Student> iter=students.iterator(); iter.hasNext();) 51 { 52 Student student = iter.next(); 53 System.out.println("id: " + student.getId() + ", name: " + student.getName() 54 + ", age:" + student.getAge()); 55 } 56 57 } 58 } 59 60 61 //需要用到的具体实例类 62 class Student 63 { 64 private String name; 65 private int age; 66 private int id; 67 68 public Student(String name, int age, int id) 69 { 70 this.name = name; 71 this.age = age; 72 this.id = id; 73 } 74 75 public String getName() 76 { 77 return name; 78 } 79 public int getAge() 80 { 81 return age; 82 } 83 public int getId() 84 { 85 return id; 86 } 87 } 88 89 90 //抽象策略类(Strategy),即策略接口 91 interface SortStrategy 92 { 93 public void sortStudent(List<Student> students); 94 } 95 96 97 //具体策略类(ConcreteStrategy),即具体正序算法实现类 98 class UpNameSort implements SortStrategy,Comparator<Student> 99 { 100 @Override 101 public void sortStudent(List<Student> students) 102 { 103 Collections.sort(students, this); 104 } 105 106 @Override 107 public int compare(Student o1, Student o2) 108 { 109 int result = o1.getName().compareTo(o2.getName()); 110 if(0==result) 111 { 112 return o1.getId() - o2.getId(); 113 } 114 return result; 115 } 116 } 117 118 //具体策略类(ConcreteStrategy),即具体倒序算法实现类 119 class DownNameSort implements SortStrategy, Comparator<Student> 120 { 121 @Override 122 public void sortStudent(List<Student> students) 123 { 124 Collections.sort(students, this); 125 126 } 127 128 @Override 129 public int compare(Student o1, Student o2) 130 { 131 int result = o2.getName().compareTo(o1.getName()); 132 if(0==result) 133 { 134 return o1.getId() - o2.getId(); 135 } 136 return result; 137 } 138 } 139 140 //使用环境类(Context) 141 //环境类根据接收到客户端具体的策略来对对象进行使用,同样也能用setSortStrategy方法 142 //随时根据客户端的需求去改变策略算法,并且不影响对象。 143 class Context 144 { 145 private SortStrategy concreteStrategy; //使用策略配置环境类 146 147 public Context(SortStrategy conSortStrategy) 148 { 149 this.concreteStrategy = conSortStrategy; 150 } 151 152 public Context() 153 { 154 155 } 156 157 //可随意定制化具体策略 158 public void setSortStrategy(SortStrategy conSortStrategy) 159 { 160 this.concreteStrategy = conSortStrategy; 161 } 162 163 //使用具体的策略(concreteStrategy)对对象进行操作 164 public void sort(List<Student> students) 165 { 166 concreteStrategy.sortStudent(students); 167 } 168 }
标签:
原文地址:http://www.cnblogs.com/vincentzh/p/5964947.html