I still have a little problem understanding the concept of pure OOD.
Let's say we have the class "Man" and we live in a world where sometimes people walk (the brain commands legs), sometimes trees disappear (people notice this), and sometimes people accidentally fall into each other.
The first two cases are really no problem:
class Tree {
private:
void disappear()
{
}
};
class Human {
public:
void onTreeDisappeared();
private:
int x, y, z;
void moveForward();
void hit(Human &target);
};
Now I have a very bad problem with the hit method. Of course it's nice that you can say
anna.hit(bob);
Until here I think itβs good (please, please, if something is bad) and read like prose (what a good OOP code should be). But how do you bear the blow in OOP? If Anna hits Bob and Bob falls, then the fall will not be directly caused by Anna or Bob. This is caused by bump, loss of balance and physics.
2 , - , :
public: void fallDown()
{ z = 0; }
public: void hit(Human &target)
{
bool targetFallsDown = true;
if(targetFallsDown)
{ target.fallDown(); }
}
"" . . , . :
private: void fallDown()
{ z = 0; }
public: void onHitCausesMeToFallDown()
{ fallDown(); }
public: void hit(Human &target)
{
bool targetFallsDown = true;
if(targetFallsDown)
{ target.onHitCausesMeToFallDown(); }
}
"", , "" . , , , - .
, , , , , B, B B.