- 浏览: 728405 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (419)
- 杂软粉墨 (2)
- 创意灵感 (3)
- 经验记录 (137)
- 开源轨迹 (2)
- sip-communicator (2)
- 闲侃杂谈 (8)
- 问题交流 (24)
- 概念模式 (32)
- 难点备案 (5)
- JwChat (1)
- 中国象棋 (1)
- 教育探索 (6)
- 英语研究 (58)
- 星际争霸 (1)
- 电信知识 (1)
- 软件架构 (3)
- 哲学探索 (26)
- 算法灵魂 (8)
- 近视探索 (6)
- 数学数学 (3)
- 牛角钻尖 (23)
- 至强文言 (3)
- 数据结构 (1)
- 宇宙物理 (2)
- 网络架构 (3)
- 游戏领域 (4)
- 图形处理 (2)
- 修炼之路 (8)
- 读书天地 (20)
- 编解乱码 (2)
- 概念探索 (8)
- 格物致知 (1)
- 其它语言 (1)
- 测试领域 (3)
- 文化风流 (1)
- JQuery (1)
- 網頁領域 (1)
- Unix/Linux (1)
- Inside JVM (1)
- 异常分析 (1)
最新评论
-
suyujie:
引用
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
iamzhoug37:
您能说一下"局部变量不受文本顺序限制" 是 ...
声明前为什么能赋值却不能输出,都是使用
effective java 2nd Item 18
- 博客分类:
- 读书天地
Prefer interfaces to abstract classes
其中一段:
Interfaces allow the construction of nonhierarchical type frameworks.
Type hierarchies are great for organizing some things, but other things don’t fall
neatly into a rigid hierarchy. For example, suppose we have an interface representing
a singer and another representing a songwriter:
public interface Singer {
AudioClip sing(Song s);
}
public interface Songwriter {
Song compose(boolean hit);
}
In real life, some singers are also songwriters. Because we used interfaces
rather than abstract classes to define these types, it is perfectly permissible for a
single class to implement both Singer and Songwriter. In fact, we can define a
third interface that extends both Singer and Songwriter and adds new methods
that are appropriate to the combination:
public interface SingerSongwriter extends Singer, Songwriter {
AudioClip strum();
void actSensitive();
}
You don’t always need this level of flexibility, but when you do, interfaces are
a lifesaver. The alternative is a bloated class hierarchy containing a separate class
for every supported combination of attributes. If there are n attributes in the type
system, there are 2^n possible combinations that you might have to support.
This is
what’s known as a combinatorial explosion. Bloated class hierarchies can lead to
bloated classes containing many methods that differ only in the type of their arguments,
as there are no types in the class hierarchy to capture common behaviors
.
问题:
1. 这里提到的在type system中的attributes是指什么,因为对示例接口而言,没有域,难不成是指方法?
2. 为什么会有2^n种可能组合?假如有abc三个属性,那是(),(a),(b),(c),(a,b),(a,c)(b,c),(a,b,c)=2^3
如果是这个意思,它又要如何support?
3.
Bloated class hierarchies can lead to bloated classes containing many
methods that differ only in the type of their arguments, why?
为什么只能在参数类型上有区别?
4. there are no types in the class hierarchy to capture common behaviors?
这句话如何解释?
thanks in advance!
my answer:
因为是替代方案,所以试着对比着来看
interface:
public interface SingerSongwriter extends Singer, Songwriter {
AudioClip strum();
void actSensitive();
}
实际上它有4个方法
bloated class hierarchy:
就可能有
class SingerSongwriter extends Singer{
AudioClip sing(Song s);
Song compose(boolean hit);
AudioClip strum();
void actSensitive();
}
or
class SingerSongwriter extends Songwriter {
AudioClip sing(Song s);
Song compose(boolean hit);
AudioClip strum();
void actSensitive();
}
但
无论哪一种,都会缺失一种类型(there are no types in the class hierarchy to capture
common behaviors),以及可能连带缺失的类型,比如Song等,这样可能就会导致Bloated class hierarchies
can lead to bloated classes containing many methods that differ only in
the type of their arguments。
同时也满足了一个类包含所有可能属性组合(The alternative is a
bloated class hierarchy containing a separate class for every supported
combination of
attributes.),我觉得这里可以把attributes理解为方法之类的,方法也属于对象属性之一。这里恰好是2^2=4,有点全组合的味道,
但是不知道strum()和actSensitive怎么来的,权且把strum看成是sing和compose组合的产物,actSensitive代
表全组合中的空,这样就能解释2^n,总的来说还有些牵强的地方,但不知道他说的是否100%精准。
发表评论
-
Head First HTML with CSS and XHTML
2012-03-27 14:50 934i finished it by fast reading a ... -
head first jquery
2012-03-22 10:21 1076Thursday, March 22, 2012 fi ... -
Software Architecture Design Patterns in Java
2011-05-13 22:53 782to be continued... -
Effective Java 2nd edition
2011-04-25 16:19 986Item 3: Enforce the singleton p ... -
Clean Code: chapter 13~15
2011-04-20 17:20 987What follows is a series of p ... -
Clean Code: chapter 11~12
2011-04-15 09:35 1015The startup process is a concer ... -
Clean Code: chapter 9~10
2011-04-12 11:49 954Yes, we’ve come a long way; but ... -
Clean Code: chapter 6~8
2011-04-06 17:05 8062011.04.07 Objects and D ... -
Open Stanford Course: programming methodology 04
2011-04-05 11:52 886because this is sort of pre thi ... -
Open Stanford Course: programming methodology 03
2011-04-04 13:01 912any questions to start off with ... -
Clean Code: chapter 1~5
2011-04-02 11:26 898you should try several diffe ... -
Open Stanford Course: programming methodology 02
2011-03-31 00:30 917If you are stuck in the back, j ... -
Open Yale Course: Frontiers of Biomedical Engineering 01
2011-03-30 11:21 877http://www.verycd.com/topics/28 ... -
Open Stanford Course: programming methodology 01
2011-03-25 11:00 896you can pick them up on the ... -
Java Puzzler
2011-03-24 23:58 10302011.03.24 now i've been thr ... -
How Computers Work
2011-03-04 09:58 959Yes, im at last reading this fa ... -
The Java Language Specification Third Edition
2011-01-25 10:36 815starting day: 2011.01.25 -
Tricks of the Java Programming Gurus
2011-01-24 11:22 886In the world of programming, t ... -
Thinking in Java Fourth Edition
2011-01-17 15:59 958以前这些经典书籍都零零散散地阅览过,现在准备重新精读一遍,随时 ...
相关推荐
《Effective Java》第三版中文版目录 第一章 介绍 1 第二章 创建和销毁对象 4 1 考虑用静态工厂方法替换构造器 4 2 当遇到多个构造器参
Effective Java Effective Java Effective Java
effective c++ 2nd 中文
Effective Java读书笔记.pdf
effective-java.pdf
Effective java 3 学习记录
effective java 读书笔记,第二版自己摘要并翻译,以备速查。
Effective C++ 3rd Item 9 Effective C++ 3rd Item 9 Effective C++ 3rd Item 9 Effective C++ 3rd Item 9 Effective C++ 3rd Item 9
Effective Java读书笔记,记载了大部分我觉的有用的东西,前半部分有代码说明,但后半部分的代码,太过琐碎,就没有整理
《Effective Java》读书分享.pptx
【Effective Java】阅读笔记markdown 文件
Effective C++(2nd ED) Effective C++(2nd ED)
15. 使类和成员的可访问性最小化 16. 在公有类中使用访问方法而非公有域 17.使可变性最小化:不可变类
Effective C# 2nd Edition C#高效编程第二版,体验新的编程技巧!
effective-java 配套代码
有效的Java Java Effective 2nd Edition书中的源代码
[Addison-Wesley] Effective Java 2nd Edition (E-Book) ☆ 出版信息:☆ [作者信息] Joshua Bloch [出版机构] Addison-Wesley [出版日期] 2008年05月28日 [图书页数] 346页 [图书语言] 英语 [图书格式] PDF ...