java設(shè)計模式---策略模式
策略模式使用場景
主要是在遇到如何讓算法和對象分開來洲赵,使得算法可以獨立于使用它的客戶而變化?
采用策略模式主要思想:
把一個類中經(jīng)常改變或者將來可能改變的部分提取出來叔收,作為一個接口,然后在類中包含這個對象的實例跨琳,這樣類的實例在運行時就可以隨意調(diào)用實現(xiàn)了這個接口的類的行為
例子
專門用于排序的類DataSorter中實現(xiàn)對所有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是要根據(jù)不同的類的采取不同的方法實現(xiàn)排序窒升,那么具體實現(xiàn)排序的方法交由子類去實現(xiàn),且都實現(xiàn)同一個接口Comparable慕匠,那么DataSorter只需對Comparable排序饱须,而無需理會具體要排序的是
類之間的關(guān)系
1
具體代碼如下:
1.DataSorter
/**
*目標:專門用于排序的類DataSorter中實現(xiàn)對所有A類、B類....等等進行排序
* 如果直接在Datasorter中使用對A台谊、B蓉媳、C類排序這樣擴展行差,要支持對新類的排序時,要修改代碼
* Created by td on 2017/8/29.
* 更好的思路是::既然DataSorter是要根據(jù)不同的類的采取不同的方法實現(xiàn)排序锅铅,那么具體實現(xiàn)排序的方法交由子類去實現(xiàn)酪呻,且都實現(xiàn)同一個接口Comparable,那么DataSorter只需對Comparable排序盐须,而無需理會具體要排序的是
*/
public class DataSorter {
public static void sort(Comparable [] a) {
int index; //保存每次比較,最大值的下標;
for(int i = 1; i < a.length; i++){ //控制外循環(huán)次數(shù)
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;
}
//輸出數(shù)組元素
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){
}*/
/*
//冒泡排序法
//主要思路:按升序排序玩荠,數(shù)組元素兩兩比較,大的立即排后面
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);
}
}
}
//直接選擇排序法
//主要思路:按升序排序贼邓,每次循環(huán)找出最小數(shù)阶冈,把他放到第i位置上
public static void selectSort(int[] array) {
}
//交換數(shù)組元素
private static void swap(int[] a, int x, int y) {
int tmp = a[x];
a[x] = a[y];
a[y] = tmp;
}
//輸出數(shù)組元素
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并實現(xiàn)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并實現(xiàn)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.輸出結(jié)果
student5
student10
student100
----------------------
teacher--3
teacher--10
teacher--12