A simple scenario, how to enable Tell Tell do not ask?

I am trying to simulate a basic scenario involving Person and Seat. A person has the Status: Sitting or Standing property. The seat has the Sit property, which indicates the person who is currently sitting in it. In addition, the seat is special in that it only “accepts” certain people to sit in it. I know that it’s weird for Seat to “accept” someone, but just imagine that he prefers certain people over others.

The following " Tell me, don't ask :" How do I create Person and Seat objects so that a person can sit in Sit only when the seat "accepts" him, and his status has changed to Sitting. My first thought was that a person should have a SitDown method as follows:

Person.SitDown(Seat seat);

But it looks like the Person class was supposed to check the state of the seat before it was sitting, and also update the Seat Seated property (instead of Seat updating the property itself):

// inside the Person class
void SitDown(Seat seat) {
    if (seat.AcceptsPlayer(this)) {
        seat.Seated = this;
        this.Status = Sitting;
    }
}

It seems better to have a Seat class pen position a person:

Seat.SeatPerson(Person person);

// inside Seat class
void SeatPerson(Person person) {
    if (IsAccepted(person)) {
        this.Seated = person;
        person.Status = Sitting;
    }
}

But it still requires Seat to change a person’s status. Is this a way to update a person’s status? Should only a person change his status? How would you simulate this simple scenario?

+5
6

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

+4

, . . , .

, , . "".

, .

+2

, . .

"Tell, Do not Ask" , . , , , .

void Person.SitDown(Seat seat) {
    if (seat.AcceptsPlayer(this)) {
        seat.SeatPerson(this);
        this.Status = Status.Sitting;
    }
}

void Seat.SeatPerson(Person person) {
    this.Seated = person;
}

( "Tell, Do not Ask" ) . . , .

void Person.SitDown(Seat seat) {
    if (seat.SeatPerson(this)) {
        this.Status = Status.Sitting;
    }
    else
    {
        //Couldn't sit down!
    }
}

bool Seat.SeatPerson(Person person) {
    if (this.IsAccepted(person) && this.Seated == null) {
        this.Seated = person;
        return true;
    }
    else
    {
        return false;
    }
}
+1

, , .

public class Seat
{
  public void SeatPerson(Person person, Action successAction)
  {
    if (IsAccepted(person))
    {
      this.Seated = person;
      successAction();
    }
  }
}


public class Person
{
  public void Sit(Seat seat)
  {
    seat.SeatPerson(this, this.SitComplete);
  }

  public void SitComplete()
  {
    this.Status = Sitting;
  }
}

.

, , , . , . , .

, successAction , SeatPerson. , Seat .

+1

Seat. Seat , . Seat Person isSitting() {return this.Status == Sittting; }

0

:

myPerson.TrySeat(targetseat), true, .

//inside Person class
        public bool TrySeat(Seat seat)
        {
            if (seat.TrySeat(this))
            {
                Status = Sitting;
                return true;
            }
            else
            {
                return false;
            }
        }

//inside Seat class
        internal bool TrySeat(Person person)
        {
            if (CanSeat(person))
            {
                Seated = person;
                return true;
            }
            else
            {
                return false;
            }
        }
-1

All Articles