Making subclass more type-specific with accessors

I have a super class: TriggerManager with a subclass TimedTriggerManager. NOTE: I’m working in java

TimedTriggerManager only deals with TimedTrigger s, a subclass of Trigger. TriggerManager references all Trigger objects. To modify the subclass (TimedTriggerManager) to only accept TimedTriggers into its accessors and mutators (getters and setters) I have the following questions:

1) I have in TriggerManager the following public void addTrigger(Trigger t). In TimedTriggerManager should I just override the method to throw an error if the argument (t) is not a TimedTrigger. If so, What error should I throw? If not, what should I do?

2) Should I add another method getTimedTrigger to work alongside the supper class’s getTrigger so that I don’t have to keep type casting? (Since the latter would return a Trigger object)

3) Is there anything else I’m forgetting, or some common practice that I’m not doing?

1

Your situation reeks of the classic Shape hierarchy where a Square inherits from Rectangle (and adds the constraint that both dimensions are the same). This is generally regarded as failing the Liskov Substitution Principle (LSP), because the added constraint makes that the base-class operations are not always appropriate for the derived class.

If the getTrigger method is only meant for use within the hierarchy, then you could turn the inheritance between TriggerManager and TimedTriggerManager around (making TimedTriggerManager the base class), as a derived class may weaken the requirements placed on method parameters but not strengthen them.

If getTrigger is also meant for outside use, then TriggerManager and TimedTriggerManager can’t have an inheritance relation with each other without violating the LSP.
In that case, it might be best to make TriggerManager and TimedTriggerManager sibling-classes that both inherit from a AbstractTriggerManager. The AbstractTriggerManager provides all the common functionality, but no public methods where TriggerManager and TimedTriggerManager place different requirements on the parameters (such as addTrigger).

1) Yes. Unless you have a common interface for both Trigger and TimedTrigger, which means the TimedTriggerManager does not require any special functionality from TimedTrigger, you should check the type of the received Trigger and ensure its a TimedTrigger.

2) It doesn’t seem a good idea to me. The base class usually should not know anything about the the derived classes. So I think casting is not a big problem here. It results in a cleaner design in my opinion.

3) Do you actually use a concrete TriggerManager class directly? One think that you may want to consider is to use an interface instead of a base object for the specialized managers.

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