Re: OO Principle - reference needed



"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.

.



Relevant Pages

  • Re: OO Principle - reference needed
    ... Say an object uses a timer. ... continued example below is an expansion, and that, "Integrity," means basically, "Who makes sure that both objects make the correct assumptions about one another?" ... Software being what it is, anyone could create a Timer object to be, "Not running on instantiation," and a consumer object to, "Assume that newly-instantiated Timer objects are not running." ... www.EdmundKirwan.com - Home of The Fractal Class Composition. ...
    (comp.object)
  • Re: curiosity about java.util.Timer
    ... I'll fix the problem by instantiating a new Timer instance when I catch the IllegalState error. ... I'm curious why the timer would cancel. ... If the server is not creating many medium lived objects, then there is no reason for a major collection. ...
    (comp.lang.java.programmer)
  • Re: curiosity about java.util.Timer
    ... I'll fix the problem by instantiating a new Timer instance when I catch the IllegalState error. ... I'm curious why the timer would cancel. ... also ends the timer when the thread got an Exception/Error. ...
    (comp.lang.java.programmer)
  • Re: Does timer interval gets stored anywhere in system or in registry.
    ... A possible workaround might be to dispose the Timer instance every day or so and create a new instance to work with. ... My question is if i am using timer in my windows service, ... So i want to know where in system timer related information is stored. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: curiosity about java.util.Timer
    ... I'll fix the problem by instantiating a new Timer instance when I catch the IllegalState error. ... I'm curious why the timer would cancel. ...
    (comp.lang.java.programmer)