Re: OO Principle - reference needed
- From: "S. A. Gnezdov" <S.Gnezdov@xxxxxxxxx>
- Date: Wed, 18 Apr 2007 19:08:51 -0700
"Leslie Sanford" <jabberdabber@xxxxxxxxxxxxxxxxx> wrote in message news:4626c0bd$0$17165$4c368faf@xxxxxxxxxxxxxxxxx
I have a question about setting up collaborations. Let me give a trivial
example. Say an object uses a timer. It doesn't create the timer itself,
rather the timer is passed to it when it is created. The object that
uses the timer stops and starts the timer as it pleases and consumes
"tick" events generated by the timer. My question is whose
responsibility is it to ensure the integrity of the relationship between
the two object?
To continue with the example, the consumer, i.e. the object that uses
the timer, makes certain assumptions. It assumes that when it starts the
timer, it's not already running. And when it stops the timer no one else
will start it again. In other words, it's in control of the timer.
Is it whoever set up the collaboration responsibility to make sure that
these assumptions are met? How far should the consumer go in making sure
that its assumptions are true, e.g. that the timer is running when it
should be or not?
The answer depends on your high level usage scenario.
If you want to use a timer at a global application level, then it would be difficult to ensure that separate parts of the system main state correctly. In this case timer object should maintain its state. In other words if a timer instance is in a running state, then I'd expect that timer object would do nothing if StartTimer() call is executed. The same is true if timer is in stopped state and StopTimer() is called.
If you need to time some local operation, then state management in not an issue. You'd create a timer instance right when you need it, start it and then stop it.
State safe implementation (first example) can be used for both scenarios and that's how timers tend to be implemented.
You can learn from existing examples. Take a look at .NET System.Diagnostics.StopWatch and System.Diagnostics.PerformanceCounter classes.
.
- References:
- OO Principle - reference needed
- From: Nathan
- Re: OO Principle - reference needed
- From: H. S. Lahman
- Re: OO Principle - reference needed
- From: Leslie Sanford
- OO Principle - reference needed
- Prev by Date: Re: OO Principle - reference needed
- Next by Date: Re: OO-Inquisition
- Previous by thread: Re: OO Principle - reference needed
- Next by thread: Re: OO Principle - reference needed
- Index(es):
Relevant Pages
|