3分钟看完Java 8——史上最强Java 8新特性总结之第三篇 函数式编程技巧

耗尽温柔 提交于 2020-11-24 10:41:19

目录

· 改写设计模式

    · 策略模式(Strategy Pattern)

    · 模板方法模式(Template Method Pattern)

    · 观察者模式(Observer Pattern)

    · 责任链模式(Chain of Responsibility Pattern)

    · 简单工厂模式(Simple Factory Pattern)

· 高阶函数与柯里化


 

改写设计模式

策略模式(Strategy Pattern)

1. 改写前

    a) ValidationStrategy.java

1 public interface ValidationStrategy {
2 
3     boolean execute(String s);
4 
5 }

    b) IsNumeric.java

1 public class IsNumeric implements ValidationStrategy {
2 
3     public boolean execute(String s) {
4         return s.matches("\\d+");
5     }
6 
7 }

    c) IsAllLowerCase.java

1 public class IsAllLowerCase implements ValidationStrategy {
2 
3     public boolean execute(String s) {
4         return s.matches("[a-z]+");
5     }
6 
7 }

    d) Validator.java

 1 public class Validator {
 2     private final ValidationStrategy strategy;
 3 
 4     public Validator(ValidationStrategy v) {
 5         this.strategy = v;
 6     }
 7 
 8     public boolean validate(String s) {
 9         return strategy.execute(s);
10     }
11 }

    e) Test.java

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         Validator numericValidator = new Validator(new IsNumeric());
 5         boolean b1 = numericValidator.validate("aaaa");
 6         System.out.println(b1); // false
 7         Validator lowerCaseValidator = new Validator(new IsAllLowerCase());
 8         boolean b2 = lowerCaseValidator.validate("bbbb");
 9         System.out.println(b2); // true
10     }
11 
12 }

2.改写后

    a) Test.java

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         Validator numericValidator = new Validator((String s) -> s.matches("\\d+"));
 5         boolean b1 = numericValidator.validate("aaaa");
 6         System.out.println(b1); // false
 7         Validator lowerCaseValidator = new Validator(s -> s.matches("[a-z]+"));
 8         boolean b2 = lowerCaseValidator.validate("bbbb");
 9         System.out.println(b2); // true
10     }
11 
12 }

模板方法模式(Template Method Pattern)

1. 改写前

    a) Customer.java

 1 public class Customer {
 2 
 3     private int id;
 4     private String name;
 5 
 6     public Customer(int id, String name) {
 7         this.id = id;
 8         this.name = name;
 9     }
10 
11     public int getId() {
12         return id;
13     }
14 
15     public void setId(int id) {
16         this.id = id;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27 }

    b) OnlineBanking.java

 1 public abstract class OnlineBanking {
 2 
 3     public void processCustomer(int id) {
 4         Customer c = new Customer(id, "Jhon");
 5         makeCustomerHappy(c);
 6     }
 7 
 8     abstract void makeCustomerHappy(Customer c);
 9 
10 }

2.改写后

    a) OnlineBankingLambda.java

 1 import java.util.function.Consumer;
 2 
 3 public class OnlineBankingLambda {
 4 
 5     public void processCustomer(int id, Consumer<Customer> makeCustomerHappy) {
 6         Customer c = new Customer(id, "Jhon");
 7         makeCustomerHappy.accept(c);
 8     }
 9 
10 }

    b) Test.java

1 public class Test {
2 
3     public static void main(String[] args) {
4         new OnlineBankingLambda().processCustomer(1337, (Customer c) -> System.out.println("Hello " + c.getName()));
5     }
6 
7 }

观察者模式(Observer Pattern)

1. 改写前

    a) Observer.java

1 public interface Observer {
2 
3     void notify(String tweet);
4 
5 }

    b) NYTimes.java

1 public class NYTimes implements Observer {
2 
3     public void notify(String tweet) {
4         if (tweet != null && tweet.contains("money")) {
5             System.out.println("Breaking news in NY! " + tweet);
6         }
7     }
8 
9 }

    c) Guardian.java

1 public class Guardian implements Observer {
2 
3     public void notify(String tweet) {
4         if (tweet != null && tweet.contains("queen")) {
5             System.out.println("Yet another news in London... " + tweet);
6         }
7     }
8 
9 }

    d) LeMonde.java

1 public class LeMonde implements Observer {
2 
3     public void notify(String tweet) {
4         if (tweet != null && tweet.contains("wine")) {
5             System.out.println("Today cheese, wine and news! " + tweet);
6         }
7     }
8 
9 }

    e) Subject.java

1 public interface Subject {
2 
3     void registerObserver(Observer o);
4 
5     void notifyObservers(String tweet);
6 
7 }

    f) Feed.java

 1 public class Feed implements Subject {
 2 
 3     private final List<Observer> observers = new ArrayList<>();
 4 
 5     public void registerObserver(Observer o) {
 6         this.observers.add(o);
 7     }
 8 
 9     public void notifyObservers(String tweet) {
10         observers.forEach(o -> o.notify(tweet));
11     }
12 
13 }

    g) Test.java

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         Feed f = new Feed();
 5         f.registerObserver(new NYTimes());
 6         f.registerObserver(new Guardian());
 7         f.registerObserver(new LeMonde());
 8         f.notifyObservers("The queen said her favourite book is Java 8 in Action!");
 9     }
10 
11 }

2.改写后

    a) Test.java

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         Feed f = new Feed();
 5         f.registerObserver((String tweet) -> {
 6             if (tweet != null && tweet.contains("money")) {
 7                 System.out.println("Breaking news in NY! " + tweet);
 8             }
 9         });
10         f.registerObserver((tweet) -> {
11             if (tweet != null && tweet.contains("queen")) {
12                 System.out.println("Yet another news in London... " + tweet);
13             }
14         });
15         f.registerObserver((tweet) -> {
16             if (tweet != null && tweet.contains("wine")) {
17                 System.out.println("Today cheese, wine and news! " + tweet);
18             }
19         });
20         f.notifyObservers("The queen said her favourite book is Java 8 in Action!");
21     }
22 
23 }

责任链模式(Chain of Responsibility Pattern)

1. 改写前

    a) ProcessingObject.java

 1 public abstract class ProcessingObject<T> {
 2 
 3     protected ProcessingObject<T> successor;
 4 
 5     public void setSuccessor(ProcessingObject<T> successor) {
 6         this.successor = successor;
 7     }
 8 
 9     public T handle(T input) {
10         T r = handleWork(input);
11         if (successor != null) {
12             return successor.handle(r);
13         }
14         return r;
15     }
16 
17     protected abstract T handleWork(T input);
18 }

    b) HeaderTextProcessing.java

1 public class HeaderTextProcessing extends ProcessingObject<String> {
2 
3     public String handleWork(String text) {
4         return "From Raoul, Mario and Alan: " + text;
5     }
6 
7 }

    c) SpellCheckerProcessing.java

1 public class SpellCheckerProcessing extends ProcessingObject<String> {
2 
3     public String handleWork(String text) {
4         return text.replaceAll("labda", "lambda");
5     }
6 
7 }

    d) Test.java

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         ProcessingObject<String> p1 = new HeaderTextProcessing();
 5         ProcessingObject<String> p2 = new SpellCheckerProcessing();
 6         p1.setSuccessor(p2);
 7         String result = p1.handle("Aren't labdas really sexy?!!");
 8         System.out.println(result);
 9     }
10 
11 }

2.改写后

    a) Test.java

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         UnaryOperator<String> headerProcessing = (String text) -> "From Raoul, Mario and Alan: " + text;
 5         UnaryOperator<String> spellCheckerProcessing = (String text) -> text.replaceAll("labda", "lambda");
 6         Function<String, String> pipeline = headerProcessing.andThen(spellCheckerProcessing);
 7         String result = pipeline.apply("Aren't labdas really sexy?!!");
 8         System.out.println(result);
 9     }
10 
11 }

简单工厂模式(Simple Factory Pattern)

1. 改写前

    a) Product.java

1 public interface Product {
2 }

    b) Loan.java

1 public class Loan implements Product {
2 }

    c) Stock.java

1 public class Stock implements Product {
2 }

    d) Bond.java

1 public class Bond implements Product {
2 }

    e) ProductFactory.java

 1 public class ProductFactory {
 2 
 3     public static Product createProduct(String name) {
 4         switch (name) {
 5             case "loan":
 6                 return new Loan();
 7             case "stock":
 8                 return new Stock();
 9             case "bond":
10                 return new Bond();
11             default:
12                 throw new RuntimeException("No such product " + name);
13         }
14     }
15 
16 }

    f) Test.java

1 public class Test {
2 
3     public static void main(String[] args) {
4         Product p = ProductFactory.createProduct("loan");
5     }
6 
7 }

2. 改写后

    a) ProductFactory.java

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 import java.util.function.Supplier;
 4 
 5 public class ProductFactory {
 6 
 7     final static Map<String, Supplier<Product>> map = new HashMap<>();
 8 
 9     static {
10         map.put("loan", Loan::new);
11         map.put("stock", Stock::new);
12         map.put("bond", Bond::new);
13     }
14 
15     public static Product createProduct(String name) {
16         Supplier<Product> p = map.get(name);
17         if (p != null) return p.get();
18         throw new IllegalArgumentException("No such product " + name);
19     }
20 
21 }

    b) Test.java

1 public class Test {
2 
3     public static void main(String[] args) {
4         Product p = ProductFactory.createProduct("loan");
5     }
6 
7 }

高阶函数与柯里化

1. 高阶函数(Higher-order Function):满足以下任意一个条件都是高阶函数。

    a) 接受至少一个函数作为参数。

    b) 返回的结果是一个函数。

2. 柯里化(Currying):假设有一个函数 f(x, y) ,柯里化就是把多个参数的函数f转化为一个参数的函数g,并且函数g的返回值一个新函数,即 f(x, y) = (g(x))(y) 。

3. 柯里化好处:灵活、复用。

4. 举例

    a) 柯里化前

 1 public class Test {
 2 
 3     public static double converter(double x, double f, double b) {
 4         return x * f + b;
 5     }
 6 
 7     public static void main(String[] args) {
 8         double gbp = converter(1000, 0.6, 0);
 9         System.out.println(gbp);
10     }
11 
12 }

    b) 柯里化后

 1 public class Test {
 2 
 3     public static DoubleUnaryOperator curriedConverter(double f, double b) {
 4         return (double x) -> x * f + b;
 5     }
 6 
 7     public static void main(String[] args) {
 8         DoubleUnaryOperator convertCtoF = curriedConverter(9.0 / 5, 32);
 9         DoubleUnaryOperator convertUSDtoGBP = curriedConverter(0.6, 0);
10         DoubleUnaryOperator convertKmtoMi = curriedConverter(0.6214, 0);
11 
12         double gbp = convertUSDtoGBP.applyAsDouble(1000);
13         System.out.println(gbp);
14     }
15 
16 }

 

作者:netoxi
出处:http://www.cnblogs.com/netoxi
本文版权归作者和博客园共有,欢迎转载,未经同意须保留此段声明,且在文章页面明显位置给出原文连接。欢迎指正与交流。

 

 

 

原文出处:https://www.cnblogs.com/netoxi/p/10346537.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!