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 ...
随机推荐
- 前端翻译:Promises/A+规范
原文地址:https://promisesaplus.com/ 本篇为原文翻译+个人理解,若有谬误请各位指正,谢谢. 尊重原创,转载请注明来自:http://www.cnblogs.com/fsjoh ...
- C#函数式程序设计之局部套用与部分应用
函数式设计的核心与函数的应用以及函数如何作为算法的基本模块有关.利用局部套用技术可以把所有函数看成是函数类的成员,这些函数只有一个形参,有了局部套用,才有部分应用.部分应用是使函数模块化成为可能的两个 ...
- 组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix
Tom and matrix Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5226 Mean: 题意很简单,略. analy ...
- zTree的使用
一.节点模糊搜索功能:搜索成功后,自动高亮显示并定位.展开搜索到的节点. 二.节点异步加载:1.点击展开时加载数据:2.选中节点时加载数据. 前台代码如下: <script type=" ...
- 学习jQuery的事件dblclick
Insus.NET一直以来都是asp.net的开发的,少使用javascript.现在学习asp.net mvc了,jQuery是一个必须掌握的客户端语言. 不用急,慢慢来.一步一步.这篇练习jQue ...
- las数据集加载las数据
引用的类库:ESRI.ArcGIS.GeoDatabaseExtensions 逻辑步骤: 1.创建las数据集(ILasDataset). 2.实例化las数据集的编辑器(ILasDatasetEd ...
- SystemMenu类的用法
先声明对象以及相应常数: //SystemMenu对象 private SystemMenu m_systemMenu = null; // ID 常数定义 (可变,只要不与系统冲突即可) priva ...
- PHP框架中最喜欢的WindFramework
题外话, 像我这样从小到大作文打0分居多的人,写文章,实在是没有耐心的,抱歉. 尽管自己也山寨过许多PHP框架,但被山寨的对象中,最喜欢的是WindFramework. Yii其实更好,但太大而全. ...
- winform 属性
WinForm为客户端程序必须在.NET Framework框架上运行 一.常用属性: 布局: AutoScroll:当控件内容超出可见区域是否显示滚动条: Autosize:当控件内容有超出时是否自 ...
- has_many :through VS has_and_belongs_to_many
user role has_and_belongs_to_many role.destroy: 关联表user_roles先删除记录,再role删除. has_many :through user. ...