Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2c8d174

Browse files
committed
iluwatar#564: Collection Pipeline pattern
1 parent987994f commit2c8d174

File tree

11 files changed

+490
-0
lines changed

11 files changed

+490
-0
lines changed

‎collection-pipeline/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
layout:pattern
3+
title:Collection Pipeline
4+
folder:collection-pipeline
5+
permalink:/patterns/collection-pipeline/
6+
categories:Functional
7+
tags:
8+
-Java
9+
-Difficulty-Beginner
10+
-Functional
11+
---
12+
13+
##Intent
14+
Collection Pipeline introduces Function Composition and Collection Pipeline, two functional-style patterns that you can combine to iterate collections in your code.
15+
In functional programming, it's common to sequence complex operations through a series of smaller modular functions or operations. The series is called a composition of functions, or a function composition. When a collection of data flows through a function composition, it becomes a collection pipeline. Function Composition and Collection Pipeline are two design patterns frequently used in functional-style programming.
16+
17+
![alt text](./etc/collection-pipeline.png"Collection Pipeline")
18+
19+
##Applicability
20+
Use the Collection Pipeline pattern when
21+
22+
* When you want to perform a sequence of operations where one operation's collected output is fed into the next
23+
* When you use a lot of statements in your code
24+
* When you use a lot of loops in your code
25+
26+
##Credits
27+
28+
*[Function composition and the Collection Pipeline pattern](https://www.ibm.com/developerworks/library/j-java8idioms2/index.html)
29+
*[Martin Fowler](https://martinfowler.com/articles/collection-pipeline/)
14.2 KB
Loading
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagramversion="1.2.2"icons="true"always-add-relationships="false"generalizations="true"realizations="true"
3+
associations="true"dependencies="false"nesting-relationships="true"router="FAN">
4+
<classid="1"language="java"name="com.iluwatar.collectionpipeline.App"project="collection-pipeline"
5+
file="/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/App.java"binary="false"
6+
corner="BOTTOM_RIGHT">
7+
<positionheight="-1"width="-1"x="952"y="278"/>
8+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
9+
sort-features="false"accessors="true"visibility="true">
10+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
11+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
12+
</display>
13+
</class>
14+
<classid="2"language="java"name="com.iluwatar.collectionpipeline.ImperativeProgramming"
15+
project="collection-pipeline"
16+
file="/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/ImperativeProgramming.java"binary="false"
17+
corner="BOTTOM_RIGHT">
18+
<positionheight="-1"width="-1"x="552"y="107"/>
19+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
20+
sort-features="false"accessors="true"visibility="true">
21+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
22+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
23+
</display>
24+
</class>
25+
<classid="3"language="java"name="com.iluwatar.collectionpipeline.Car"project="collection-pipeline"
26+
file="/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Car.java"binary="false"
27+
corner="BOTTOM_RIGHT">
28+
<positionheight="-1"width="-1"x="476"y="301"/>
29+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
30+
sort-features="false"accessors="true"visibility="true">
31+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
32+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
33+
</display>
34+
</class>
35+
<classid="4"language="java"name="com.iluwatar.collectionpipeline.FunctionalProgramming"
36+
project="collection-pipeline"
37+
file="/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/FunctionalProgramming.java"binary="false"
38+
corner="BOTTOM_RIGHT">
39+
<positionheight="-1"width="-1"x="914"y="105"/>
40+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
41+
sort-features="false"accessors="true"visibility="true">
42+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
43+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
44+
</display>
45+
</class>
46+
<classid="5"language="java"name="com.iluwatar.collectionpipeline.Iterating"project="collection-pipeline"
47+
file="/collection-pipeline/src/main/java/com/iluwatar/collectionpipeline/Iterating.java"binary="false"
48+
corner="BOTTOM_RIGHT">
49+
<positionheight="-1"width="-1"x="723"y="280"/>
50+
<displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
51+
sort-features="false"accessors="true"visibility="true">
52+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
53+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
54+
</display>
55+
</class>
56+
<classifier-displayautosize="true"stereotype="true"package="true"initial-value="false"signature="true"
57+
sort-features="false"accessors="true"visibility="true">
58+
<attributespublic="true"package="true"protected="true"private="true"static="true"/>
59+
<operationspublic="true"package="true"protected="true"private="true"static="true"/>
60+
</classifier-display>
61+
<association-displaylabels="true"multiplicity="true"/>
62+
</class-diagram>

‎collection-pipeline/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!--
2+
3+
The MIT License
4+
Copyright (c) 2014 Ilkka Seppälä
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
24+
-->
25+
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
26+
<modelVersion>4.0.0</modelVersion>
27+
<parent>
28+
<groupId>com.iluwatar</groupId>
29+
<artifactId>java-design-patterns</artifactId>
30+
<version>1.20.0-SNAPSHOT</version>
31+
</parent>
32+
<artifactId>collection-pipeline</artifactId>
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-api</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.junit.jupiter</groupId>
41+
<artifactId>junit-jupiter-engine</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
packagecom.iluwatar.collectionpipeline;
24+
25+
importjava.util.List;
26+
27+
importorg.slf4j.Logger;
28+
importorg.slf4j.LoggerFactory;
29+
30+
/**
31+
* In imperative-style programming, it is common to use for and while loops for
32+
* most kinds of data processing. Function composition is a simple technique
33+
* that lets you sequence modular functions to create more complex operations.
34+
* When you run data through the sequence, you have a collection pipeline.
35+
* Together, the Function Composition and Collection Pipeline patterns enable
36+
* you to create sophisticated programs where data flow from upstream to
37+
* downstream and is passed through a series of transformations.
38+
*
39+
*/
40+
publicclassApp {
41+
42+
privatestaticfinalLoggerLOGGER =LoggerFactory.getLogger(App.class);
43+
44+
/**
45+
* Program entry point.
46+
*
47+
* @param args
48+
* command line args
49+
*/
50+
publicstaticvoidmain(String[]args) {
51+
52+
List<Car>cars =Iterating.createCars();
53+
54+
List<String>modelsImperative =ImperativeProgramming.getModelsAfter2000UsingFor(cars);
55+
LOGGER.info(modelsImperative.toString());
56+
57+
List<String>modelsFunctional =FunctionalProgramming.getModelsAfter2000UsingPipeline(cars);
58+
LOGGER.info(modelsFunctional.toString());
59+
}
60+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
packagecom.iluwatar.collectionpipeline;
24+
25+
/**
26+
* A Car class that has the properties of make, model, and year.
27+
*/
28+
publicclassCar {
29+
privateStringmake;
30+
privateStringmodel;
31+
privateintyear;
32+
33+
/**
34+
* Constructor to create an instance of car.
35+
* @param theMake the make of the car
36+
* @param theModel the model of the car
37+
* @param yearOfMake the year of built of the car
38+
*/
39+
publicCar(StringtheMake,StringtheModel,intyearOfMake) {
40+
make =theMake;
41+
model =theModel;
42+
year =yearOfMake;
43+
}
44+
45+
publicStringgetMake() {
46+
returnmake;
47+
}
48+
49+
publicStringgetModel() {
50+
returnmodel;
51+
}
52+
53+
publicintgetYear() {
54+
returnyear;
55+
}
56+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
packagecom.iluwatar.collectionpipeline;
24+
25+
importjava.util.Comparator;
26+
importjava.util.List;
27+
importjava.util.stream.Collectors;
28+
29+
/**
30+
* Iterating and sorting with a collection pipeline
31+
*
32+
* <p>In functional programming, it's common to sequence complex operations through
33+
* a series of smaller modular functions or operations. The series is called a
34+
* composition of functions, or a function composition. When a collection of
35+
* data flows through a function composition, it becomes a collection pipeline.
36+
* Function Composition and Collection Pipeline are two design patterns
37+
* frequently used in functional-style programming.
38+
*
39+
* <p>Instead of passing a lambda expression to the map method, we passed the
40+
* method reference Car::getModel. Likewise, instead of passing the lambda
41+
* expression car -> car.getYear() to the comparing method, we passed the method
42+
* reference Car::getYear. Method references are short, concise, and expressive.
43+
* It is best to use them wherever possible.
44+
*
45+
*/
46+
publicclassFunctionalProgramming {
47+
privateFunctionalProgramming() {
48+
}
49+
50+
/**
51+
* Method to get models using for collection pipeline.
52+
*
53+
* @param cars {@link List} of {@link Car} to be used for filtering
54+
* @return {@link List} of {@link String} representing models built after year 2000
55+
*/
56+
publicstaticList<String>getModelsAfter2000UsingPipeline(List<Car>cars) {
57+
returncars.stream().filter(car ->car.getYear() >2000)
58+
.sorted(Comparator.comparing(Car::getYear))
59+
.map(Car::getModel).collect(Collectors.toList());
60+
}
61+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp