Skip to content

Commit b90306b

Browse files
committed
第8章示例代码
1 parent a724f3d commit b90306b

8 files changed

Lines changed: 374 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.wbs.java8lambda.chapter8;
2+
3+
import java.util.function.Function;
4+
import java.util.function.UnaryOperator;
5+
6+
/**
7+
* TODO
8+
*
9+
* @author: wangbingshuai
10+
* @create: 2019-11-07 11:26
11+
**/
12+
public class ChainOfResponsibilityMain {
13+
public static void main(String[] args) {
14+
HeaderTextProcessing p1 = new HeaderTextProcessing();
15+
SpellCheckerProcessing p2 = new SpellCheckerProcessing();
16+
p1.setSuccessor(p2);
17+
String result1 = p1.handle("Aren't labdas really sexy?!!");
18+
System.out.println(result1);
19+
20+
UnaryOperator<String> headerProcessing = (String text) -> "From Raoul, Mario and Alan: " + text;
21+
UnaryOperator<String> spellCheckProcessing = (String text) -> text.replaceAll("labda", "lambda");
22+
Function<String, String> pipeline = headerProcessing.andThen(spellCheckProcessing);
23+
String result2 = pipeline.apply("Aren't labdas really sexy?!!");
24+
System.out.println(result2);
25+
}
26+
27+
private static abstract class ProcessingObject<T> {
28+
protected ProcessingObject<T> successor;
29+
30+
public void setSuccessor(ProcessingObject<T> successor) {
31+
this.successor = successor;
32+
}
33+
34+
public T handle(T input) {
35+
T r = handleWork(input);
36+
if (successor != null) {
37+
return successor.handleWork(r);
38+
}
39+
return r;
40+
}
41+
42+
abstract protected T handleWork(T input);
43+
}
44+
45+
private static class HeaderTextProcessing extends ProcessingObject<String> {
46+
@Override
47+
protected String handleWork(String input) {
48+
return "From Raoul, Mario and Alan: " + input;
49+
}
50+
}
51+
52+
private static class SpellCheckerProcessing extends ProcessingObject<String> {
53+
@Override
54+
protected String handleWork(String input) {
55+
return input.replaceAll("labda", "lambda");
56+
}
57+
}
58+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.wbs.java8lambda.chapter8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* TODO
8+
*
9+
* @author: wangbingshuai
10+
* @create: 2019-11-07 11:58
11+
**/
12+
public class Debugging {
13+
public static void main(String[] args) {
14+
List<Point> points = Arrays.asList(new Point(12, 2), null);
15+
points.stream().map(Point::getX).forEach(System.out::println);
16+
}
17+
18+
private static class Point {
19+
private int x;
20+
private int y;
21+
22+
public Point(int x, int y) {
23+
this.x = x;
24+
this.y = y;
25+
}
26+
27+
public int getX() {
28+
return x;
29+
}
30+
31+
public void setX(int x) {
32+
this.x = x;
33+
}
34+
}
35+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.wbs.java8lambda.chapter8;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.function.Supplier;
6+
7+
/**
8+
* TODO
9+
*
10+
* @author: wangbingshuai
11+
* @create: 2019-11-06 20:20
12+
**/
13+
public class FactoryMain {
14+
public static void main(String[] args) {
15+
Product p1 = ProductFactory.createProduct("loan");
16+
17+
Supplier<Product> loadSupplier = Loan::new;
18+
Product p2 = loadSupplier.get();
19+
20+
Product p3 = ProductFactory.createProductLambda("loan");
21+
}
22+
23+
private static class ProductFactory {
24+
public static Product createProduct(String name) {
25+
switch (name) {
26+
case "loan":
27+
return new Loan();
28+
case "stock":
29+
return new Stock();
30+
case "bond":
31+
return new Bond();
32+
default:
33+
throw new RuntimeException("No such product " + name);
34+
}
35+
}
36+
37+
public static Product createProductLambda(String name) {
38+
Supplier<Product> productSupplier = map.get(name);
39+
if (productSupplier != null) {
40+
return productSupplier.get();
41+
}
42+
throw new RuntimeException("No such product " + name);
43+
}
44+
}
45+
46+
interface Product {
47+
}
48+
49+
private static class Loan implements Product {
50+
}
51+
52+
private static class Stock implements Product {
53+
}
54+
55+
private static class Bond implements Product {
56+
}
57+
58+
final static private Map<String, Supplier<Product>> map = new HashMap<>();
59+
60+
static {
61+
map.put("loan", Loan::new);
62+
map.put("stock", Stock::new);
63+
map.put("bond", Bond::new);
64+
}
65+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.wbs.java8lambda.chapter8;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* TODO
8+
*
9+
* @author: wangbingshuai
10+
* @create: 2019-11-06 19:32
11+
**/
12+
public class ObserverMain {
13+
public static void main(String[] args) {
14+
Feed f = new Feed();
15+
f.registerObserver(new NYTimes());
16+
f.registerObserver(new Guardian());
17+
f.registerObserver(new LeMonde());
18+
f.notifyObserver("The queen said her favourite book is Java 8 in Action!");
19+
20+
Feed feedLambda = new Feed();
21+
feedLambda.registerObserver((String tweet) -> {
22+
if (tweet != null && tweet.contains("money")) {
23+
System.out.println("Breaking news in NY! " + tweet);
24+
}
25+
});
26+
feedLambda.registerObserver((String tweet) -> {
27+
if (tweet != null && tweet.contains("queen")) {
28+
System.out.println("Yet another news in London... " + tweet);
29+
}
30+
});
31+
feedLambda.notifyObserver("Money money money, give me money!");
32+
}
33+
34+
interface Observer {
35+
void inform(String tweet);
36+
}
37+
38+
interface Subject {
39+
void registerObserver(Observer o);
40+
41+
void notifyObserver(String tweet);
42+
}
43+
44+
private static class NYTimes implements Observer {
45+
@Override
46+
public void inform(String tweet) {
47+
if (tweet != null && tweet.contains("money")) {
48+
System.out.println("Breaking news in NY!" + tweet);
49+
}
50+
}
51+
}
52+
53+
private static class Guardian implements Observer {
54+
@Override
55+
public void inform(String tweet) {
56+
if (tweet != null && tweet.contains("queen")) {
57+
System.out.println("Yet another news in London... " + tweet);
58+
}
59+
}
60+
}
61+
62+
private static class LeMonde implements Observer {
63+
@Override
64+
public void inform(String tweet) {
65+
if (tweet != null && tweet.contains("wine")) {
66+
System.out.println("Today cheese, wine and news! " + tweet);
67+
}
68+
}
69+
}
70+
71+
private static class Feed implements Subject {
72+
private final List<Observer> observers = new ArrayList<>();
73+
74+
@Override
75+
public void registerObserver(Observer o) {
76+
this.observers.add(o);
77+
}
78+
79+
@Override
80+
public void notifyObserver(String tweet) {
81+
this.observers.forEach(o -> o.inform(tweet));
82+
}
83+
}
84+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.wbs.java8lambda.chapter8;
2+
3+
/**
4+
* TODO
5+
*
6+
* @author: wangbingshuai
7+
* @create: 2019-11-07 11:49
8+
**/
9+
abstract class OnlineBanking {
10+
public void processCustomer(int id) {
11+
Customer customer = Database.getCustomerWithId(id);
12+
makeCustomerHappy(customer);
13+
}
14+
15+
abstract void makeCustomerHappy(Customer customer);
16+
17+
private static class Customer {
18+
}
19+
20+
private static class Database {
21+
static Customer getCustomerWithId(int id) {
22+
return new Customer();
23+
}
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.wbs.java8lambda.chapter8;
2+
3+
import java.util.function.Consumer;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-11-07 11:53
10+
**/
11+
public class OnlineBankingLambda {
12+
public static void main(String[] args) {
13+
new OnlineBankingLambda().processCustomer(1337, (Customer c) -> System.out.println("Hello!"));
14+
}
15+
16+
void processCustomer(int id, Consumer<Customer> makeCustomerHappy) {
17+
Customer customer = Database.getCustomerWithId(id);
18+
makeCustomerHappy.accept(customer);
19+
}
20+
21+
private static class Customer {
22+
}
23+
24+
private static class Database {
25+
static Customer getCustomerWithId(int id) {
26+
return new Customer();
27+
}
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.wbs.java8lambda.chapter8;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
/**
8+
* TODO
9+
*
10+
* @author: wangbingshuai
11+
* @create: 2019-11-07 15:19
12+
**/
13+
public class Peek {
14+
public static void main(String[] args) {
15+
List<Integer> result = Stream.of(2, 3, 4, 5)
16+
.peek(x -> System.out.println("taking from stream:" + x))
17+
.map(x -> x + 17)
18+
.peek(x -> System.out.println("after map:" + x))
19+
.filter(x -> x % 2 == 0)
20+
.peek(x -> System.out.println("after filter:" + x))
21+
.limit(3)
22+
.peek(x -> System.out.println("after limit:" + x))
23+
.collect(Collectors.toList());
24+
}
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.wbs.java8lambda.chapter8;
2+
3+
/**
4+
* TODO
5+
*
6+
* @author: wangbingshuai
7+
* @create: 2019-11-06 19:19
8+
**/
9+
public class StrategyMain {
10+
public static void main(String[] args) {
11+
// old school
12+
Validator v1 = new Validator(new IsNumeric());
13+
System.out.println(v1.validate("aaaa"));
14+
Validator v2 = new Validator(new IsAllLowerCase());
15+
System.out.println(v2.validate("bbbb"));
16+
17+
// with lambdas
18+
Validator v3 = new Validator((String s) -> s.matches("\\d+"));
19+
System.out.println(v3.validate("aaaa"));
20+
Validator v4 = new Validator((String s) -> s.matches("[a-z]+"));
21+
System.out.println(v4.validate("bbbb"));
22+
}
23+
24+
interface ValidationStrategy {
25+
boolean execute(String s);
26+
}
27+
28+
private static class IsAllLowerCase implements ValidationStrategy {
29+
@Override
30+
public boolean execute(String s) {
31+
return s.matches("[a-z]+");
32+
}
33+
}
34+
35+
private static class IsNumeric implements ValidationStrategy {
36+
@Override
37+
public boolean execute(String s) {
38+
return s.matches("\\d+");
39+
}
40+
}
41+
42+
private static class Validator {
43+
private final ValidationStrategy strategy;
44+
45+
public Validator(ValidationStrategy strategy) {
46+
this.strategy = strategy;
47+
}
48+
49+
public boolean validate(String s) {
50+
return strategy.execute(s);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)