咱們會介紹幾種方法,幫助你重構代碼,以適配使用Lambda表達式,讓你的代碼具有更好的可讀性和靈活性。除此以外,咱們還會討論目前比較流行的幾種面向對象的設計模式, 包括策略模式、模板方法模式、觀察者模式、責任鏈模式,以及工廠模式,在結合Lambda表達式以後變得更簡潔的狀況。最後,咱們會介紹如何測試和調試使用Lambda表達式和Stream API的代碼。java
Java 8的新特性也能夠幫助提高代碼的可讀性:git
利用Lambda表達式、方法引用以及Stream改善程序代碼的可讀性:github
將實現單一抽象方法的匿名類轉換爲Lambda表達式算法
// 傳統的方式,使用匿名類
Runnable r1 = new Runnable(){
public void run(){
System.out.println("Hello");
}
}
// 新的方式,使用Lambda表達式
Runnable r2 = () -> System.out.println("Hello");
複製代碼
匿名 類和Lambda表達式中的this和super的含義是不一樣的。在匿名類中,this表明的是類自身,可是在Lambda中,它表明的是包含類。其次,匿名類能夠屏蔽包含類的變量,而Lambda表達式不能(它們會致使編譯錯誤),以下面這段代碼:設計模式
int a = 10;
Runnable r1 = () -> {
int a = 2; // 編譯錯誤
System.out.println(a);
};
Runnable r2 = new Runnable() {
public void run() {
int a = 2; // 正常
System.out.println(a);
}
}
複製代碼
在涉及重的上下文裏,將匿名類轉換爲Lambda表達式可能致使最終的代碼更加晦澀。實際上,匿名類的類型是在初始化時肯定的,而Lambda的類型取決於它的上下文。經過下面這個例子,咱們能夠了解問題是如何發生的。咱們假設你用與Runnable一樣的簽名聲明瞭一個函數接口,咱們稱之爲Task:架構
interface Task{
public void execute();
}
public static void doSomething(Runnable r){ r.run(); }
public static void doSomething(Task a){ a.execute(); }
doSomething(new Task() {
public void execute() {
System.out.println("Danger danger!!");
}
});
// doSomething(Runnable) 和 doSomething(Task) 都匹配該類型
doSomething(() -> System.out.println("Danger danger!!"));
// 使用顯式的類型轉換來解決這種模棱兩可的狀況
doSomething((Task)() -> System.out.println("Danger danger!!"));
複製代碼
目前大多數的集成開發環境,好比NetBeans和IntelliJ都支持這種重構,它們能自動地幫你檢查,避免發生這些問題。app
Map<CaloricLevel, List<Dish>> dishesByCaloricLevel =
menu.stream()
.collect(
groupingBy(dish -> {
if (dish.getCalories() <= 400) return CaloricLevel.DIET;
else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
else return CaloricLevel.FAT;
}));
複製代碼
將Lambda表達式的內容抽取到一個單獨的方法中,將其做爲參數傳遞給groupingBy方法。變換以後,代碼變得更加簡潔,程序的意圖也更加清晰了。ide
Map<CaloricLevel, List<Dish>> dishesByCaloricLevel = menu.stream().collect(groupingBy(Dish::getCaloricLevel));
複製代碼
咱們建議你將全部使用迭代器這種數據處理模式處理集合的代碼都轉換成Stream API的方式。爲何呢? Stream API能更清晰地表達數據處理管道的意圖。除此以外,經過短路和延遲載入以及利用第7章介紹的現代計算機的多核架構,咱們能夠對Stream進行優化。函數
// 命令式版本
List<String> dishNames = new ArrayList<>();
for(Dish dish: menu){
if(dish.getCalories() > 300){
dishNames.add(dish.getName());
}
}
// 使用Stream API
menu.parallelStream()
.filter(d -> d.getCalories() > 300)
.map(Dish::getName)
.collect(toList());
複製代碼
沒有函數式接口就沒法使用Lambda表達式,所以代碼中須要引入函數式接口。引入函數式接口的兩種通用模式:工具
使用Lambda表達式後,不少現存的略顯臃腫的面向對象設計模式可以用更精簡的方式實現了。這一節中,咱們會針對五個設計模式展開討論,它們分別是:
策略模式表明瞭解決一類算法的通用解決方案,你能夠在運行時選擇使用哪一種方案。策略模式包含三部份內容,如圖所示。
public interface ValidationStrategy {
boolean execute(String s);
}
public class IsAllLowerCase implements ValidationStrategy {
@Override
public boolean execute(String s) {
return s.matches("[a-z]+");
}
}
public class IsNumber implements ValidationStrategy {
@Override
public boolean execute(String s) {
return s.matches("\\d+");
}
}
public class Validator {
private final ValidationStrategy strategy;
public Validator(ValidationStrategy strategy) {
this.strategy = strategy;
}
public boolean validate(String s) {
return strategy.execute(s);
}
}
public class StrategyDemo {
public static void main(String[] args) {
Validator numericValidator = new Validator(new IsNumber());
boolean b1 = numericValidator.validate("aaaa");
Validator lowerCaseValidator = new Validator(new IsAllLowerCase());
boolean b2 = lowerCaseValidator.validate("bbbb");
System.out.println(b1 + " " + b2);
Validator numericValidator1 = new Validator((String s) -> s.matches("[a-z]+"));
boolean b11 = numericValidator1.validate("aaaa");
Validator lowerCaseValidator1 = new Validator((String s) -> s.matches("\\d+"));
boolean b21 = lowerCaseValidator.validate("bbbb");
System.out.println(b11 + " " + b21);
}
}
複製代碼
模板 方法模式在你「但願使用這個算法,可是須要對其中的某些行進行改進,才能達到但願的效果」 時是很是有用的。
public abstract class OnlineBanking {
public void processCustomer(int id) {
Customer c = DataUtil.getCustomerWithId(id);
makeCustomerHappy(c);
}
abstract void makeCustomerHappy(Customer c);
}
public class OnlineBankingLambda {
public void processCustomer(int id, Consumer<Customer> consumer) {
Customer c = DataUtil.getCustomerWithId(id);
consumer.accept(c);
}
}
public class TemplateMethod {
public static void main(String[] args) {
new OnlineBanking() {
@Override
void makeCustomerHappy(Customer c) {
System.out.println(c.getName() + " happy!");
}
}.processCustomer(1);
new OnlineBankingLambda().processCustomer(1, (Customer c) -> System.out.println(c.getName() + " happy!"));
}
}
複製代碼
觀察者模式是一種比較常見的方案,某些事件發生時(好比狀態轉變),若是一個對象(一般咱們稱之爲主題)須要自動地通知其餘多個對象(稱爲觀察者),就會採用該方案。
public interface Observer {
void notify(String tweet);
}
public class NYTime implements Observer {
@Override
public void notify(String tweet) {
if(tweet != null && tweet.contains("money")){
System.out.println("Breaking news in NY! " + tweet);
}
}
}
public class Guardian implements Observer {
@Override
public void notify(String tweet) {
if(tweet != null && tweet.contains("queen")){
System.out.println("Yet another news in London... " + tweet);
}
}
}
public class LeMonde implements Observer {
@Override
public void notify(String tweet) {
if(tweet != null && tweet.contains("wine")){
System.out.println("Today cheese, wine and news! " + tweet);
}
}
}
public interface Subject {
void registerObserver(Observer o);
void nofityObservers(String tweet);
}
public class Feed implements Subject {
private final List<Observer> observers = new ArrayList<>();
@Override
public void registerObserver(Observer o) {
this.observers.add(o);
}
@Override
public void nofityObservers(String tweet) {
observers.forEach(o -> o.notify(tweet));
}
}
public class ObserverDemo {
public static void main(String[] args) {
Feed f = new Feed();
f.registerObserver(new NYTime());
f.registerObserver(new Guardian());
f.registerObserver(new LeMonde());
f.nofityObservers("The queen said her favourite book is Java 8 in Action!");
f.registerObserver((String tweet) -> {
if (tweet != null && tweet.contains("money")) {
System.out.println("Breaking news in NY! " + tweet);
}
});
f.registerObserver((String tweet) -> {
if (tweet != null && tweet.contains("queen")) {
System.out.println("Yet another news in London... " + tweet);
}
});
}
}
複製代碼
責任鏈模式是一種建立處理對象序列(好比操做序列)的通用方案。一個處理對象可能須要在完成一些工做以後,將結果傳遞給另外一個對象,這個對象接着作一些工做,再轉交給下一個處理對象,以此類推。
public abstract class ProcessingObject<T> {
protected ProcessingObject<T> successor;
public void setSuccessor(ProcessingObject<T> successor) {
this.successor = successor;
}
public T handle(T input) {
T r = handleWork(input);
if (successor != null) {
return successor.handle(r);
}
return r;
}
abstract protected T handleWork(T input);
}
public class HeaderTextProcessing extends ProcessingObject<String> {
@Override
protected String handleWork(String input) {
return "From Raoul, Mario and Alan: " + input;
}
}
public class SpellCheckerProcessing extends ProcessingObject<String> {
@Override
protected String handleWork(String input) {
return input.replaceAll("labda", "lambda");
}
}
public class ChainOfResponsibilityDemo {
public static void main(String[] args) {
ProcessingObject<String> p1 = new HeaderTextProcessing();
ProcessingObject<String> p2 = new SpellCheckerProcessing();
p1.setSuccessor(p2);
String result = p1.handle("Aren't labdas really sexy?!!");
System.out.println(result);
UnaryOperator<String> headerProcessing = (String text) -> "From Raoul, Mario and Alan: " + text;
UnaryOperator<String> spellCheckerProcessing = (String text) -> text.replaceAll("labda", "lambda");
Function<String, String> pipeline = headerProcessing.andThen(spellCheckerProcessing);
String result1 = pipeline.apply("Aren't labdas really sexy?!!");
System.out.println(result1);
}
}
複製代碼
使用工廠模式,你無需向客戶暴露實例化的邏輯就能完成對象的建立。
public interface Product {}
@Data
public class Loan implements Product {}
@Data
public class Stock implements Product {}
@Data
public class Bond implements Product {}
public class ProductFactory {
public static Product createProduct(String name) {
switch (name) {
case "loan":
return new Loan();
case "stock":
return new Stock();
case "bond":
return new Bond();
default:
throw new RuntimeException("No such product " + name);
}
}
public static void main(String[] args) {
Product p = ProductFactory.createProduct("loan");
System.out.println(p);
}
}
public class ProductFactoryLambda {
private final static Map<String, Supplier<Product>> map = new HashMap<>();
static {
map.put("loan", Loan::new);
map.put("stock", Stock::new);
map.put("bond", Bond::new);
}
public static Product createProduct(String name) {
Supplier<Product> p = map.get(name);
if (p != null) {
return p.get();
}
throw new IllegalArgumentException("No such product " + name);
}
public static void main(String[] args) {
Product p = ProductFactoryLambda.createProduct("loan");
System.out.println(p);
}
}
複製代碼
略
public class Debugging{ 11 public static void main(String[] args) {
List<Point> points = Arrays.asList(new Point(12, 2), null);
points.stream().map(p -> p.getX()).forEach(System.out::println); }
}
運行這段代碼會產生下面的棧跟蹤:
Exception in thread "main" java.lang.NullPointerException
at Debugging.lambda$main$0(Debugging.java:6)
at Debugging$$Lambda$5/284720968.apply(Unknown Source)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline
.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators
.java:948)
複製代碼
咱們須要特別注意,涉及Lambda表達式的棧可能很是難理解。這是Java編譯器將來版本能夠改進的一個方面。
peek的設計初衷就是在流的每一個元素恢復運行以前,插入執行一個動做。可是它不像forEach那樣恢復整個流的運行,而是在一個元素上完成操做以後,它只會將操做順承到流水線中的下一個操做。圖8-4解釋了peek的操做流程。
List<Integer> result = numbers.stream()
.peek(x -> System.out.println("from stream: " + x))
.map(x -> x + 17)
.peek(x -> System.out.println("after map: " + x))
.filter(x -> x % 2 == 0)
.peek(x -> System.out.println("after filter: " + x))
.limit(3)
.peek(x -> System.out.println("after limit: " + x))
.collect(toList());
輸出結果:
from stream: 2
after map: 19
from stream: 3
after map: 20
after filter: 20
after limit: 20
from stream: 4
after map: 21
from stream: 5
after map: 22
after filter: 22
after limit: 22
複製代碼
本文同步發表在公衆號,歡迎你們關注!😁 後續筆記歡迎關注獲取第一時間更新!