繁体   English   中英

使用内部类使用Builder Pattern扩展对象

[英]Extending an object with Builder Pattern using an inner class

我要做的是创建一个使用Builder模式的类( Square ),然后将该类作为内部类( MyCube )扩展到Object所需的位置( DrawMyCube )。

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

我试图让这个例子尽可能简单,因为实际的用例太复杂了,不能在这里使用:

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不能声明为static; 静态类型只能在静态或顶级类型中声明。

或者,我可以将内部类MyCube创建为常规类,但问题变得我无法引用回DrawMyCube类中的任何内容(并且在实际用例中有许多对这些类的引用)。

静态嵌套类只能在静态上下文中声明,这就是您看到编译器错误的原因。 只需声明您的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> { }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM