本文介绍了通用基础类包装嵌套泛型类,以减少类型参数规格:是否有一个名字为这种模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定问题的标题是远远不言自明的。我看到自己这样做往往:

Ok question title is far from being self-explanatory. I see myself doing this often:

从的:

public static class Equality<T>
{
    public static IEqualityComparer<T> CreateComparer<K>(Func<T, K> keySelector)
    {
        return new KeyEqualityComparer<K>(keySelector);
    }



    class KeyEqualityComparer<K> : IEqualityComparer<T>
    {
        readonly Func<T, K> keySelector;

        public KeyEqualityComparer(Func<T, K> keySelector)
        {    
            this.keySelector = keySelector;
        }

        public bool Equals(T x, T y)
        {
            ----
        }

        public int GetHashCode(T obj)
        {
            ....
        }
    }
}

我做了什么:有一个实现细节 KeyEqualityComparer< T,K> ,我只好打电话:

What did I do: There is an implementation detail KeyEqualityComparer<T, K> which I had to call:

new KeyEqualityComparer<Person, int>(p => p.ID);



通过嵌套它作为一个私有类,我不仅隐藏实现(内部公共构造类现在是晦涩难懂),但有一个更好的语法:

By nesting it as a private class, not only did I hide the implementation (the public constructor of internal class is obscure now), but got a better syntax:

Equality<Person>.CreateComparer(p => p.ID);

请注意,我没有继承嵌套类的父类(这是静态的)。

Note here that I haven't inherited nested class from the parent class (which is static).

或者有时候我看到自己的:

Or sometimes I see myself doing this:

public abstract class Equater<T> : IEqualityComparer<T>
{
    public static Equater<T> Create<TKey>(Func<T, TKey> keySelector)
    {
        return new Impl<TKey>(keySelector);
    }

    public abstract bool Equals(T x, T y);

    public abstract int GetHashCode(T obj);



    class Impl<TKey> : Equater<T>
    {
        readonly Func<T, TKey> keySelector;

        public Impl(Func<T, TKey> keySelector)
        {
            this.keySelector = keySelector;
        }

        public override bool Equals(T x, T y)
        {
            ----
        }

        public override int GetHashCode(T obj)
        {
            ....
        }
    }
}

另一个类似的1

public class Accessor<S>
{
    public static Accessor<S, T> Create<T>(Expression<Func<S, T>> memberSelector)
    {
        return new GetterSetter<T>(memberSelector);
    }

    class GetterSetter<T> : Accessor<S, T>
    {
        public GetterSetter(Expression<Func<S, T>> memberSelector) : base(memberSelector)
        {

        }
    }
}

public class Accessor<S, T> : Accessor<S>
{
    Func<S, T> Getter;
    Action<S, T> Setter;

    public bool IsReadable { get; private set; }
    public bool IsWritable { get; private set; }
    public T this[S instance]
    {
        get
        {
            if (!IsReadable)
                throw new ArgumentException("Property get method not found.");

            return Getter(instance);
        }
        set
        {
            if (!IsWritable)
                throw new ArgumentException("Property set method not found.");

            Setter(instance, value);
        }
    }

    protected Accessor(Expression<Func<S, T>> memberSelector) //access not given to outside world
    {
        ----
    }

}

请注意,在这两种情况下,我从包裹类继承。所以,现在我不仅得到了前者的好处,但我也能保持这样的列表:

Note that in these two cases I inherited from wrapping class. So now not only did I get the benefits of the former but I can also maintain a list like this:

List<Equater<Person>> { persons with different implementations };



它帮助我不时地。 所以我很好奇,想知道是否有这种模式的一个名字?

推荐答案

作为正确由伯蒂指出,我可能没有遵循的任何一个的模式在这里。

As rightly pointed out by Bertie, I might have not followed any one pattern here.

我要说的是,通过委派具体落实到 CreateComparer 方法,我刚才已经简化了创建对象 - 这涉及下的的。通过为对象实例化一个静态函数,这是形式的的工厂模式的 - 专门的的这种变异。

I would say that by delegating the instantiation of concrete implementation to CreateComparer method, I have just simplified the object creation - which comes under Creation Method patterns. By having a static function for object instantiation, it was sort of a factory pattern - specifically this variant of Factory Method.

通过继承默认地将Impl Equater地我有几分跟着 - 其中 Equater地是工厂的厂房和默认地将Impl 终止其执行给默认地将Impl 本身回来,除了默认地将Impl 真是没有创造任何其他对象(即默认地将Impl 并不意味着是一个工厂),但是的使用实例化本身通过构造。因此,严格意义上来说,它不是抽象工厂模式,但它会更接近,如果默认地将Impl 可以调用它本身的构造,并返回一个实例的方法。

By inheriting Impl from Equater I have sort of followed the Abstract Factory pattern - where Equater is factory of factory and Impl is its implementation to give Impl itself back, except that Impl is really not creating any other object (in other words Impl is not meant to be a factory), but getting instantiated itself via constructor. So in strict sense, its not Abstract Factory pattern, but it will be closer to if Impl can have a method to call the constructor of itself and return back an instance.

另外通过嵌入和隐藏实现细节(嵌套类作为这样),暴露的类(父类)假货到外界,就好像它是做它的工作。这就是被称为

Also by embedding and hiding the implementation detail (the nested classes as such), the exposed class (parent class) fakes to outside world as if it is doing it's job. It's what is called Delegation Pattern

至于一个名字的隐藏实现泛型类的嵌套泛型类给予更容易签名方法调用的来讲,我不认为存在之一。即使任何名称存在,它必须是非常具体以来的语言结构像泛型/类型推理等所涉及的语言(或类似语言)。埃里克利珀这里发现在嵌套类一样使用,虽然它不是仿制药有关,在那里,他将其称为工厂模式。

As far as a name for hiding implementation of a generic class in a nested generic class to give easier signature for method calls is concerned I don't think there exist one. Even if any name existed, it has to be very specific to the language (or similar languages) since language constructs like generics/type inference etc are involved. Eric Lippert finds the same use in nested classes here although its not generics related, where he calls it Factory pattern.

这篇关于通用基础类包装嵌套泛型类,以减少类型参数规格:是否有一个名字为这种模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!