自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java流水線Pipeline設(shè)計模式

開發(fā) 前端
Pipeline模式適用于流式遞歸傳遞輸入和處理后的輸出,對于比較簡單的場景,使用Java函數(shù)接口是挺不錯的選項。

概述

管道模式背后的主要思想是創(chuàng)建一組操作(管道)并通過它傳遞數(shù)據(jù)。跟責任鏈和裝飾器模式相比,Pipeline的主要優(yōu)勢在于它對結(jié)果的類型具有靈活性。

管道可以處理任何類型的輸入和輸出。

不可變管道

讓我們創(chuàng)建一個不可變的管道的例子。從管道接口開始:

public interface Pipe<IN, OUT> {
    OUT process(IN input);
}

這是一個非常簡單的接口,只有一個方法,它接受輸入并產(chǎn)生輸出。接口是參數(shù)化的,我們可以在其中提供任何實現(xiàn)。

現(xiàn)在,讓我們創(chuàng)建一個管道類:

public class Pipeline<IN, OUT> {

    private Collection<Pipe<?, ?>> pipes;

    private Pipeline(Pipe<IN, OUT> pipe) {
        pipes = Collections.singletonList(pipe);
    }

    private Pipeline(Collection<Pipe<?, ?>> pipes) {
        this.pipes = new ArrayList<>(pipes);
    }

    public static <IN, OUT> Pipeline<IN, OUT> of(Pipe<IN, OUT> pipe) {
        return new Pipeline<>(pipe);
    }

    public <NEW_OUT> Pipeline<IN, NEW_OUT> withNextPipe(Pipe<OUT, NEW_OUT> pipe) {
        final ArrayList<Pipe<?, ?>> newPipes = new ArrayList<>(pipes);
        newPipes.add(pipe);
        return new Pipeline<>(newPipes);
    }

    public OUT process(IN input) {
        Object output = input;
        for (final Pipe pipe : pipes) {
            output = pipe.process(output);
        }
        return (OUT) output;
    }
}

因為我們需要一個類型安全級別,并且不允許使管道失效,所以在添加新管道時,將產(chǎn)生一個新的獨立管道。

簡單管道

我們可以稍微簡化上面的例子,并完全去掉Pipeline類:

public interface Pipe<IN, OUT> {
    OUT process(IN input);

    default <NEW_OUT> Pipe<IN, NEW_OUT> add(Pipe <OUT, NEW_OUT> pipe) {
        return input -> pipe.process(process(input));
    }
}

與以前使用管道的實現(xiàn)相比,此解決方案非常簡單和靈活。

改進

我們可以用現(xiàn)有的Function接口替代它:

public interface Function<T, R> {
    //...
    R apply(T t);
    //...
}

此外,F(xiàn)unction接口包含兩個有用的方法,其中一個是andThen:

default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
    Objects.requireNonNull(after);
    return (T t) -> after.apply(apply(t));
}

我們可以使用它來代替以前的add方法。此外,F(xiàn)unction接口提供了一種在管道開始時添加函數(shù)的方法:

default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
    Objects.requireNonNull(before);
    return (V v) -> apply(before.apply(v));
}

通過使用Function,我們可以創(chuàng)建非常靈活和易于使用的管道:

@Test
void whenCombiningThreeFunctions_andInitializingPipeline_thenResultIsCorrect() {
    Function<Integer, Integer> square = s -> s * s;
    Function<Integer, Integer> half = s -> s / 2;
    Function<Integer, String> toString = Object::toString;
    Function<Integer, String> pipeline = square.andThen(half)
        .andThen(toString);
    String result = pipeline.apply(5);
    String expected = "12";
    assertEquals(expected, result);
}

我們可以使用BiFunctions擴展管道:

@Test
void whenCombiningFunctionAndBiFunctions_andInitializingPipeline_thenResultIsCorrect() {
    BiFunction<Integer, Integer, Integer> add = Integer::sum;
    BiFunction<Integer, Integer, Integer> mul = (a, b) -> a * b;
    Function<Integer, String> toString = Object::toString;
    BiFunction<Integer, Integer, String> pipeline = add.andThen(a -> mul.apply(a, 2))
        .andThen(toString);
    String result = pipeline.apply(1, 2);
    String expected = "6";
    assertEquals(expected, result);
}

因為andThen方法采用Function,所以我們必須將mul BiFunction轉(zhuǎn)換為一個Function。

結(jié)論

Pipeline模式適用于流式遞歸傳遞輸入和處理后的輸出,對于比較簡單的場景,使用Java函數(shù)接口是挺不錯的選項。

責任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2024-01-07 12:47:35

Golang流水線設(shè)計模式

2021-11-08 07:41:16

Go流水線編程

2017-03-02 14:12:13

流水線代碼Clojure

2022-07-18 06:05:28

Gitlab流水線

2017-02-28 16:00:45

DevOpsMarkdownreST

2024-05-24 08:11:50

Jenkins用戶權(quán)限管理

2013-06-06 09:31:52

2017-02-28 15:40:30

Docker流水線Azure

2021-12-17 18:21:54

大數(shù)據(jù)流水線設(shè)計

2021-06-26 14:22:34

Tekton流水線Kubernetes

2022-01-26 08:12:42

Jenkins開源流水線

2023-08-18 10:24:52

GitLabCI 流水線

2020-06-16 10:20:32

JavaStream流水線

2021-06-28 06:32:46

Tekton Kubernetes Clone

2021-12-24 08:02:48

GitLabCI模板庫流水線優(yōu)化

2021-06-18 05:48:02

Tekton DevopsKubernetes

2023-09-27 08:24:49

2021-01-05 08:39:51

容器前端流水線

2012-04-19 11:44:52

iPhone

2018-10-23 16:35:19

華為云
點贊
收藏

51CTO技術(shù)棧公眾號