How can I hide critical methods like Update() but reveal necessary details?
I'm working on my game framework and I have, for argument sakes, a TimeService that is responsible for calculating the frame / delta time and the total time the app has been running. TimeService derives from Service which has an Update() method. What's the best way of revealing the TimeService's data like DeltaTime and TotalTime but hiding the Up开发者_如何学编程date() method to make sure nothing using the DeltaTime or TotalTime can accidentally call Update() and throw all the timings off? The ServiceManager calls the Update() method once a frame so that's why it's exposed.
Ideas?
I would use interfaces. Basically you have one for things that need to know the time and another for things that need to control the time.
A clean way of doing this is to use the facade pattern. Define a class that only declares the methods you want exposed (DeltaTime, TotalTime, etc.). Implement it to relay those calls to a (hidden) TimeService object with which it is associated.
I agree with @stonemetal on interfaces. In C# for instance, you can explicitly implement interfaces so that interface methods are only visible through an interface reference.
Another option is to reduce the visibility of the Update method (make it internal, friend, etc.) with access modifiers.
Much of this depends on your programming language; it's difficult to give a language-agnostic answer.
精彩评论