03 Strategy策略

抽丝剥茧设计模式 之 Strategy策略 - 更多内容请见 目录

文章目录

  • 一、Strategy策略
  • 二、Comparable和Comparator源码分析
    • 使用案例
    • Arrays.sort源码
    • Collections.sort源码
    • Comparable源码
    • Comparator源码


一、Strategy策略

策略模式是一种设计模式,它定义了一系列的算法,并将每个算法封装起来,使它们可以互相替换。Java中的Comparable和Comparator两个接口确实是策略模式的典型应用。

在Go语言的基本类库中,也有一些接口和实现是基于策略模式的。例如,io.Reader和io.Writer就是这样的接口。这两个接口定义了读取和写入数据的通用方法,但具体的实现可以根据不同的需求进行替换。你可以使用内存中的字节切片作为数据源,也可以使用文件、网络连接或其他任何数据源作为数据源,只要实现io.Reader和io.Writer接口即可。

另一个例子是http.Handler接口,它定义了处理HTTP请求的方法。你可以编写自己的实现该接口的函数,然后将其注册到HTTP服务器上,以处理特定的URL路径或路由。

这些例子中,接口定义了一组通用的方法,而具体的实现可以根据不同的需求进行替换。这种模式使得代码更加灵活,易于扩展和维护,符合策略模式的思想。

二、Comparable和Comparator源码分析

以下代码分析,基于openjdk-jdk8-b120,我们可以注意到,我们自定义了一个Comparator的子类,用于封装一组可以对对象进行比较的算法。传给sort()方法作为参数,用于决定排序的实际策略。

使用案例

首先看两个使用案例,Arrays.sort和Collections.sort都可以接收一个自定义的Comparator,用于对数组排序。

// Arrays.sort
import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        Integer[] numbers = {3, 1, 2, 4};

        // 使用自定义的Comparator对数组进行排序
        Comparator<Integer> comparator = new MyComparator();
        Arrays.sort(numbers, comparator);

        System.out.println(Arrays.toString(numbers)); // 输出: [1, 2, 3, 4]
    }

    static class MyComparator implements Comparator<Integer> {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
        }
    }
}
// Collections.sort
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        // 使用自定义的比较器对列表进行排序
        Comparator<String> comparator = new LengthComparator();
        Collections.sort(names, comparator);

        System.out.println(names); // 输出: [Charlie, Bob, Alice]
    }

    static class LengthComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            return Integer.compare(o1.length(), o2.length());
        }
    }
}

Arrays.sort源码

package java.util;

public class Arrays {

    private Arrays() {}
    public static <T> void sort(T[] a, Comparator<? super T> c) {
        if (c == null)
            c = NaturalOrder.INSTANCE;
        if (LegacyMergeSort.userRequested)
            legacyMergeSort(a, c);
        else
            TimSort.sort(a, 0, a.length, c, null, 0, 0);
    }
}

Collections.sort源码

package java.util;
public class Collections {
    // Suppresses default constructor, ensuring non-instantiability.
    private Collections() {
    }
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static <T> void sort(List<T> list, Comparator<? super T> c) {
        Object[] a = list.toArray();
        Arrays.sort(a, (Comparator)c);
        ListIterator<T> i = list.listIterator();
        for (int j=0; j<a.length; j++) {
            i.next();
            i.set((T)a[j]);
        }
    }
}

Comparable源码

package java.lang;
import java.util.*;
public interface Comparable<T> {
    public int compareTo(T o);
}

Comparator源码

package java.util;

import java.io.Serializable;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.ToDoubleFunction;
import java.util.Comparators;

@FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2);
    boolean equals(Object obj);
    default Comparator<T> reversed() {
        return Collections.reverseOrder(this);
    }
    default Comparator<T> thenComparing(Comparator<? super T> other) {
        Objects.requireNonNull(other);
        return (Comparator<T> & Serializable) (c1, c2) -> {
            int res = compare(c1, c2);
            return (res != 0) ? res : other.compare(c1, c2);
        };
    }
    default <U extends Comparable<? super U>> Comparator<T> thenComparing(
            Function<? super T, ? extends U> keyExtractor,
            Comparator<? super U> keyComparator)
    {
        return thenComparing(comparing(keyExtractor, keyComparator));
    }
    default <U extends Comparable<? super U>> Comparator<T> thenComparing(
            Function<? super T, ? extends U> keyExtractor)
    {
        return thenComparing(comparing(keyExtractor));
    }

    default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) {
        return thenComparing(comparingInt(keyExtractor));
    }

    default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) {
        return thenComparing(comparingLong(keyExtractor));
    }

    default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) {
        return thenComparing(comparingDouble(keyExtractor));
    }

    public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {
        return Collections.reverseOrder();
    }

    @SuppressWarnings("unchecked")
    public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
        return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE;
    }

    public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) {
        return new Comparators.NullComparator<>(true, comparator);
    }

    public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) {
        return new Comparators.NullComparator<>(false, comparator);
    }

    public static <T, U> Comparator<T> comparing(
            Function<? super T, ? extends U> keyExtractor,
            Comparator<? super U> keyComparator)
    {
        Objects.requireNonNull(keyExtractor);
        Objects.requireNonNull(keyComparator);
        return (Comparator<T> & Serializable)
            (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),
                                              keyExtractor.apply(c2));
    }

    public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
            Function<? super T, ? extends U> keyExtractor)
    {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
    }

    public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
    }

    public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2));
    }

    public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2));
    }
}



你可能感兴趣的:(策略模式,开发语言)