Object oriented programming - class design confusion

前端 未结 13 1776
你的背包
你的背包 2021-02-05 22:01

I am trying to wrap my head around object oriented programming.

My understanding is that we have objects so we can design our programs to mirror real-life objects.

13条回答
  •  无人共我
    2021-02-05 22:44

    I always find examples using 'animals' or 'fruit' counter-intuitive. People are difficult objects to model, and it's unlikey that you'll encounter applications with the same requirements.

    Using the concept of anthropomorphisation can really help assign reponsibilities. Basically, imagine that your object is a person (I typically sketch them with a face and limbs during design sessions).

    Now you can ask these questions about your object:

    • "What responsibilites does my object have within the system?"
    • "Is this object responsible for doing xxx, or should xxx be the responsibility of another object?"
    • "Is this object doing more than it should be?" (e.g. if it's design to calculate something, it shouldn't be responsible for loading values)

    "Object Thinking" by David West is a good book to read on the topic.

提交回复
热议问题