How can I understand the concept of pure OOD?

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()
    {
       // call onTreeDisappeared() for all human observers
    }
};

class Human {
  public:
    // The human notices that a tree disappeared
    void onTreeDisappeared();
  private:
    int x, y, z;
    // Human wants to walk forward
    void moveForward();
    // Hit another human, possibly causing him to fall down
    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; // could be random or any algorithm you like
  if(targetFallsDown)
  { target.fallDown(); }
}

"" . . , . :

private: void fallDown()
{ z = 0; }

public: void onHitCausesMeToFallDown()
{ fallDown(); }

public: void hit(Human &target)
{
  bool targetFallsDown = true; // could be random or any algorithm you like
  if(targetFallsDown)
  { target.onHitCausesMeToFallDown(); }
}

"", , "" . , , , - .

, , , , , B, B B.

+5
2

, , "" , .

? , , , , . , , . , .

OOD - , , .

, . , , , , , .

void Human::receiveHit(Hit hit)
{
    if (hit.IsBigForThisWeight(this->weight))
        this->fallDown();
}

, , , , , . . "", . , Hit . - .

+10

, - , . , anna.hit(bob).

, :

  • anna.hit(bob) , bob falling.

  • bob falling.

  • bob on_ground.

  • bob cries_for_mommy.

State , OO. " " Design Patterns.

+1

All Articles