开发者

How do I modelize my two classes knowing one needs the method of the other?

alt text

I have these two classes 开发者_如何学Goand the class Retrodoc needs to know the versionPath() to execute its method run($versionId). So what is the best modelization? Do I instanciate Version in the method and then I can use the method getVersionPath()?


If the version path will remain static throughout the lifetime of the dependent object, then create a member variable in Retrodoc and initialize it by instantiating a version object and calling the getter in the constructor. Then the object will be destroyed when the constructor goes out of scope, you have the value handy within your object and you make only one function call plus one call to __construct and one to __destruct. The version path is stored in a single primitive type.

If the path is something that will change from one call of run to another then you would make the version object the class member. then call the getter each time you run the function, but you do not create and destroy it each time. Still only one call to the constructor and one to the destructor plus one call to the getter for each call.

This is a has-a type of relationship From a UML design point of view where the Retrodoc object has-a version. This is called composition. Try to choose the design that is most efficient with function calls having a time cost (including construction and destruction) and objects having a higher memory cost than primitives.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜