开发者

In OOP is Time a Property of An Object?

I searched in Google for an hour and can't find anything (maybe I'm searching for the wrong thing); I'm sure this has been discussed and explained somewhere but I don't know where that place is...

I don't understand the relationship between an Object and Time. I am deeply confused about this principle and how to apply it.

Let's say I am going to make a OOP Program. On this program, circles are going to appear over a 60 second range.

The circle has the following properties.

var circle1 = new Circle();

circle1.color = "yellow";
circle1.size = "100";
circle1.text = "hello";

The Time开发者_如何学Cline has the following properties:

var timeline1 = new Timeline();
timeline.currentSeconds = 0;

And methods:

timeline.start();
  1. Where is the best place to store the data of when that specific circle is going to appear and disappear? For example should it be circle1.startTime =1, circle1.endTime = 10... and store the times that the circle is visible and exists as properties of the circle?

  2. Is it best to add an EventListener to the object or to the Timeline? Should the Circle object have an event listener that listens for currentSeconds to update?

Basically I guess I am asking, what is the best way for these 2 objects to communicate with each other?


It's a design decision that will depend on the specifics of the application and the opinion of the developers.

  • If each circle 'owns' its own time period, and this is something that circles always have, I would put it with the circle.
  • If circles have other, more generic uses (such as being displayed, but not being animated), it may make sense to have a class which extends Circle, and put the time data in there.
  • If you want the timeline object to be completely responsible for animation data, you can put the data in there.
  • It may make sense to have an ObjectAnimation class which maintains reference to a single Circle, and provides the additional start and end-time information

I would tend to think that having the time-based display of the objects would be kept away from the objects themselves. Rather than having a time-event listener, Circle will likely have a renderCircle() function, which will be called conditionally based upon the time, and the circle's time properties (wherever they may be held).


The second one. Time is not a property of a thing; it is a property of the universe. But objects can be affected by and can react to the passage of time.. and are generally at least aware of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜