这是获得抽象静态方法功能的唯一方法吗?

时间:2017-05-17 11:26:11

标签: c# abstract-class static-methods abstract

我正在研究一个框架,其座右铭是“无冗余”和“我不想知道供应商的具体细节”,因此大多数事情都是通过Interfaces和Generic类来处理的。现在我的情况是,我有一个抽象类,想要根据它自己的Enum变量 se 来匹配事物,但它不应该知道供应商如何提供相关变量与 se 相匹配。供应商可能已经决定了一个整数,一个Enum或一个字符串最好保存这些信息但老实说我不想知道。 所以我认为没有问题有一个抽象静态方法必须由包装器的每个实现提供,以比较se与供应商保存该信息的特定方式。

//The original version I wanted to be possible
public abstract class AbstractGenericClass<TWrapper<T>, T> where TWrapper : AbstractGenericWrapper<T> {
    protected TWrapper tWrapper;
    //our SomeEnum se is somehow relatable to every T
    //but we don't want to know how
    protected SomeEnum se = ...;

    //called on Start
    public void Start() {
        List<T> ts = FindObjectsOfType<T>;
        foreach (T t in ts) {
            if(T.Compare(t, this.se)) {
                tWrapper = new TWrapper(t);
            }
        }
    }
}

public abstract class AbstractGenericWrapper<T> {
    T _t;

    public AbstractGenericWrapper(T t) {
        _t = t;
    }

    public static abstract bool Compare(T t, SomeEnum someEnum);
}

public class ConcreteNongenericWrapper : AbstractGenericWrapper<VendorSpecificImplementation> {
    public static bool Compare(VendorSpecificImplementation t, SomeEnum someEnum) {
        return t.vendorVariable.toLower().Equals(Enum.GetValues(typeof(someEnum), someEnum));
    }
}

public class OtherConcreteNongenericWrapper : AbstractGenericWrapper<OtherVendorSpecificImplementation> {
    public static bool Compare(OtherVendorSpecificImplementation t, SomeEnum someEnum) {
        return t.otherVendorVariable % 3 == (int) someEnum;
    }
}

public class SomeImplementation {
    public static void main() {
        AbstractGenericClass<ConcreteNongenericWrapper<ConcreteNongeneric>, ConcreteNongeneric> foo 
            = new AbstractGenericClass<ConcreteNongenericWrapper<ConcreteNongeneric>, ConcreteNongeneric>();
        AbstractGenericClass<OtherConcreteNongenericWrapper<OtherConcreteNongeneric>, OtherConcreteNongeneric> bar
            = new AbstractGenericClass<OtherConcreteNongenericWrapper<OtherConcreteNongeneric>, OtherConcreteNongeneric>();

        foo.Start();
        bar.Start();
    }
}

我发现这是不可能的,我想知道下面的这个版本是否是最佳/唯一的方式吗?它有冗余,我不喜欢它,它更长。

//An attempt at a solution:
public abstract class AbstractGenericClass<TWrapper<T>, T> where TWrapper : AbstractGenericWrapper<T> {
    protected TWrapper tWrapper;
    //our SomeEnum se is somehow relatable to every T
    //but we don't want to know how
    protected SomeEnum se = ...;

    //called on start
    public abstract void Start();
}

public abstract class AbstractGenericWrapper<T> {
    T _t;

    public AbstractGenericWrapper(T t) {
        _t = t;
    }
}

public class ConcreteNongenericClass : AbstractGenericClass<VendorSpecificImplementation> {
    //called on start
    public override void Start() {
        List<VendorSpecificImplementation> ts = FindObjectsOfType<VendorSpecificImplementation>;
        foreach (VendorSpecificImplementation t in ts) {
            if(t.vendorVariable.toLower().Equals(Enum.GetValues(typeof(someEnum), someEnum))) {
                tWrapper = new ConcreteNongenericWrapper(t);
            }
        }
    }
}

public class ConcreteNongenericWrapper : AbstractGenericWrapper<VendorSpecificImplementation> {
}


public class OtherConcreteNongenericClass : AbstractGenericClass<OtherVendorSpecificImplementation> {
    //called on start
    public void Start() {
        List<OtherVendorSpecificImplementation> ts = FindObjectsOfType<OtherVendorSpecificImplementation>;
        foreach (OtherVendorSpecificImplementation t in ts) {
            if(t.otherVendorVariable % 3 == (int) someEnum) {
                tWrapper = new OtherConcreteNongenericWrapper(t);
            }
        }
    }
}

public class OtherConcreteNongenericWrapper : AbstractGenericWrapper<OtherVendorSpecificImplementation> {
}

public class SomeImplementation {
    public static void main() {
        AbstractGenericClass<ConcreteNongenericWrapper<ConcreteNongeneric>, ConcreteNongeneric> foo 
            = new AbstractGenericClass<ConcreteNongenericWrapper<ConcreteNongeneric>, ConcreteNongeneric>();
        AbstractGenericClass<OtherConcreteNongenericWrapper<OtherConcreteNongeneric>, OtherConcreteNongeneric> bar
            = new AbstractGenericClass<OtherConcreteNongenericWrapper<OtherConcreteNongeneric>, OtherConcreteNongeneric>();

        foo.Start();
        bar.Start();
    }
}

非常感谢您的时间和帮助!

0 个答案:

没有答案