开发者

A better name for INeedSomething...?

开发者 https://www.devze.com 2023-03-04 18:13 出处:网络
I was trying pick a name for an interfa开发者_高级运维ce through which I can pass something into an object like:

I was trying pick a name for an interfa开发者_高级运维ce 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 ?


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


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


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;
   } };
}


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

Use attributes instead:

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

精彩评论

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

关注公众号