如果您使用基于界面的设计方法,您如何命名更多行为界面?

发布于 2024-09-08 01:37:56 字数 355 浏览 2 评论 0原文

我正在开发一个包含多个控件(或组件)的库。我使用基于界面的方法进行类型设计,但最终它导致我遇到了一个困扰我很长时间的问题。如果您使用基于接口的设计方法,如何命名行为接口?例如,假设您有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里兽 2024-09-15 01:37:56

将接口命名为正确描述实现它的任何类的单词或短语。 “HasText”是一个很好的例子,因为它描述了实现者(它有一个 Text 属性)。

使其特定于界面。 “IsAnObject”或“MightHaveData”或“ExistsInCyber​​space”不是特定于该接口的。

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.

草莓味的萝莉 2024-09-15 01:37:56

确定您需要不同的行为界面吗?实现一个行为接口和几个具体行为还不够吗?

举个例子,假设我们有一个真正的野生动物的实现,比如一个带有一些传感器和大量行为的 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!!". The Cat will query it's Collection of Behaviour instances if there's a fit, iaw, if any of the stored Behaviour instances likes to take action. Assume we have an instance of class HuntMouse implements Behaviour stored in the list and this is the best fit, then the cat would call action() on that Behaviour and hunt the mouse.

Back to your question - I think, one Behaviour interface with 2-3 methods would be enough, concrete Behaviour objects do not need a prefix or suffix. And if needed, I suggest -Behaviour (like HuntMouseBehaviour)


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 while hasText is more suiteable. Implementing the interface adds the provide 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 ;))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文