设计模式——策略模式(Strategy) 笔记

来源:互联网 发布:cocos2dx安装 mac 编辑:程序博客网 时间:2024/06/05 20:12


引入:::

排序方法的种类:  {冒择路(入)兮(希尔)快归堆}


package com.linux_lihuafeng.strategy;

public class Guest {

 public static void main(String[] args) {
  
  //站在使用者角度編程  ,, 會使考慮接口更加簡單
  int [] a = {9 , 5 , 3, 7 , 1};
  DataSorter dataSorter = new DataSorter();
  dataSorter.sort(a);
  dataSorter.print(a);
 }
}



package com.linux_lihuafeng.strategy;

public class DataSorter {

 
 public void sort(int a[]) {
  for(int i = 0 ; i < a.length -1 ; i++ ) {
   for(int j = 0 ; j < a.length - i -1 ;j ++){
    exchange( a, j , j+1);
   }
   
  }
 }
 
 public void exchange(int[] a , int i, int j) {
  if(a[i] > a[j] ) {
   int temp ;
   temp = a[i];
   a[i] = a[j];
   a[j] = temp;
  }
 }
 public void print(int a[]) {
  
  for(int i = 0 ; i < a.length ; i++) {
   System.out.print("     a["+ i+ "] = "+ a[i] );
  }
 }

}

现在想要对猫的身高进行排序,,  修改代码如下:::


package com.linux_lihuafeng.strategy;


public class DataSorter {



public void sort(Cat a[]) {
for(int i = 0 ; i < a.length-1 ; i++ ) {
for(int j = 0 ; j < a.length-i-1 ;j++){
if(a[j].getHeight() > a[j+1].getHeight() ) 
exchange( a, j , j+1);
}

}
}

public void exchange(Cat[] a , int i, int j) {

Cat temp ;
temp = a[i];
a[i] = a[j];
a[j] = temp;

}
public void print(Cat a[]) {

for(int i = 0 ; i < a.length ; i++) {
System.out.print("     a["+ i+ "] = "+ a[i].getHeight() );
}



}


package com.linux_lihuafeng.strategy;


public class Cat {


public Cat(int height, int weight) {
super();
this.height = height;
this.weight = weight;
}
private int height;
private int 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;
}

}



package com.linux_lihuafeng.strategy;


public class Guest {


public static void main(String[] args) {

//站在使用者角度編程  ,, 會使考慮接口更加簡單
// int [] a = {9 , 5 , 3, 7 , 1};
DataSorter dataSorter = new DataSorter();




Cat[] a = {new Cat(5,1), new Cat(3 ,1), new Cat(1 , 1)};
dataSorter.sort(a);
dataSorter.print(a);
}
}

 
现在即想对猫排序,,又想对狗排序,,怎么办?? 这里的关键点是比较的的问题,,对于不同的类型会有不同的比较方式,,故想到用接口对比较进行封装。。。
package com.linux_lihuafeng.strategy;


public interface Camparable {


public int campareTo(Object o) ;
}


package com.linux_lihuafeng.strategy;


public class Cat implements Camparable{


public Cat(int height, int weight) {
super();
this.height = height;
this.weight = weight;
}
private int height;
private int 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;
}




public String toString () {
return "height = "+this.height;
}
public int campareTo(Object o) {
if (o instanceof Cat) {
Cat c = (Cat)o;
if(this.height > c.getHeight()){
return 1;
}else if(this.height == c.getHeight()){
return 0;
}else{
return -1;
}
}
try{
//这个地方写的不太对
return -1000;
}catch(Exception e){
System.out.println("the object is not a Cat");
}
return -100;

}


}



package com.linux_lihuafeng.strategy;


public class DataSorter {



public void sort(Object a[]) {
for(int i = 0 ; i < a.length -1 ; i++ ) {
for(int j = 0 ; j < a.length - i -1 ;j ++){
//接口的显示实现,需要转换成接口才能调用这些方法
//有了了接口之后,,在这里就不必考虑a[] 的类型问题了
Camparable o1 = (Camparable)a[j];
Camparable o2 = (Camparable)a[j+1];
if(o1.campareTo(o2) > 0 ) 
exchange( a, j , j+1);

// if(a[i].getHeight() > a[j].getHeight() ) 
// exchange( a, j , j+1);
}

}
}

public void exchange(Object[] a , int i, int j) {

Object temp ;
temp = a[i];
a[i] = a[j];
a[j] = temp;

}
public void print(Object a[]) {

for(int i = 0 ; i < a.length ; i++) {
System.out.print("     a["+ i+ "] : "+ a[i].toString() );
}


}





package com.linux_lihuafeng.strategy;


public class Guest {


public static void main(String[] args) {

//站在使用者角度編程  ,, 會使考慮接口更加簡單
// int [] a = {9 , 5 , 3, 7 , 1};
DataSorter dataSorter = new DataSorter();




Cat[] a = {new Cat(5,1), new Cat(3 ,1), new Cat(1 , 1)};
dataSorter.sort(a);
dataSorter.print(a);
}
}


这样做的话达到了可扩展的目的::
例如,现在想要对狗的食量进行排序,,只需添加一个狗的类就可以了


package com.linux_lihuafeng.strategy;


public class Dog implements Camparable{


//定义狗的食量
private int capacity;


public int getCapacity() {
return capacity;
}


public void setCapacity(int capacity) {
this.capacity = capacity;
}


public Dog(int capacity) {
super();
this.capacity = capacity;
}

public String toString () {

return "capacity = " + capacity;
}
@Override
public int campareTo(Object o) {


if(o instanceof Dog){
Dog d = (Dog)o;
if(this.capacity > d.getCapacity()){
return 1;
}else if (this.capacity == d.getCapacity()){
return 0;
}else {
return -1;
}
}
return -100;
}

}


package com.linux_lihuafeng.strategy;


public class Guest {


public static void main(String[] args) {

//站在使用者角度編程  ,, 會使考慮接口更加簡單
// int [] a = {9 , 5 , 3, 7 , 1};
DataSorter dataSorter = new DataSorter();




Cat[] a = {new Cat(5,1), new Cat(3 ,1), new Cat(1 , 1)};
Dog[] d = {new Dog(7), new Dog(8), new Dog(1), new Dog(9)}; 
dataSorter.sort(d);
dataSorter.print(d);
}
}

下面是策略模式::===========================
对于上面例如猫的compareTo方法的实现,,我们是将比较方法写死在程序中的,,无法实现其他的比较,,比如比较猫的高度,,猫的胡子长度以及猫的体重等等。。。

也就是说要对对比方式进行扩展::
加一个比较器Comparator接口,,专门用于比较
加一个类CatHeightComparator,,用于实现Comparator接口,,实现比较猫高度的功能

还可以扩展,,加一个CatWeightComparator,,用于实现Comparator接口,,实现比较猫重量的功能



package com.linux_lihuafeng.strategy;

public interface Comparator {

//interface 中的方法均默认为public
int compare(Object o1, Object o2);
}



package com.linux_lihuafeng.strategy;

public class CatHeightComparator implements Comparator {


@Override
public int compare(Object o1, Object o2) {
Cat c1 = (Cat)o1;
Cat c2 = (Cat)o2;
if(c1.getHeight() > c2.getHeight()){
return 1;
}else if(c1.getHeight() < c2.getHeight()){
return -1;
}else {
return 0;
}
}

}

package com.linux_lihuafeng.strategy;

public class CatWeightComparator implements Comparator {


@Override
public int compare(Object o1, Object o2) {


Cat c1 = (Cat)o1;
Cat c2 = (Cat)o2;
if(c1.getWeight() > c2.getWeight()){
return 1;
}else if(c1.getWeight() < c2.getWeight()){
return -1;
}else {
return 0;
}
}
}

package com.linux_lihuafeng.strategy;


public class Cat implements Camparable{


private int height;
private int weight;
private Comparator comparator = new CatHeightComparator();

public Comparator getComparator() {
return comparator;
}


public void setComparator(Comparator comparator) {
this.comparator = comparator;
}


public Cat(int height, int weight) {
super();
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;
}


public String toString () {
return "height = "+this.height;
}
public int campareTo(Object o) {

// if (o instanceof Cat) {
// Cat c = (Cat)o;
// if(this.height > c.getHeight()){
// return 1;
// }else if(this.height == c.getHeight()){
// return 0;
// }else{
// return -1;
// }
// }
// try{
// //这个地方写的不太对
// return -1000;
// }catch(Exception e){
// System.out.println("the object is not a Cat");
// }
// return -100;
return comparator.compare(this, o);

}


}

解释::当要比较大小时,定义一个策略的比较器,,然后有具体的比较策略来决定到底谁大谁小。。。



逻辑思路::
先写了一个sort()方法,,目的是让sort方法得到复用,,最好是各种各样任意类型的对象都可以排序,,
解决办法就是让各种各样的类来实现Comparable接口,,实现Comparable接口,,就必须有一个compareTo方法,,
根据ComparaTo方法,,我们就可以对这个类的两个对象进行比较,,,但比较大小仅仅实现compareTo方法,,
将比较代码写死在compareTo方法中,是不明智的,,所以还需要将比较大小{如  高度、 重量等}的策略进行扩展,,
解决方案是写了一个Comparator接口,,用于封装两个对象的比较,,用具体的策略类{如  CatHeightComparator  、 CatWeightComparator}
具体实现比较的方法。。。所以在比较策略上也实现了扩展。
0 0