这本书最特别之处,在于它把 Java 当作一种函数式语言来教。当大多数编程书把「对象」当作世界的核心时,作者却让 new 一个对象变得像构造一个列表那样层层嵌套——一串 new Onion(new Lamb(new Skewer())) 读下来,读者会忽然察觉 class、interface、object 之间那层一直模糊的关系。全书围绕 visitor pattern 徐徐展开,不是告诉你「这是什么模式」,而是让你亲眼看着它如何从「数据类型变种越来越多、每次都要改代码」的困境里自然长出来。它适合那些不满足于「会用 Java」,而想理解面向对象与函数式究竟如何交汇的人。当然,也有人觉得它繁琐、最后一章晦涩,但正是这种较真,让读完的人对「设计」二字有了不同的体感。
AI导读
核心看点
用Java子集讲解函数式编程思想
循序渐进推导访问者模式原理
一问一答式引导理解OOP概念
读者共识
讲解清晰,自然引出设计模式
将Java当作函数式语言来玩
示例简练,适合入门学习Java
精彩摘录
"And a third? 21 Here's one more: new Onion( new Lamb( new Onion( new Skewer()))). Are there only Onions on this Shish: new Skewer()? 22 true, because there is neither Lamb nor Tomato on new Skewer()"
"If Java had parametric polymorphism for methods, no downward cast would be necessary for our visitors (Martin Odersky and Philip Wadler, Pizza into Java: Translating Theory into Practice, Conference Record on Principles of Programming Languages, 146-159. Paris, 1997)"
"Does that mean that anybody can write yy.p = new Bot() and thus change a pieman like yy? 36 No, because yy's type is Peiman, p isn't available. Only addTop, remTop, substTop, and occTop are visible. Isn't it good that we didn't include p in Peiman? 37 Yes, with this trick we can prevent others from "