坦克大战(一期)-设计模式版-策略模式

DesignPatterns
bingor_yhj 2 years ago
parent f38dcc7a03
commit ee1a314d2a

@ -12,7 +12,9 @@ import java.util.Properties;
*/
public class PropertyMgr {
private static Properties props = new Properties();
private static final Properties props = new Properties();
private PropertyMgr() {}
static {
try {

@ -0,0 +1,30 @@
package com.msb.strategy;
/**
* @Author bingor
* @Date 2022-10-06 17:12
* @Description: com.msb.strategy
* @Version: 1.0
*/
public class Cat implements Comparable<Cat> {
private int weight, height;
public Cat(int weight) {
this.weight = weight;
}
@Override
public int compareTo(Cat o) {
if(this.weight < o.weight) return -1;
else if(this.weight > o.weight) return 1;
else return 0;
}
@Override
public String toString() {
return "Cat{" +
"weight=" + weight +
'}';
}
}

@ -0,0 +1,34 @@
package com.msb.strategy;
/**
* @Author bingor
* @Date 2022-10-06 17:12
* @Description: com.msb.strategy
* @Version: 1.0
*/
public class Cat2 {
private int weight, height;
public Cat2(int weight, int height) {
this.weight = weight;
this.height = height;
}
@Override
public String toString() {
return "Cat2{" +
"weight=" + weight +
", height=" + height +
'}';
}
public int getWeight() {
return weight;
}
public int getHeight() {
return height;
}
}

@ -0,0 +1,16 @@
package com.msb.strategy;
/**
* @Author bingor
* @Date 2022-10-06 17:56
* @Description: com.msb.strategy
* @Version: 1.0
*/
public class CatHeightComparator implements Comparator<Cat2> {
@Override
public int compare(Cat2 o1, Cat2 o2) {
if(o1.getHeight() < o2.getHeight()) return -1;
else if(o1.getHeight() > o2.getHeight()) return 1;
else return 0;
}
}

@ -0,0 +1,16 @@
package com.msb.strategy;
/**
* @Author bingor
* @Date 2022-10-06 17:56
* @Description: com.msb.strategy
* @Version: 1.0
*/
public class CatWeightComparator implements Comparator<Cat2> {
@Override
public int compare(Cat2 o1, Cat2 o2) {
if(o1.getWeight() < o2.getWeight()) return -1;
else if(o1.getWeight() > o2.getWeight()) return 1;
else return 0;
}
}

@ -0,0 +1,11 @@
package com.msb.strategy;
/**
* @Author bingor
* @Date 2022-10-06 17:05
* @Description: com.msb.strategy
* @Version: 1.0
*/
public interface Comparable<T> {
int compareTo(T o);
}

@ -0,0 +1,11 @@
package com.msb.strategy;
/**
* @Author bingor
* @Date 2022-10-06 17:50
* @Description: com.msb.strategy
* @Version: 1.0
*/
public interface Comparator<T> {
public int compare(T o1, T o2);
}

@ -0,0 +1,30 @@
package com.msb.strategy;
/**
* @Author bingor
* @Date 2022-10-06 17:12
* @Description: com.msb.strategy
* @Version: 1.0
*/
public class Dog implements Comparable<Dog> {
private int food;
public Dog(int food) {
this.food = food;
}
@Override
public int compareTo(Dog o) {
if(this.food < o.food) return -1;
else if(this.food > o.food) return 1;
else return 0;
}
@Override
public String toString() {
return "Dog{" +
"food=" + food +
'}';
}
}

@ -0,0 +1,37 @@
package com.msb.strategy;
import java.util.Arrays;
/**
* @Author bingor
* @Date 2022-10-06 17:06
* @Description: com.msb.strategy
* @Version: 1.0
*/
public class Sorter {
public void sort(Comparable[] arr) {
for(int i=0; i<arr.length-1; i++) {
int minPos = i;
for(int j=1; j<arr.length; j++) {
minPos = arr[j].compareTo(arr[minPos])==-1 ? j : minPos;
}
swap(arr, i, minPos);
}
}
public void swap(Comparable[] arr, int i, int j) {
Comparable temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
// Cat[] cats = new Cat[]{new Cat(5), new Cat(8), new Cat(3)};
Dog[] dogs = new Dog[]{new Dog(6), new Dog(4), new Dog(9)};
Sorter sorter = new Sorter();
sorter.sort(dogs);
System.out.println(Arrays.toString(dogs));
}
}

@ -0,0 +1,38 @@
package com.msb.strategy;
import java.util.Arrays;
/**
* @Author bingor
* @Date 2022-10-06 17:06
* @Description: com.msb.strategy
* @Version: 1.0
*/
public class Sorter2<T> {
public void sort(T[] arr, Comparator<T> comparator) {
for(int i=0; i<arr.length-1; i++) {
int minPos = i;
for(int j=1; j<arr.length; j++) {
minPos = comparator.compare(arr[j], arr[minPos])==-1 ? j : minPos;
}
swap(arr, i, minPos);
}
}
public void swap(T[] arr, int i, int j) {
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
Cat2[] cats = new Cat2[]{new Cat2(3, 5), new Cat2(5, 2), new Cat2(1, 4)};
// CatWeightComparator comparator = new CatWeightComparator();
CatHeightComparator comparator = new CatHeightComparator();
Sorter2<Cat2> sorter = new Sorter2<Cat2>();
sorter.sort(cats, comparator);
System.out.println(Arrays.toString(cats));
}
}
Loading…
Cancel
Save