Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Object Hierarchy - To Event or not to Event

Your job is to design a Project Plan class library which supports the tracking of tasks (similar to how MS Project works). This class library has a Task object (among others).

The Task object has a EstimatedHours (Double), StartDate (DateTime), and EndDate (DateTime) properties, among others. A Task object can have one parent Task, and several children Task objects. The EstimatedHours, StartDate, and EndDate properties of a Task which has children (is a parent) depend on those properties of its immediate children. The parent Task's StartDate is the earliest StartDate of its children. The parent Task's EndDate is the latest EndDate of its children. The parent Task's EstimatedHours is the sum of its children's EstimatedHours. Therefore, it is invalid to change these properties on a Task which has children.

How would you handle the use case where the EstimatedHours, StartDate, or EndDate are changed on a task which has a parent? (The parent's properties are a reflection of its children, so any changes to children may require the parent's properties to be adjusted to reflect the changes appropriately)

One option is to have an event for when each property changes. A parent Task would listen for these events on its immediate children Task objects, and make appropriate changes to its own properties when those events occurred. Is this a good approach, or is there a better way? How would you do it?

Here's a basic idea of what a Task object might look like:

Public Class Task

  Private mChildren As List(Of Task)

  Private mEndDate As DateTime = DateTime.MinVlue
  Public Property EndDate() As DateTime
    Get
      Return mEndDate 
    End Get
    Set(ByVal value As DateTime)
      mEndDate = value
      'What to do here?
    End Set
  End Property

  Private mEstimatedHours As Double = 0.0
  Public Property EstimatedHours() As Double 
    Get
      Return mEstimatedHours 
    End Get
    Set(ByVal value As Double)
      mEstimatedHours = value
      'What to do here?
    End Set
  End Property

  Private mStartDate As DateTime = DateTime.MinVlue
  Public Property StartDate() As DateTime
    Get
      Return mStartDate 
    End Get
    Set(ByVal value As DateTime)
      mStartDate = value
      'What to do here?
    End Set
  End Property

End Class
like image 880
Brandon Montgomery Avatar asked Jan 23 '23 17:01

Brandon Montgomery


1 Answers

The right approach to solve this problem will be to use Observer Design Pattern. Detailed explaination of implementing Observer pattern is beyond the scope of this discussion. But here are some great links for Observer Pattern. One link is here and another is here.

http://www.dofactory.com/Patterns/PatternObserver.aspx

http://en.wikipedia.org/wiki/Observer_pattern

like image 132
this. __curious_geek Avatar answered Jan 31 '23 14:01

this. __curious_geek