02-重构设计模式之strategy策略(坦克射击)

Java中的comparable和comparator

定义了一族算法(业务规则);
封装了每个算法;
这族的算法可互换代替(interchangeable)
comparable

public interface Comparable<T> {//在泛型中指定自己的类型
    int compareTo(T o);
}

comparator

public interface Comparator<T> {
    int compare(T o1, T o2);
}

cat

public class Cat implements Comparable<Cat> {
    int weight, height;

    public Cat(int weight, int height) {
        this.weight = weight;
        this.height = height;
    }

    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 +
                '}';
    }
}

CatComparator

public class CatComparator implements Comparator<Cat> {
    @Override
    public int compare(Cat o1, Cat o2) {
        return o2.weight - o1.weight;
    }
}

dog

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 +
                '}';
    }
}

DogComparator

public class DogComparator implements Comparator<Dog> {
    @Override
    public int compare(Dog o1, Dog o2) {
        if (o1.food > o2.food) return 1;
        else if (o1.food < o2.food) return -1;
        else return 0;
    }
}

Sorter 利用传入的数据类型自己的比较方法进行排序

public class Sorter<T> {
    public void sort(T[] arr,Comparator<T> comparator) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minPos = i;
            for (int j = i + 1; j < arr.length; j++) {
                minPos = comparator.compare(arr[j],arr[minPos]) == -1 ? j : minPos;
            }
            swap(arr, i, minPos);
        }
    }

    void swap(T[] arr, int i, int j) {
        T temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

Main

public class Main {
    public static void main(String[] args) {
        Dog[] dogs = {new Dog(1), new Dog(9), new Dog(2)};
        Cat[] cats = {new Cat(1,1),new Cat(3,3),new Cat(2,2)};
        Sorter<Dog> dogSorter = new Sorter<>();
        Sorter<Cat> catSorter = new Sorter();
        dogSorter.sort(dogs,new DogComparator());
        catSorter.sort(cats,new CatComparator());
        System.out.println(Arrays.toString(dogs));
        for (Cat cat : cats) {
            System.out.println(cat);
        }
    }
}

给坦克的射击重构为策略模式

接口FireStrategy

public interface FireStrategy {
    void fire(Tank t);
}

默认射击方法

+public class DefaultFireStrategy implements FireStrategy{
    @Override
    public void fire(Tank t) {
        int bX = t.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;

        new Bullet(bX, bY, t.dir, t.group, t.tf);
        if (t.group == Group.GOOD) new Thread(() -> new Audio("audios/tank_fire.wav").play()).start();
    }
}

四个方向射击方法

public class DefaultFireStrategy implements FireStrategy{
    @Override
    public void fire(Tank t) {
        int bX = t.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        int bY = t.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;

        new Bullet(bX, bY, t.dir, t.group, t.tf);
        if (t.group == Group.GOOD) new Thread(() -> new Audio("audios/tank_fire.wav").play()).start();
    }
}

给坦克加上属性FireStrategy

己方坦克使用四个方向的射击策略

敌方坦克使用一个方向的策略

        if (group == Group.GOOD) {
            String goodFSName = (String) PropertyManager.get("goodFS");
            try {
                fs = (FireStrategy) Class.forName(goodFSName).getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            String badFS = (String) PropertyManager.get("badFS");
            try {
                fs = (FireStrategy) Class.forName(badFS).getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

config

#fireStrategy
goodFS=com.xxx.tank.FourDirFireStrategy
badFS=com.xxx.tank.DefaultFireStrategy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值