Strategy(策略模式)

Strategy(策略模式)

Comparable(不够灵活,不完美的)

package strategy;
//这里是自己写的   尽量用系统的Comparable
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));
    }
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值