Effective Java 20 Prefer class hierarchies to tagged classes
Disadvantage of tagged classes
1. Verbose (each instance has unnecessary irrelevant fields).
2. Error-prone (Program will fail by initializing wrong fields).
3. Inefficient (Memory footprint is increased for not used field).
/**
* Tagged class - vastly inferior to a class hierarchy!
* @author Kaibo
*/
class Figure {
enum Shape {
RECTANGLE, CIRCLE
};
// Tag field - the shape of this figure
final Shape shape;
// These fields are used only if shape is RECTANGLE
double length;
double width;
// This field is used only if shape is CIRCLE
double radius;
// Constructor for circle
Figure(double radius) {
shape = Shape.CIRCLE;
this.radius = radius;
}
// Constructor for rectangle
Figure(double length, double width) {
shape = Shape.RECTANGLE;
this.length = length;
this.width = width;
}
double area() {
switch (shape) {
case RECTANGLE:
return length * width;
case CIRCLE:
return Math.PI * (radius * radius);
default:
throw new AssertionError();
}
}
}
/*
* Refined with Hierarchies
*/
/**
* Class hierarchy replacement for a tagged class
* @author Kaibo
*/
abstract class Figure {
abstract double area();
}
public class Rectangle extends Figure {
// specific fields
final double length;
final double width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
/*
* Common methods
* @see com.effectivejava.classinterface.Figure#area()
*/
@Override
double area() {
return length * width;
}
}
public class Circle extends Figure {
/**
* specific fields.
*/
final double radius;
Circle(double radius) {
this.radius = radius;
}
/*
* common method
* @see com.effectivejava.classinterface.Figure#area()
*/
@Override
double area() {
return Math.PI * (radius * radius);
}
}
Summary
If you're tempted to write a class with an explicit tag field, think about whether the tag could be eliminated and the class replaced by a hierarchy. When you encounter an existing class with a tag field, consider refactoring it into a hierarchy.
Effective Java 20 Prefer class hierarchies to tagged classes的更多相关文章
- Effective Java 69 Prefer concurrency utilities to wait and notify
Principle Use the higher-level concurrency utilities instead of wait and notify for easiness. Use Co ...
- Effective Java 35 Prefer annotations to naming patterns
Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...
- Effective Java 53 Prefer interfaces to reflection
Disadvantage of reflection You lose all the benefits of compile-time type checking, including except ...
- Effective Java 68 Prefer executors and tasks to threads
Principle The general mechanism for executing tasks is the executor service. If you think in terms o ...
- Effective Java 18 Prefer interfaces to abstract classes
Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...
- Effective Java 25 Prefer lists to arrays
Difference Arrays Lists 1 Covariant Invariant 2 Reified at runtime Erased at run time 3 Runtime type ...
- Effective Java 46 Prefer for-each loops to traditional for loops
Prior to release 1.5, this was the preferred idiom for iterating over a collection: // No longer the ...
- Effective Java 49 Prefer primitive types to boxed primitives
No. Primitives Boxed Primitives 1 Have their own values Have identities distinct from their values 2 ...
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
随机推荐
- ASP.NET 让无码编程不在是梦 -.NET通用平台、通用权限、易扩展、多语言、多平台架构框架
先拿出我半前年前平台的设计初稿,经过半年的努力我已经完成了该设计稿的所有功能.并且理念已经远远超出该设计稿. 下面是一些博友对我贴子的评价: 1.楼主,想法很美好,现实很骨感,我们公司就有一套你说的这 ...
- html5中的大纲
html5中的大纲 前言: 在html5中我们可以使用结构元素来编排一份大纲,这样我们就可以通过这个网页的大纲来了解网页中有哪些内容,网页中以什么样的形式来组织这些内容有更清楚的认识. 1.html5 ...
- vertical-align两种应用场合
vertical-align两种应用场合 (1)用在td/th中或display:table-cell元素中:让当前元素中的文本内容在竖直方向上居中 css部分: .content{ ...
- [JS] javascript基础语法
W3CSchool全套Web开发手册:点击下载 1.javascript是什么 js是具有面向对象能力的,解释性的程序设计语言. 2.js的类型 [基本类型]:string number boolea ...
- 0506团队项目-Scrum 项目1.0
题目 1.应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 2.录制为演说视频,上传到视频网站,并把链接发到团队博客上. 团队项目选题 金融工具:复利计算与投资记录项目继续升级,开 ...
- 已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性。
错误:已超过传入消息(65536)的最大消息大小配额.若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 属性. 或者 错误:反序列化操作“GetAllUserData ...
- 创业型互联网公司应该选择PHP, JavaEE还是.NET技术路线?
通常JavaEE和.NET被定义为构建大型在线系统,因为其支持面向对象设计,异步通讯,MVC等都相对比较完善,而PHP通常用于构建比较轻量的业务,例如SNS服务. 因为实施速度快,工程师社区规模大,开 ...
- Python入门笔记(19):Python函数(2):函数/方法装饰器
一.装饰器(decorators) 装饰器的语法以@开头,接着是装饰器函数的名字.可选参数. 紧跟装饰器声明的是被装饰的函数和被装饰的函数的可选参数,如下: @decorator(dec_opt_ar ...
- Discuz!X3解读之类引入机制及目录结构
实例: - /source/class/table/table_forum_faq.php - /source/class/model/model_forum_post.php - /source/p ...
- 并发式IO的解决方案:多路非阻塞式IO、多路复用、异步IO
在Linux应用编程中的并发式IO的三种解决方案是: (1) 多路非阻塞式IO (2) 多路复用 (3) 异步IO 以下代码将以操作鼠标和键盘为实例来演示. 1. 多路非阻塞式IO 多路非阻塞式IO访 ...