In java, can you use the builder pattern with required and reassignable fields?

后端 未结 5 694
南笙
南笙 2021-01-20 01:57

This is related to the following question:

How to improve the builder pattern?

I\'m curious whether it\'s possible to implement a builder with the following

5条回答
  •  不思量自难忘°
    2021-01-20 02:14

    Why don't you want to override the setters in BuilderFinal? They would just need to downcast the super method:

    public static class BuilderFinal extends BuilderC {
    
        @Override
        public BuilderFinal a(int v) {
            return (BuilderFinal) super.a(v);
        }
    
        @Override
        public BuilderFinal b(int v) {
            return (BuilderFinal) super.b(v);
        }
    
        public Foo build() {
            return new Foo(
                    a(),
                    b(),
                    c());
        }
    }
    

提交回复
热议问题