INeedSomething 的更好名字...?

发布于 2024-11-06 05:17:44 字数 270 浏览 1 评论 0原文

我试图为一个接口选择一个名称,通过它我可以将一些东西传递给一个对象,例如:

  • 如果该对象是 INeedX 那么 SetX(some x) 方法将被调用;
  • 如果对象是 INeedY 则 SetY(some y) 方法将被调用;
  • 等等。 (很有趣,我知道:))

我尝试为这种接口找到一个不同的名称,但我无法弄清楚。

有人知道如何命名INeedSomething接口吗?

I was trying pick a name for an interface through which I can pass something into an object like:

  • If the object is INeedX then SetX(some x) method will be called;
  • If the object is INeedY then SetY(some y) method will be called;
  • and so on. (funny as hell, I know :) )

I try to find a different name for this kind of interfaces but I can't figure this out.

Does anybody have an idea how to name the INeedSomething interface ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

倾城°AllureLove 2024-11-13 05:17:44

您的界面的名称 IDependsOn 怎么样(又名:IDependsOnUserId、IDependsOnTimeOut)

What about the name IDependsOn for your interface (aka. IDependsOnUserId, IDependsOnTimeOut)

灯角 2024-11-13 05:17:44

嗯,INeed 界面可能表明设计很尴尬。在我看来,您真正寻找的是依赖注入,这相当于声明一个对象需要另一个对象才能运行的代码。因此

class Car:INeed<Engine> {
    public Set(Engine engine)
}

,也许您真正想要的是

class Car {
    public Car(Engine engine)
}

如果我是您,我会尝试看看上述内容是否适用于您的情况,这是声明需求/依赖项的更标准的方式

Well, the INeed interface is probably an indication of an awkward design. It seems to me that what you are really looking for is dependency injection, which is the code equivalent of stating that an object requires another object to function. So instead of having

class Car:INeed<Engine> {
    public Set(Engine engine)
}

, maybe what you really want is

class Car {
    public Car(Engine engine)
}

If I were you I would try to see if the above is applicable in your case, which is a more standard way of declaring needs/dependencies

筱武穆 2024-11-13 05:17:44
public interface INeed<T>
{
   public T SetValue { set; }
}

通过使用泛型,接口传递的类型稍后定义,如下所示:

class Needy : INeed<int>
{
   private int internalValue;
   public int SetValue { set {
     internalValue = value;
   } };
}
public interface INeed<T>
{
   public T SetValue { set; }
}

by using generics, the type passed by your interface is defined later, like this:

class Needy : INeed<int>
{
   private int internalValue;
   public int SetValue { set {
     internalValue = value;
   } };
}
风流物 2024-11-13 05:17:44

IRequire 听起来更好,但是,为此使用接口似乎是错误的。

使用属性代替:

class MyClass
{
    [Required]
    double X
    {
         get;
         set;
    }
}

IRequire sounds better, however, the use of the interface for this seems wrong.

Use attributes instead:

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