Strategy(策略模式)
Comparable(不够灵活,不完美的)
package strategy;
public interface Comparable < T> {
int compareTo ( T t) ;
}
package strategy;
public class Sorter {
public void sort ( Comparable[ ] arr) {
for ( int i = 0 ; i < arr. length; i++ ) {
int minPos = i;
for ( int j = i+ 1 ; j < arr. length; j++ ) {
minPos = arr[ j] . compareTo ( arr[ minPos] ) == - 1 ? j: minPos;
awap ( arr, i, minPos) ;
}
}
}
private void awap ( Comparable[ ] arr, int i, int j) {
Comparable temp = arr[ i] ;
arr[ i] = arr[ j] ;
arr[ j] = temp;
}
}
package strategy;
public class Cat implements Comparable < Cat> {
int weight, height;
public Cat ( int weight, int height) {
this . weight = weight;
this . height = height;
}
@Override
public int compareTo ( Cat cat) {
if ( this . weight < cat. weight) return - 1 ;
else if ( this . weight > cat. weight) return 1 ;
else return 0 ;
}
@Override
public String toString ( ) {
return "Cat{" +
"weight=" + weight +
", height=" + height +
'}' ;
}
}
package strategy;
public class Dog implements Comparable < Dog> {
int food;
public Dog ( int food) {
this . food = food;
}
@Override
public int compareTo ( Dog dog) {
if ( this . food < dog. food) return - 1 ;
else if ( this . food > dog. food) return 1 ;
else return 0 ;
}
@Override
public String toString ( ) {
return "Dog{" +
"food=" + food +
'}' ;
}
}
package strategy;
import java. util. Arrays;
public class Main {
public static void main ( String[ ] args) {
Dog[ ] dog = { new Dog ( 3 ) , new Dog ( 2 ) , new Dog ( 1 ) } ;
Sorter sorter = new Sorter ( ) ;
sorter. sort ( dog) ;
System. out. println ( Arrays. toString ( dog) ) ;
}
}
Comparator(真正的策略模式)
package strategy;
public interface Comparator < T> {
int compare ( T o1, T o2) ;
}
package strategy;
public class Sorter < T> {
public void sort ( T[ ] arr, Comparator< T> comparator) {
for ( int i = 0 ; i < arr. length; i++ ) {
int minPos = i;
for ( int j = i+ 1 ; j < arr. length; j++ ) {
minPos = comparator. compare ( arr[ j] , arr[ minPos] ) == - 1 ? j: minPos;
awap ( arr, i, minPos) ;
}
}
}
private void awap ( T[ ] arr, int i, int j) {
T temp = arr[ i] ;
arr[ i] = arr[ j] ;
arr[ j] = temp;
}
}
package strategy;
public class Cat {
int weight, height;
public Cat ( int weight, int height) {
this . weight = weight;
this . height = height;
}
@Override
public String toString ( ) {
return "Cat{" +
"weight=" + weight +
", height=" + height +
'}' ;
}
}
package strategy;
public class Dog {
int food;
public Dog ( int food) {
this . food = food;
}
@Override
public String toString ( ) {
return "Dog{" +
"food=" + food +
'}' ;
}
}
package strategy;
import java. util. Arrays;
public class Main {
public static void main ( String[ ] args) {
Dog[ ] dog = { new Dog ( 3 ) , new Dog ( 2 ) , new Dog ( 1 ) } ;
Sorter< Dog> sorterDog = new Sorter < > ( ) ;
sorterDog. sort ( dog, ( o1, o2) - > {
if ( o1. food < o2. food) return - 1 ;
else if ( o1. food > o2. food) return 1 ;
else return 0 ;
} ) ;
System. out. println ( Arrays. toString ( dog) ) ;
Cat[ ] cat = { new Cat ( 3 , 3 ) , new Cat ( 2 , 2 ) , new Cat ( 1 , 1 ) } ;
Sorter< Cat> sorterCat = new Sorter < > ( ) ;
sorterCat. sort ( cat, ( o1, o2) - > {
if ( o1. weight < o2. weight) return - 1 ;
else if ( o1. weight > o2. weight) return 1 ;
else return 0 ;
} ) ;
System. out. println ( Arrays. toString ( cat) ) ;
sorterCat. sort ( cat, ( o1, o2) - > {
if ( o1. height > o2. height) return - 1 ;
else if ( o1. height < o2. height) return 1 ;
else return 0 ;
} ) ;
System. out. println ( Arrays. toString ( cat) ) ;
}
}