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的更多相关文章

  1. 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 ...

  2. Effective Java 35 Prefer annotations to naming patterns

    Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...

  3. Effective Java 53 Prefer interfaces to reflection

    Disadvantage of reflection You lose all the benefits of compile-time type checking, including except ...

  4. 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 ...

  5. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  6. Effective Java 25 Prefer lists to arrays

    Difference Arrays Lists 1 Covariant Invariant 2 Reified at runtime Erased at run time 3 Runtime type ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 2015年百度之星初赛(1) --- A 超级赛亚ACMer

    超级赛亚ACMer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem D ...

  2. .net 中读取自定义Config文件

    今天做一个windows插件式服务程序,插件有时要读取配置文件的设置,但是服务是动态加载到服务上的,没有办法作到动态修改服务的配置文件(app.config).在.net 2.0中有一个Configu ...

  3. Winform开发框架之权限管理系统改进的经验总结(1)-TreeListLookupEdit控件的使用

    最近一直在做一些技术性的研究和框架改进工作,博客也落下好几天没有更新了,也该是时候静下心来,总结这段时间的一些技术改进的经验了.和上一阶段的CRM系统开发和技术研究一样,我都喜欢在一个项目或者模块完成 ...

  4. 【原创】本地通过IIS设置开发的localhost网站的域名改为个性域名方法

    效果图:   操作步骤如下:  第一步: 在本地IIS上新建个网站,如下图所示      第二步,修改host文件       加配置节点如下图所示       第三步,在vs里面找到你的web项目, ...

  5. iostat命令

    http://www.orczhou.com/index.php/2010/03/iostat-detail/ Linux系统出现了性能问题,一般我们可以通过top.iostat.free.vmsta ...

  6. 百度FIS入门

    1.fis作为nodejs的模块来管理的,所以首先得安装nodejs,看我前面的安装nodejs的文章. 2.官方的案例下载包https://github.com/hefangshi/fis-quic ...

  7. 性能分析之-- JAVA Thread Dump 分析综述

    性能分析之-- JAVA Thread Dump 分析综述       一.Thread Dump介绍 1.1什么是Thread Dump? Thread Dump是非常有用的诊断Java应用问题的工 ...

  8. Android5.0新特性——图片和颜色(drawable)

    图片和颜色 tint属性 tint属性一个颜色值,可以对图片做颜色渲染,我们可以给view的背景设置tint色值,给ImageView的图片设置tint色值,也可以给任意Drawable或者NineP ...

  9. Socket 学习实例

    整理一份Socket代码,整理前辈的代码 http://www.cnblogs.com/yellowapplemylove/archive/2011/04/19/2021586.html 直接贴代码 ...

  10. CSS选择器特殊性与重要性

    特殊性 在编写CSS代码的时候,我们会出现多个样式规则作用于同一个元素的情况,例如 <!-- HTML --> <header> <nav class="nav ...