Should a parent class ever reference child classes?

后端 未结 4 707

Good morning,

I inherited some legacy code at work and it is using a rather unusual design pattern. The only reference I could find on the forums to a similar patter

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-14 18:25

    Whether this is a good or bad practice it depends on situation. Eg when the 'parent' knows how to create all the children, it may be a good practice. When the parent doesn't know it, this solution will only cause trouble.

    Another problem is testability: if parent has lots of children, it may be hard to create parent in isolation from the children, but again it depends.

提交回复
热议问题