OOD and subject matter confusion

Suppose I have a definition for a door:

class Door
{
    public void Lock()
    {
        // lock the door
    }
}

It seemed to make sense to me, at least for a while. But now I'm not sure. If I had a Person object that wanted to lock the Door, it would call aDoor.Lock (). But in real life, we do not close the doors, communicating the doors to lock ourselves.

It seems that a more accurate model of the situation will be the one who can directly change the state of aDoor, provided that it has sufficient power to lock the doors. For example, aCat should not set aDoor.IsLocked = true. I could see how to do this with properties if they support parameters:

class Person
{
    public void LockDoor(Door door)
    {
        door.IsLocked(this) = true;
    }
}

class Door
{
    bool isLocked;

    public bool IsLocked(Person person)
    {
        set
        {
            if(person != null) // ensure there is a real person trying to lock the door
            {
                this.isLocked = value;
            }
        }
    }
}

static void Main()
{
    Person personFromThinAir = new Person();
    Door doorFromThinAir = new Door();
    personFromThinAir.LockDoor(doorFromThinAir);
}

Instead, we can do the following:

class Person
{
    public void LockDoor(Door door)
    {
        door.SetLocked(this, true);
    }
}

class Door
{
    bool isLocked;

    public void SetLocked(Person person, bool locked)
    {
        if(person != null)
        {
            this.isLocked = locked;
        }
    }
}

, , , , , , , . , ? , ? , aDoor.Lock() ; , -, - .

+3
3

"" , ( frobbing) ( ), . , , , - , , . , , lock.lock(), .

, , , ( ). ( ) - , . , - , .

+9

, " ". . , , . , , , , .

- . , , . , , , :

class Door
{
    public bool Open(){}
    public bool Close(){}
    public void Lock(){}
    public void Unlock(){}
}
+6

- , , , /. , , - ( ), , , is_human /. , , /. , . , , , . , (-, ), ( ) , . .. ..

:

Player: "I am trying to unlock you."
Lock: "Do you meet requirement A?"
Player: "Yes"
Lock: "Do you meet requirement B?"  // Only some doors would ask this.
Player: "Yes"
Lock: "OK, you succeed.  I am unlocked!"

, , , , /.

#, , Java, , Java, #, , Player lock_unlock_credentials inner get_locked/get_unlocked Door ( Lock, ). lock_unlock_credentials , Player, Player, Player. Lockable , , . , , .

, #, -, .

0

Source: https://habr.com/ru/post/1713671/


All Articles