Is OO design's strength in semantics or encapsulation?

后端 未结 11 1378
余生分开走
余生分开走 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:25

    Some form of modularity is the key to any scalable design. The limitations of human beings prevents people from "grokking" too much information at once, so we have to subdivide problems into manageable, cohesive chunks, both to provide a basis for understanding a large project, as well as a way to subdivide the work assignments of a large project among many people.

    How to choose the most effective "division"/"partitioning" of a large project to meet the goals above? Experience has shown that OO is the big winner here, and I think many people would agree that two key attributes of OO that make it good at this are:

    • Encapsulated: each class encapsulates a "secret" - a set of implementation-specific assumptions-that-are-likely-to-have-to-change-over-time about its implementation - and exposes an interface that is agnostic to these assumptions; layering such encapsulated abstractions makes it possible to architect a robust design where components/implementations can easily be swapped in the face of anticipated changes.
    • Noun-centric: in most domains, humans seem better at first decomposing a domain model by thinking about the nouns/data of a domain, followed by identifying the supportive verbs that are associated with each noun.

    Regarding functional programming (FP) versus OO, I have blogged about this, but briefly I think that FP is more about implementation techniques whereas OO is more about program structure and design, and thus the two are complementary, with OO being more dominant on the 'large' end of the scale and FP being more dominant on the 'small' end. That is, on a large project, the high-level structure is best described by the OO class design, but many of the module-level details (implementations, and details of the shapes of the interfaces of the modules) are best shaped by FP influences.

提交回复
热议问题