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 ...
随机推荐
- 利用 ELK系统分析Nginx日志并对数据进行可视化展示
一.写在前面 结合之前写的一篇文章:Centos7 之安装Logstash ELK stack 日志管理系统,上篇文章主要讲了监控软件的作用以及部署方法.而这篇文章介绍的是单独监控nginx 日志分析 ...
- 模拟浏览器搜索功能(Ctrl + F)
写的匆忙,示意一下,有待完善.把以下代码复制到文本文件中,把文件扩展名改为.html就可以运行了. <html> <head> <style type="tex ...
- css 布局absolute与relative的区别
absolute:当使用时,表示在文档流中没有实际存在位置(浮动),在不设置任何方位值时,只能按兵不动,当设置了方位值之后,会紧接着去寻找距离最近的能够将它包含住的父级元素,然后进行定位. relat ...
- 使用VS开发C语言
在嵌入开发板上做了一段时间的C语言开发后,今天突然心血来潮,想起大学时期在TurboC和TC3下写代码的情形.大一时宿舍里有台386(在当时是算比较先进的了),大一大二基本上都在玩DOS和WIN31. ...
- C++隐藏规则
在面向对象的开发过程中,经常出现类的继承,这里面出现的成员函数的重载(overload).覆盖(override)与隐藏(hidden)很容易混淆. 首先澄清这3个概念: 重载 相同的范围(在同一个类 ...
- windows的host文件的位置和作用
在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\ ...
- sql跨数据库转移
结构一样的话insert into 数据库A.dbo.TableAselect * from 数据库B.dbo.TableA 另外:nsert into DDD(字段1,字段2,字段3 .....)( ...
- bootstrap dialog自行控制窗口的关闭
在使用dialog的时候,我们通常不希望点击btn的时候自动隐藏dialog,通常需要做一些清理或者ajax操作,在bootstrap dialog中,这是通过 data-dismiss=" ...
- 自己写的表格插件autotable
自己写的表格插件autotable 作者:田想兵,个人网址:http://www.lovewebgames.com 这个表格插件所完成的功能是:ajax请求数据,然后动态绑定到指定表格下,格式化,分页 ...
- IOS6学习笔记(一)
一.ARC 1.ARC环境下可以使用-(void)dealloc{};处理一些事情(比如移除KVO观察),但不要调用[super dealloc]; 2.ARC与非ARC混编要注意符合Cocoa命名约 ...