General programming question. When to use OOP?

后端 未结 6 2202
没有蜡笔的小新
没有蜡笔的小新 2021-02-11 03:44

My program needs to do 2 things.

  1. Extract stuff from a webpage.

  2. Do stuff with a webpage.

However, there are many webpages, su

6条回答
  •  不思量自难忘°
    2021-02-11 04:26

    "My program needs to do 2 things."

    When you start out like that, the objects cannot be seen. You're perspective isn't right.

    Change your thinking.

    "My program works with stuff"

    That's OO thinking. What "stuff" does your program work with? Define the stuff. Those are your basic classes. There's a class for each kind of stuff.

    "My program gets the stuff from various sources"

    There's a class for each source.

    "My program displays the stuff"

    This is usually a combination of accessor methods of the stuff plus some "reporting" classes that gather parts of the stuff to display it.

    When you start out defining the "stuff" not the "do", you're doing OO programming. OO applies to everything, since every single program involves "doing" and "stuff". You can chose the "doing" POV (which is can be procedural or functional), or you can chose the "stuff" POV (which is object-oriented.)

提交回复
热议问题