java设计模式---策略模式
策略模式使用场景
主要是在遇到如何让算法和对象分开来,使得算法可以独立于使用它的客户而变化?
采用策略模式主要思想:
把一个类中经常改变或者将来可能改变的部分提取出来,作为一个接口,然后在类中包含这个对象的实例,这样类的实例在运行时就可以随意调用实现了这个接口的类的行为
例子
专门用于排序的类DataSorter中实现对所有A类、B类....等等进行排序
初步想法
DataSorter.java的代码如下
public class DataSorter {
/* public static void sort(A a){
}
public static void sort(B a){
}
public static void sort(C c){
}*/
}
基于上面的做法会造成DataSorter的可扩展性差,要支持对新类的排序时,要修改代码
更好的办法
更好的思路是:既然DataSorter是要根据不同的类的采取不同的方法实现排序,那么具体实现排序的方法交由子类去实现,且都实现同一个接口Comparable,那么DataSorter只需对Comparable排序,而无需理会具体要排序的是
类之间的关系
1具体代码如下:
1.DataSorter
/**
*目标:专门用于排序的类DataSorter中实现对所有A类、B类....等等进行排序
* 如果直接在Datasorter中使用对A、B、C类排序这样扩展行差,要支持对新类的排序时,要修改代码
* Created by td on 2017/8/29.
* 更好的思路是::既然DataSorter是要根据不同的类的采取不同的方法实现排序,那么具体实现排序的方法交由子类去实现,且都实现同一个接口Comparable,那么DataSorter只需对Comparable排序,而无需理会具体要排序的是
*/
public class DataSorter {
public static void sort(Comparable [] a) {
int index; //保存每次比较,最大值的下标;
for(int i = 1; i < a.length; i++){ //控制外循环次数
index = 0;
for(int j = 1; j <= a.length - i ; j++){
if(a[j].compareTo(a[index]) == 1){
index = j;
}
}
swap(a, index, a.length -i);
}
}
private static void swap(Comparable[] a, int x, int y) {
Comparable tmp = a[x];
a[x] = a[y];
a[y] = tmp;
}
//输出数组元素
public static void show(Comparable[] a) {
for(int i = 0; i < a.length; i++){
System.out.println(a[i]);
}
}
/* public static void sort(A a){
}
public static void sort(B a){
}
public static void sort(C c){
}*/
/*
//冒泡排序法
//主要思路:按升序排序,数组元素两两比较,大的立即排后面
public static void bubbleSort(int[] a) {
for (int i=0;i<a.length;i++){
int index = i;
for (int j = i;j<a.length;j++){
if (a[index]>a[j]){
index = j;
}
}
if (index!=i){
swap(a,i,index);
}
}
}
//直接选择排序法
//主要思路:按升序排序,每次循环找出最小数,把他放到第i位置上
public static void selectSort(int[] array) {
}
//交换数组元素
private static void swap(int[] a, int x, int y) {
int tmp = a[x];
a[x] = a[y];
a[y] = tmp;
}
//输出数组元素
public static void show(int[] a) {
for(int i = 0; i < a.length; i++){
System.out.println(a[i]);
}
}
public static void main(String[] args) {
int[] a = {1,4,0,5,1,2,3,10,9,2};
bubbleSort(a);
show(a);
}
*/
}
2.定义排序接口Comparable
/**
* Created by td on 2017/8/29.
*/
public interface Comparable<T> {
public int compareTo(T o);
}
3.定义student并实现Comparable
/**
* Created by td on 2017/8/29.
*/
public class Student implements Comparable<Student> {
private int mark;
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
public Student(int mark) {
super();
this.mark = mark;
}
@Override
public String toString() {
return "student" +mark+" ";
}
@Override
public int compareTo(Student o) {
if(this.mark > o.getMark()){
return 1;
}else if(this.mark == o.getMark()){
return 0;
}else{
return -1;
}
}
}
4.定义Teacher并实现Comparable
public class Teacher implements Comparable<Teacher> {
private int title;
public Teacher(int title) {
super();
this.title = title;
}
public int getTitle() {
return title;
}
public void setTitle(int title) {
this.title = title;
}
@Override
public int compareTo(Teacher o) {
if(this.title > o.getTitle()){
return 1;
}else if(this.title == o.getTitle()){
return 0;
}else{
return -1;
}
}
@Override
public String toString() {
return "teacher--" +title+" ";
}
}
- 定义测试类
public class Test {
public static void main(String[] args) {
Student[] students = {new Student(10),new Student(5),new Student(100)};
DataSorter.sort(students);
DataSorter.show(students);
System.out.println("----------------------");
Teacher [] ts = {new Teacher(10),new Teacher(3),new Teacher(12)};
DataSorter.sort(ts);
DataSorter.show(ts);
}
}
6.输出结果
student5
student10
student100
----------------------
teacher--3
teacher--10
teacher--12
网友评论