c# - Generic base class wraps nested generic class to reduce type argument specification: Is there a name for this pattern? -
ok question title far being self-explanatory. see myself doing often:
from this answer:
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) { .... } } }
what did do: there implementation detail keyequalitycomparer<t, k>
had call:
new keyequalitycomparer<person, int>(p => p.id);
by nesting private class, not did hide implementation (the public constructor of internal class obscure now), got better syntax:
equality<person>.createcomparer(p => p.id);
note here haven't inherited nested class parent class (which static).
or 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) { .... } } }
another similar 1 here
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] { { if (!isreadable) throw new argumentexception("property 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 outside world { ---- } }
note in these 2 cases inherited wrapping class. not did benefits of former can maintain list this:
list<equater<person>> { persons different implementations };
its helping me time time. so i'm curious know if there name pattern?
i think lot class clusters pattern based on abstract factory pattern.
Comments
Post a Comment