复合图案设计问题
我对示例复合类图中常见的两个操作有疑问。
* 获取后代
* GetChild(int)
一个常见的例子是文件和目录,我将继续使用它。假设感兴趣的操作是 Size,因此 File 有一个实际大小,而 Directory 有一个从 GetDescentents 的递归迭代导出的 Size。到目前为止,一切都很好。我的问题与客户对 GetDescentents 的使用有关。假设您需要目录中的文件作为某些给定操作的图像。因此,在实践中,您可以使用 GetDescendents 和 Children 的某种组合来返回 imageFiles(取决于客户端是否需要所有嵌套的 imageFiles 还是仅在根级别)。
所以第一个问题是,您是否可能在组合上使用 GetImageFiles 方法而不是让客户端弄清楚?假设是这样,GetDescentents 是否可以实际暴露给组合之外的客户端调用者(如 ImageViewer)?
第二个问题关于GetChild(int); int 是返回单个子项的序数位置索引吗? GetDescentents 的深度?客户如何使用该方法的示例是什么?
干杯,
贝里尔
I have a question about two operations you commonly see in an example Composite class diagram.
* GetDescendents
* GetChild(int)
A common example being Files and Directories, I'll stick with that. Let's say the operation of interest is Size, so File has a real size and Directory has a Size derived from the recursive iteration of GetDescendents. So far so good. My question has to do with the client's use of GetDescendents. Say you need the files in a directory that are images for some given operation. So in practice, you use some combination of GetDescendents and Children to return the imageFiles (depending on whether the client wanted all nested imageFiles or just at the root level).
So question number one is, wouldn't you likely have a GetImageFiles method on the composite as opposed to making the client figure it out? And assuming so, is GetDescendents ever practical to expose to client callers (like the ImageViewer) outside of the composition?
The second question about GetChild(int); is the int an ordinal position index to return a single child? A level of depth into GetDescendents? What would be an example of how a client would use that method?
Cheers,
Berryl
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些问题与复合模式本身无关,而是关于如何进行 API 设计以及如何明确传达作为类开发人员的意图的更大问题。
例如,如果您希望
GetChild(int)
提供索引的直接子级,您可以将其命名为GetChildAtIndex(int index)
;如果您希望它为层次结构中的某个级别提供子级,您可以将其命名为GetChildrenAtLevel(int level)
(请注意,这是复数形式并返回一个集合)。作为类设计者,您有责任公开足够的操作以使您的类易于理解和使用。如果您认为获取目录结构上的图像文件是一个非常常见的操作,则可以公开
GetAllImageFiles()
方法。但对于更通用的目录类,这种选择似乎是任意的,并且处于错误的抽象级别。为什么图像文件如此特殊?相反,您可以提供一种更通用的方法,该方法将根据扩展名获取所有文件,或者提供一种采用谓词根据客户端提供的条件过滤结果的方法。These questions are not about the composite pattern per se, but about the bigger issue of how you do API design and communicate unambiguously your intent as a class developer.
For example, if you want your
GetChild(int)
to give the indexed immediate child, you could name itGetChildAtIndex(int index)
; if you want it to give the children at a certain level in the hierarchy, you could name itGetChildrenAtLevel(int level)
(note that this is plural and returns a collection).It's up to you as a class designer to expose enough operations as to make your class understandable and usable. If you think that a very common operation would be to get the image files on your directory structure, you could expose a
GetAllImageFiles()
method. For a more general directory class though, this choice seems arbitrary and it's at the wrong level of abstraction. Why are image files so special? You could, instead, provide a more general method that would get all files based on their extensions or a method that takes a predicate to filter the results based on client provided criteria.