Fluent interface implementation and reflection. Case study

I hope I picked the right group for this question…

I have this little framework I want to implement and I would like to have an opinion on it. In particular I am more concerned about the succinctness and style of the solution.
My class sketch looks like this:

public interface Builder<T> {

    T build();
}

public interface RootObject {


    public static abstract class MutableBuilder<T> implements Builder<T> {

        protected MutableBuilder() { }

        protected abstract void setA(A a);

        protected abstract void setB(B b);

        protected abstract void setC(C c);

        protected abstract void setD(D d);
    }
}

The goal is to have a Builder which returns an instance of (hopefully) immutable objects. The whole implementation is left to the client code, but I still want to give the imprinting of the Effective Java’s fluent interface.

If it was just for this, I would not be that puzzled. The problem comes into lay when I add the requirements:

  • the framework needs to reflectively search through a generic RootObject implementation in order to find if the MutableBuilder static inner class has been implemented.
  • if found, the framework instantiates one or more MutableBuilder dynamically (that is the reason why I have an abstract class with a constructor and return it or them to the client code.

The mere talking about reflection makes me wonder if I am doing the right thing. It looks a reasonable solution to me (of course).

4

If possible, go natural

I want to start by saying Java has built-in (“natural”) object creation techniques. Forcing your clients to declare types specifically for object creation for no specific reason will result in your framework being far from “easy-to-use”.

Avoid introspection if possible.


You’re misusing the (accepted) term “builder”.

A builder should only be used if a type has optional parameters. Not all types have this, so requiring a builder for every object is abuse of the pattern.

If you’re using the builder to decouple an implementation, the correct pattern to implement would be the factory method pattern

Prefer a factory over your current solution

In the comments of your post, you mentioned your framework now requires the client to pass in a builder. Although it’s nice you found a solution, there seems to be a bigger problem with your current design.

Builders should not define required properties, since the client isn’t forced to execute the behaviors provided by it. This turns the requirement into a (critically) recommended option, and is why builders still utilize class constructors. Your design currently misuses the pattern, and by the sound of the behavior you’re attempting to achieve, a factory would be better suited.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật