Let's assume we have something like that:
class Page{
Header header;
Body body;
Footer<TFooterModel> footer; //TFooterModel is a type of content (subcontrol in some sense) that loaded inside footer.
public Page(Header h, Body b, Footer<TFooterModel> f){...} //Let's assume that we setting part in constructor or using Builder pattern.
}
So, Footer
is generic one. However, it will be logically wrong, to make Page
also generic and use Page<TFooterModel>
, just because I don't really use TFooterModel
anywhere inside, except specifying Footer's
type. Converting Footer
to inheritance chain also seem wrong, since it's just a container for some content and there is no sense in inheritance, since children will be identical. This is looks like design issue for me, but I'm not sure how to fix it.
So, what is a proper way to handler those scenario, which really great developers use?
Footer it's just a container, which load some content using loadContent(BaseContent content)
, but it has an event. onContentLoaded(OnContentLoadedHandler<ContentImpl> handler)
, so just single difference between children will be a type of OnContentLoadedHandler
used, that's why I think that inheritance is something to much.
There was some options proposed.
- Create non-generic
Footer
, inherit generic footer from non-generic, and use non-generic inside Page - Pass OnContentLoadedHandler in
Footer's
constructor and avoid generic (however my personal opinion, it's could have some pitfalls, due to type resolving when calling handler (if it's lambda for example, or if it'sdynamic
, but i'm not fully sure.)) - Add interface and use it instead (probably logically similar to 1.)
Footer<TFooterModel>
as you show above...? What exactly is the problem that you need to "fix"?TFooterModel
be provided (I am pretty sure you meant it to be a type variable, otherwise there would not be any problem.)