Suppose I have a definition for a door:
class Door
{
public void Lock()
{
}
}
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)
{
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() ; , -, - .