Is OO design's strength in semantics or encapsulation?

后端 未结 11 1379
余生分开走
余生分开走 2021-01-30 23:28

Object-oriented design (OOD) combines data and its methods. This, as far as I can see, achieves two great things: it provides encapsulation (so I don\'t care what data there is,

11条回答
  •  遥遥无期
    2021-01-31 00:11

    Your question reads like You want to derive the benefits of a house by analyzing a brick.

    Having the ability to provide semantic context and encapsulation are just the basic capabilities of a class in OO. (Like a brick can withstand a certain force and claim a certain space.)

    To continue the analogy: To get the maximum out of bricks, just put them together. The very same applies to classes and objects.

    There are a lot of design patterns that can be used for OO programming. Most of them rely on the abilities "encapsulation" and "semantic", that You mentioned.

    Some of those patterns are even an answer to the third paragraph of Your question:

    • If You want to extend the behaviour of an existing class, You can create a derived class.
    • If You want to extend or change the behaviour of an existing object, You might consider the decorator pattern.

提交回复
热议问题