I have a MeetingRoom object that contains a list of Employees, there is a function on each employee called SetMeetingDate, this method has to take into account the state of all other employees in the MeetingRoom in particicular the IsReadyForMeeting property.
An individual employee for example can only successfully call SetMeetingDate(Date date) when every employee in the ChatRoom has IsReadyForMeeting set to true.
I have thought about a couple of ways to implement this functionality, I can do something where the employee checks the state of the other employees by going via the MeetingRoom object like this:
public void SetMeetingDate(DateTime date)
{
if (!MeetingRoom.AllEmployeesAreReady)
{
throw new ApplicationException("all employees are not ready");
}
MeetingDate = date;
}
which seems to me to have the responsibility in the wrong place.
A second approach that I can use is to set up an observer so that the Employee class will inform the MeetingRoom object that it has set a meeting date and the MeetingRoom class will raise the exception if MeetingRoom.AllEmployeesAreReady is false thus moving the responsibility to the MeetingRoom object and the code for the employee would look like this:
public void SetMeetingDate(DateTime date)
{
MeetingDate = date;
Observer.MeetingDateAdded();
}
And the Meeting Room would then have a method like this:
public void MeetingDateAdded()
{
if (!AllEmployeesAreReady)
{
throw new ApplicationException("all employees are not ready");
}
}
This use of the observer has been met with opposition from other developers in my team and I can see why but I can't think of a better way to implement the requirement, is there an easy/different approach that can be used in this situation?
"not all employees are ready"
since your condition is that ALL employees must be ready, not some.SetMeetingDate
? That's also the meeting's job. A meeting should be a combination of attendees, location, and time, and it's the meeting's job to make sure these all check out, like all attendees actually being available at the given time, the location being available, etc.