is it wrong for my parent object to make assumption about child objects?

I have a simple domain with about 6 objects in the model. For the most part they have a clear child/parent relationship with parent having multuple children. I’m going to have all of them be immutable. However, at some point I’ll need to both fetch a set of all objects of a given type, and be able to fetch a specific object of a certain type by it’s label.

I was going to have a model with some generic maps, and an abstract class for objects in the model. Adding to the model sticks the object in my map, and asks for the parent objects and sticks those in the map etc. Likewise a delete can delete all classes that called themselves children. This way I can query for specific classes in a generic method without writing logic to hunt down each class.

Once I thought of this my next thought was rather I can use a similar framework to save CPU by looking up children in the same manner. If I already have to store “these objects claimed x was a parent” to do the delete I can then say “give me children of X”. However, this assumes that X knows something about the other objects (what classes are effectively children of it). I can put in type safty via reflection mostly; but is it brittle for one object to presume that certain other class is going to claim it as a parent and persumably be in the model as such?

TO clarify, lets say I have a node and edge class. My edge would report a node as it’s parent. My Node can then say “give me all Objects that claimed me as parent and are edges” and do logic on them; even though it never did anything to ensure that there were edges out there using it as a node. Is this wrong?

Note, this is a small system. I know that there are great abstraction patterns for huge systems, that I mostly don’t think are worth the implementation cost or less explicit code for such a simple system.

0

Like an interface, a non-final class is like a contract. Classes that derive from it are expected to satisfy the contract, regardless of what other features they support. Object-oriented languages do a lot to ensure that child classes do indeed satisfy the contracts specified by their base-classes, but in a few cases, the contract cannot be satisfied by the features of the language alone.

You should do what you can within your language of choice to ensure that the contract is satisfied (generics or templates might help here if they are supported). But I would say it’s okay to treat the child classes as though they satisfy the contract if you are not able to guarantee it through the language.

If its not a framework (with new children being added by users) and you put a smaller version of question in the parent as code comments then its fine.

Even if it was a framework it would be okay as long you have warned users, though would be more useful if there was some validation/ checking -> explicit and/or via language features.

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