Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric base class wraps nested generic class to reduce type argument specification: Is there a name for this pattern?
    primarykey
    data
    text
    <p>Ok question title is far from being self-explanatory. I see myself doing this often:</p> <p>From <a href="https://stackoverflow.com/questions/98033/wrap-a-delegate-in-an-iequalitycomparer">this answer</a>:</p> <pre><code>public static class Equality&lt;T&gt; { public static IEqualityComparer&lt;T&gt; CreateComparer&lt;K&gt;(Func&lt;T, K&gt; keySelector) { return new KeyEqualityComparer&lt;K&gt;(keySelector); } class KeyEqualityComparer&lt;K&gt; : IEqualityComparer&lt;T&gt; { readonly Func&lt;T, K&gt; keySelector; public KeyEqualityComparer(Func&lt;T, K&gt; keySelector) { this.keySelector = keySelector; } public bool Equals(T x, T y) { ---- } public int GetHashCode(T obj) { .... } } } </code></pre> <p>What did I do: There is an implementation detail <code>KeyEqualityComparer&lt;T, K&gt;</code> which I had to call: </p> <pre><code>new KeyEqualityComparer&lt;Person, int&gt;(p =&gt; p.ID); </code></pre> <p>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:</p> <pre><code>Equality&lt;Person&gt;.CreateComparer(p =&gt; p.ID); </code></pre> <p>Note here that I haven't inherited nested class from the parent class (which is static). </p> <p>Or sometimes I see myself <a href="https://codereview.stackexchange.com/questions/25131/generic-wrapper-for-equality-and-hash-implementation-is-there-a-way-i-can-make">doing this</a>:</p> <pre><code>public abstract class Equater&lt;T&gt; : IEqualityComparer&lt;T&gt; { public static Equater&lt;T&gt; Create&lt;TKey&gt;(Func&lt;T, TKey&gt; keySelector) { return new Impl&lt;TKey&gt;(keySelector); } public abstract bool Equals(T x, T y); public abstract int GetHashCode(T obj); class Impl&lt;TKey&gt; : Equater&lt;T&gt; { readonly Func&lt;T, TKey&gt; keySelector; public Impl(Func&lt;T, TKey&gt; keySelector) { this.keySelector = keySelector; } public override bool Equals(T x, T y) { ---- } public override int GetHashCode(T obj) { .... } } } </code></pre> <p>Another similar one <a href="https://stackoverflow.com/a/16082916/661933">here</a></p> <pre><code>public class Accessor&lt;S&gt; { public static Accessor&lt;S, T&gt; Create&lt;T&gt;(Expression&lt;Func&lt;S, T&gt;&gt; memberSelector) { return new GetterSetter&lt;T&gt;(memberSelector); } class GetterSetter&lt;T&gt; : Accessor&lt;S, T&gt; { public GetterSetter(Expression&lt;Func&lt;S, T&gt;&gt; memberSelector) : base(memberSelector) { } } } public class Accessor&lt;S, T&gt; : Accessor&lt;S&gt; { Func&lt;S, T&gt; Getter; Action&lt;S, T&gt; 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&lt;Func&lt;S, T&gt;&gt; memberSelector) //access not given to outside world { ---- } } </code></pre> <p>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:</p> <pre><code>List&lt;Equater&lt;Person&gt;&gt; { persons with different implementations }; </code></pre> <p>Its helping me from time to time. <strong>So I'm curious to know if there is a name for this pattern?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload