zoukankan      html  css  js  c++  java
  • 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.

       

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    关于mysql的wait_timeout参数 设置不生效的问题【转】
    mysql只读模式的设置方法与实验【转】
    ansible批量修改linux服务器密码的playbook
    Serv-U 的升级及数据备份和迁移【转】
    java系统的优化
    JBoss6.1.0修改启动jvm内存以及修改日志级别【转】
    JAVA_OPTS讲解【转】
    RabbitMQ集群、镜像部署配置
    LVS+MYCAT+读写分离+MYSQL主备同步部署手册
    常用数据库高可用和分区解决方案(2) — MongoDB篇
  • 原文地址:https://www.cnblogs.com/haokaibo/p/prefer-class-hierarchies-to-tagged-classes.html
Copyright © 2011-2022 走看看