如果您使用基于界面的设计方法,您如何命名更多行为界面?
我正在开发一个包含多个控件(或组件)的库。我使用基于界面的方法进行类型设计,但最终它导致我遇到了一个困扰我很长时间的问题。如果您使用基于接口的设计方法,如何命名行为接口?例如,假设您有一些 getter setter 函数,并且它们被许多接口使用,并且它们提供的功能不能用“-able”后缀命名;你会做什么,或者你会做什么?谢谢...
编辑:例如,我创建了一个这样的接口:
public interface HasText {
public String getText();
public void setText(String text);
}
大多数使用此函数的接口没有通用的超类型。
I'm developing a library containing multiple controls (or components). I'm using an interface based approach to type design, but eventually it led me to a question that bothers me for a long time. If you use an interface-based design approach, how do yo name the behaviour interfaces? For example, let's assume you have some getter setter functions and they're used by many interfaces, and functionality they provide is cannot be named with a "-able" postfix; what would you do, or do you do? Thanks...
Edit: For example i created an interface like this:
public interface HasText {
public String getText();
public void setText(String text);
}
most interfaces that use this functions has no common super type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将接口命名为正确描述实现它的任何类的单词或短语。 “HasText”是一个很好的例子,因为它描述了实现者(它有一个 Text 属性)。
使其特定于界面。 “IsAnObject”或“MightHaveData”或“ExistsInCyberspace”不是特定于该接口的。
Name the interface as a word or phrase correctly describing any class that implements it. "HasText" is a fine example as it describes the implementor (it has a Text property).
Make it specific to the interface. "IsAnObject" or "MightHaveData" or "ExistsInCyberspace" is not specific to the interface.
确定您需要不同的行为界面吗?实现一个行为接口和几个具体行为还不够吗?
举个例子,假设我们有一个真正的野生动物的实现,比如一个带有一些传感器和大量行为的
Cat
类。现在猫眼传感器报告“老鼠!!”。Cat
将查询其Behaviour
实例的Collection
是否存在合适的情况,iaw,如果有任何存储的Behaviour
实例喜欢采取行动。假设我们有一个class HuntMouse Implements Behaviour
的实例存储在列表中,并且这是最合适的,那么猫会对该Behaviour< 调用
action()
/code> 并捕捉鼠标。回到你的问题 - 我认为,一个具有 2-3 个方法的 Behaviour 接口就足够了,具体的 Behaviour 对象不需要前缀或后缀。如果需要,我建议
-Behaviour
(例如HuntMouseBehaviour
)最初您要求Behaviours,但您的
HasText
示例显示某种功能。在这种特殊情况下,TextProvider
可能是更好的选择,而hasText
更合适。实现该接口将提供一些文本
功能添加到类中。不是作为问题的答案,而是为了进一步阅读:行为设计模式列表。
(顺便说一句 - 我上面的例子是受到机器人领域的实际实现的启发 - 即使猫不是机器人;))
Sure that you need different behaviour interfaces? Wouldn't it be enough to implement one behaviour interface and a couple of concrete behaviours?
Just an example, assume we have an imlplementation of a real wild animal, say a class
Cat
with some sensors and a big catalog of behaviours. Now the cat's eye-sensor reports "mouse!!". TheCat
will query it'sCollection
ofBehaviour
instances if there's a fit, iaw, if any of the storedBehaviour
instances likes to take action. Assume we have an instance ofclass HuntMouse implements Behaviour
stored in the list and this is the best fit, then the cat would callaction()
on thatBehaviour
and hunt the mouse.Back to your question - I think, one
Behaviour
interface with 2-3 methods would be enough, concreteBehaviour
objects do not need a prefix or suffix. And if needed, I suggest-Behaviour
(likeHuntMouseBehaviour
)Initially you asked for Behaviours but your
HasText
example shows some kind of Feature. Where in this special case,TextProvider
could be an a better choice whilehasText
is more suiteable. Implementing the interface adds theprovide some text
feature to the class.Not as an answer to the question but for further reading: List of behavioural design patterns.
(BTW - my example above was inspired by real implementations from the robotics area - even though a cat is not a robot ;))