Is it wrong to extend an inner static class in java? (Builder pattern)

I’m working on a java project for the university. The project is a card game in which you travel around a 2D map and fight against some enemies. My part consists of creating the deck and the cards.

I made the card interface, an abstract card and an implementation that extends the abstract card and implements the interface.

Now I also want an internal static builder in the abstract class and a related builder implementation in all classes that extend the abstract class (because if the new card has a new field I also want to add it in the builder). I have extended the builder in the implementation, but I don’t know if it is correct to extend a static class.

The idea came to me from:

Effective Java, 3rd edition, Joshua Bloch, Item 2: Consider a builder when faced with many constructor parametersconstructor parameters

The question is: Is it correct to extends a static inner class?

Because what I would mainly like is to “force” all classes that extend the abstract class to have a builder, since the constructor has many parameters.

1

You are facing an Open/close dilemma:

  • The abstract card is meant to be open for extension but closed for modification.
  • At the same time you want to force all extension to implement the builder pattern, which only takes the currently known construction parameters.
  • As a result, the extension is less open than it could: what if a concrete implementation of your card, for a variant of your game, would need more parameters for its construction?

Bloch’s builder pattern is primarily meant for dealing with many constructor parameters. If you want to generalize the construction process, independently of the concrete classes to instantiate, you may better look at GoF’s more general builder pattern, which aims to:

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

This pleads for decoupling the builder from the abstract class.

No, roughtly speaking, there is nothing wrong in defining a subclass of an inner class, static or not, at least a priori.

An inner class has a strong coupling / dependecy with its holding class. Thus, if a class called ANY “uses” an inner class called INNER which is defined as a inner class inside the class called HOLDER, ANY need to know both INNER and HOLDER definitions. In Java terms, it means that HOLDER and INNER must be in the same classpath of class ANY if ANY uses INNER.

Here “uses” means a parameter type, a local variable, a method call, an attribute or, as in your case, an inheritance relationship.

Thus, a subclass of the INNER class needs to know the HOLDER class as well. And any other class which uses this subclass of INNER eventually will need to know the definition of HOLDER in the same classpath.

The point if all those coupling is valid or has nothing about the fact that the class is a specialization of an inner class or not. The MDL principle suggests that the coupling is valid if it is necessary. If it is not required to a class know another class, the dependency may be avoided, allowing a more reusable and maintanable code. Hence, you would ask yourself: is there a context where the subclass of INNER can be used without the presence of the HOLDER class definition? If yes, it is time to a refactor, moving INNER to outside HOLDER. By the concrete use case of the game cards you illustrate, I don’t think it would be the case, so define a subclass of your inner builder looks fine.

Finally, being static inner class makes using INNER and its subclass easier (because you needn’t a proper instance of HOLDER at hand to use them). But it doesn’t change the dependency hierarchy described previously.

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