java设计模式 -------- 行为模式 之 策略模式(1)

[本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020]

我们实现一个排序类,使其可以实现对数组中元素进行排序,以及打印数组的功能。如下所示,可以这样设计:

DataSorter.java

/**
 * 排序类
 * 
 * @author jesson
 * 
 */
public class DataSorter {
	/**
	 * 冒泡排序方法
	 * 
	 * @param a
	 */
	public static void bubbleSort(int[] a) {
		for (int i = a.length - 1; i >= 1; i--) {
			for (int j = 0; j < i; j++) {
				if (a[j] > a[j + 1]) {
					swap(a, j, j + 1);
				}
			}
		}
	}

	/**
	 * 交换两个数据
	 * 
	 * @param a
	 *            数组
	 * @param x
	 *            数组下标1
	 * @param y
	 *            数组下标2
	 */
	private static void swap(int[] a, int x, int y) {
		// TODO Auto-generated method stub
		int temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}

	/**
	 * 打印数组
	 * 
	 * @param a
	 */
	public static void print(int[] a) {
		for (int i = 0; i < a.length; i++)
			System.out.print(a[i] + " ");
		System.out.println();
	}

}</span>
Test.java
/**
 * 测试类
 * @author jesson
 *
 */
public class Test {
	public static void main(String[] args) {
		int[] a = new int[]{9,8,2,4,5,6,7};
		DataSorter.print(a);
		DataSorter.bubbleSort(a);
		DataSorter.print(a);
	}
}
</span>

  通过这个DataSorter可以实现将数组进行冒泡排序,以及输出数组的功能。

  

  但是现在,需求变了,我们不仅要实现对整数数组进行排序,还要对float型和double型数组进行排序,当然了,想必大家也想到了,这可以重载排序方法,以实现不同类型的数组进行排序,是,这是一种方法。但是,现在需求又变了,不仅要实现对这些常规类型的数组进行排序,还要对其他自定义的一些类进行排序,如,我们要对Cat进行排序,并输出,这是怎么做呢??想必大家很容易想到用重载,如下,可以这样写:

Cat.java

/**
 * Cat类 有属性身高,体重及toString()方法
 * 
 * @author jesson
 * 
 */
public class Cat {
	private int height; // 身高
	private int weight; // 体重

	public Cat(int height, int weight) {
		// TODO Auto-generated constructor stub
		this.height = height;
		this.weight = weight;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	/**
	 * 重写toString()方法
	 */
	@Override
	public String toString() {
		return this.getHeight() + "|" + this.getWeight();
	}

}
</span>
DataSorter.java

/**
 * 排序类
 * 
 * @author jesson
 * 
 */
public class DataSorter {

	/**
	 * 冒泡排序方法
	 * 
	 * @param Cat类型数组
	 */
	public static void bubbleSort(Cat[] a) {
		for (int i = a.length - 1; i >= 1; i--) {
			for (int j = 0; j < i; j++) {
				if (a[j].getHeight() > a[j + 1].getHeight()) {
					swap(a, j, j + 1);
				}
			}
		}
	}

	/**
	 * 冒泡排序方法
	 * 
	 * @param a
	 *            整数数组
	 */
	public static void bubbleSort(int[] a) {
		for (int i = a.length - 1; i >= 1; i--) {
			for (int j = 0; j < i; j++) {
				if (a[j] > a[j + 1]) {
					swap(a, j, j + 1);
				}
			}
		}
	}

	/**
	 * 交换两个数据
	 * 
	 * @param a
	 *            数组
	 * @param x
	 *            数组下标1
	 * @param y
	 *            数组下标2
	 */
	private static void swap(Cat[] a, int x, int y) {
		// TODO Auto-generated method stub
		Cat temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}

	/**
	 * 交换两个数据
	 * 
	 * @param a
	 *            数组
	 * @param x
	 *            数组下标1
	 * @param y
	 *            数组下标2
	 */
	private static void swap(int[] a, int x, int y) {
		// TODO Auto-generated method stub
		int temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}

	/**
	 * 打印数组
	 * 
	 * @param a
	 *            Cat类型数组
	 */
	public static void print(Cat[] a) {
		for (int i = 0; i < a.length; i++)
			System.out.print(a[i] + " ");
		System.out.println();
	}

	/**
	 * 打印数组
	 * 
	 * @param a
	 *            int类型数组
	 */
	public static void print(int[] a) {
		for (int i = 0; i < a.length; i++)
			System.out.print(a[i] + " ");
		System.out.println();
	}

}
Test.java 

/**
 * 测试类
 * @author jesson
 *
 */
public class Test {
	public static void main(String[] args) {
		//int[] a = new int[]{9,8,2,4,5,6,7};
		Cat[] a = {new Cat(5,5),new Cat(3,3),new Cat(1,1)};
		DataSorter.print(a);
		DataSorter.bubbleSort(a);
		DataSorter.print(a);
	}
}
   这样,不仅可以对int类型的数组进行冒泡排序,还可以对Cat类型的数组进行排序。

   现在问题又来了,需求变化,要实现对Dog类型的数组进行排序,该怎么办呢,当然了,还是可以用上面重载的方法,但是太麻烦了,如果以后又要对其他类型的数组进行排序,岂不是又要重载排序方法,所以,这种方法不可取。那么,如何用一个统一的排序方法,对所有类型的数组都能进行排序呢?答案是肯定的。

   为了实现对所有的类都能进行排序,所以需要定义比较的规则,所以要比较的类首先是可以进行比较的,也就是这些待比较的所有类都有一个共同的特征,即可比较,因此,我们定义一个Comparable接口,让需要排序的类都实现这个接口,这样,我们再将DataSorter类中的排序方法中对Cat类进行排序的代码进行修改,类型定义为Object,这样,所有的类都可进行排序了,具体如下:

Comparable.java

/**
 * 可比较接口
 * 定义了一个比较方法
 * @author jesson
 *
 */
public interface Comparable {
	public int compareTo(Object o);
}

Cat.java

/**
 * Cat类 有属性身高,体重及toString()方法
 * 
 * @author jesson
 * 
 */
public class Cat implements Comparable {
	private int height; // 身高
	private int weight; // 体重

	public Cat(int height, int weight) {
		// TODO Auto-generated constructor stub
		this.height = height;
		this.weight = weight;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	/**
	 * 重写toString()方法
	 */
	@Override
	public String toString() {
		return this.getHeight() + "|" + this.getWeight();
	}

	/**
	 * 实现Comparable接口的compareTO方法 返回1:当前的比参数中的对象大 返回-1:当前的比参数中的对象小 返回0:
	 * 当前的和参数中的对象相等
	 */
	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		Cat cat = (Cat) o;
		if (this.height > cat.getHeight())
			return 1;
		else if (this.height < cat.getHeight())
			return -1;
		else
			return 0;

	}
}
Dog.java

public class Dog implements Comparable {
	private int food;

	public Dog(int food) {
		super();
		// TODO Auto-generated constructor stub
		this.food = food;
	}

	public int getFood() {
		return food;
	}

	public void setFood(int food) {
		this.food = food;
	}

	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		Dog dog = (Dog) o;
		if (this.food > dog.getFood())
			return 1;
		else if (this.food < dog.getFood())
			return -1;
		else
			return 0;
	}
	
	@Override
	public String toString(){
		return this.food + "";
	}
}
DataSorter.java

/**
 * 排序类
 * 
 * @author jesson
 * 
 */
public class DataSorter {

	
	
	
	/**
	 * 冒泡排序方法
	 * 
	 * @param Cat类型数组
	 */
	public static void bubbleSort(Object[] a) {
		for (int i = a.length - 1; i >= 1; i--) {
			for (int j = 0; j < i; j++) {
				Comparable o1 = (Comparable)a[j];
				Comparable o2 = (Comparable)a[j+1];
				if (o1.compareTo(o2) == 1) {
					swap(a, j, j + 1);
				}
			}
		}
	}

	/**
	 * 冒泡排序方法
	 * 
	 * @param a
	 *            整数数组
	 */
	public static void bubbleSort(int[] a) {
		for (int i = a.length - 1; i >= 1; i--) {
			for (int j = 0; j < i; j++) {
				if (a[j] > a[j + 1]) {
					swap(a, j, j + 1);
				}
			}
		}
	}

	/**
	 * 交换两个数据
	 * 
	 * @param a
	 *            Object类型数组
	 * @param x
	 *            数组下标1
	 * @param y
	 *            数组下标2
	 */
	private static void swap(Object[] a, int x, int y) {
		// TODO Auto-generated method stub
		Object temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}

	/**
	 * 交换两个数据
	 * 
	 * @param a
	 *            数组
	 * @param x
	 *            数组下标1
	 * @param y
	 *            数组下标2
	 */
	private static void swap(int[] a, int x, int y) {
		// TODO Auto-generated method stub
		int temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}

	/**
	 * 打印数组
	 * 
	 * @param a
	 *            Object类型数组
	 */
	public static void print(Object[] a) {
		for (int i = 0; i < a.length; i++)
			System.out.print(a[i] + " ");
		System.out.println();
	}

	/**
	 * 打印数组
	 * 
	 * @param a
	 *            int类型数组
	 */
	public static void print(int[] a) {
		for (int i = 0; i < a.length; i++)
			System.out.print(a[i] + " ");
		System.out.println();
	}

}
Test.java

/**
 * 测试类
 * @author jesson
 *
 */
public class Test {
	public static void main(String[] args) {
		//int[] a = new int[]{9,8,2,4,5,6,7};
		//Cat[] a = {new Cat(5,5),new Cat(3,3),new Cat(1,1)};
		Dog[] a = {new Dog(3),new Dog(2),new Dog(6)};
		DataSorter.print(a);
		DataSorter.bubbleSort(a);
		DataSorter.print(a);
	}
}
   这样修改之后,DataSorter不仅可以对Cat类型的数组进行排序,也可以对Dog类型的数组进行排序,而且,这时,如果需要对新定义的类也能进行排序的话,只要让其实现Comparable接口,并具体实现compareTo()方法,就可以不用再改动DataSorter类,就可以直接用DataSorter对新定义的类型的数组进行排序,达到了代码重用的目的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值