【问题标题】:Extending an object with Builder Pattern using an inner class使用内部类使用 Builder 模式扩展对象
【发布时间】:2012-10-26 23:22:18
【问题描述】:

我要做的是创建一个使用 Builder 模式的类 (Square),然后在需要它的对象 (DrawMyCube) 内将该类扩展为内部类 (MyCube) .

由于有点复杂的原因,最好将它们扩展为内部类(对局部变量的引用)。

我试图使示例尽可能简单,因为实际用例过于复杂,无法在这里使用:

public abstract class Square {
    protected Integer length;
    protected Integer width;

    public abstract static class Builder {
        protected Integer length;
        protected Integer width;

        public abstract Builder length(Integer length);
        public abstract Builder width(Integer width);
    }

    protected Square(Builder builder) {
        this.length = builder.length;
        this.width = builder.width;
    }
}

现在我需要在这里扩展和使用它:

public class DrawMyCube {
    private String myText;
    private Integer height;
    private String canvas;
    private MyCube myCube;

    public DrawMyCube(String canvas) {
        this.canvas = canvas;
        myCube = new MyCube.Builder().length(10).width(10).text("HolaWorld").build();
    }

    public void drawRoutine() {
        myCube.drawMe(canvas);
    }

    protected class MyCube extends Square {
        protected String text;

        public static class Builder extends Square.Builder{
            protected String text;

            public Square.Builder length(Integer length) {this.length = length; return this;}
            public Square.Builder width(Integer width) {this.width = width; return this;}
            public Square.Builder text(String text) {this.text = text; return this;}
        }

        protected MyCube(Builder builder) {
            super(builder);
            this.text = text;
        }

        protected void drawMe(String canvas) {
            canvas.equals(this);
        }
    }
}

但问题是内部类中的静态 Builder:

成员类型Builder不能声明为静态;静态类型可以 只能在静态或顶级类型中声明。

或者,我可以将内部类 MyCube 创建为常规类,但问题是我无法引用 DrawMyCube 类中的任何内容(在实际用例中,有很多引用各种)。

【问题讨论】:

  • 我的回答有帮助吗?你最终做了什么决定?

标签: java inner-classes builder-pattern


【解决方案1】:

静态嵌套类只能在静态上下文中声明,这就是您看到编译器错误的原因。只需将您的 Builder 类声明为与 MyCube 相邻(或在静态上下文中的其他任何地方,没关系)。例如:

public class DrawMyCube {

    protected class MyCube extends Square { }

    public static class MyCubeBuilder extends Square.Builder { }
}

请注意,构建器需要引用外部DrawMyCube 实例才能实例化新的MyCube。出于这个原因,您可以将其设为MyCube 的内部(非静态)类:

public class DrawMyCube {

    protected class MyCube extends Square { }

    public class MyCubeBuilder extends Square.Builder { }
}

如您所见,我仍然将它声明为与 MyCube 相邻,因为将构建器作为其构建的内部类是没有意义的。

编辑: 正如您所提到的,一个简单的替代方法是将MyCube 设为静态:

public class DrawMyCube {

    protected static class MyCube extends Square {

        public static class Builder extends Square.Builder { }
    }
}

因为老实说,使用内部类并没有太大的好处 - 只是隐式的外部实例引用 - 这可以让您保持现有的层次结构和命名约定。您可以自己轻松实现对外部 DrawMyCube 的引用 - 只需要更多代码。


作为旁注,您可能希望使用泛型来实现构建器模式,例如抽象Builder<T>,其中实现构建T 的实例。事实上,没有办法缩小派生构建器类生成的范围。这是我暗示的草图:

abstract class Square { }

abstract class SquareBuilder<T extends Square> { }

class MyCube extends Square { }

class MyCubeBuilder extends SquareBuilder<MyCube> { }

【讨论】:

    猜你喜欢
    • 2014-08-06
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多