Open-closed principle and Java “final” modifier

后端 未结 7 1215
灰色年华
灰色年华 2020-12-29 04:37

The open-closed principle states that \"Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification\".

However, J

7条回答
  •  天涯浪人
    2020-12-29 05:16

    I don't think the two statements contradict each other. A type can be open for extension and still be closed for inheritance.

    One way to do this is to employ dependency injection. Instead of creating instances of its own helper types, a type can have these supplied upon creation. This allows you to change the parts (i.e. open for extension) of the type without changing the type itself (i.e. close for modification).

提交回复
热议问题