Generic class with multiple return types -
i know question not asked rather hard show example: have class abstracthandler. create method class:
class abstractmanager { binaryserializator binaryserializator; //just example textserializator textserializator; public abstractmanager(binaryserializator binaryserializator) { this.binaryserializator = binaryserializator; this.textserializator = null; } public abstractmanager(textserializator textserializator) { this.textserializator = textserializator; this.binaryserializator = null; } public abstracthandler createhandler() { if (this.binaryserializator != null) { return new abstracthandler<byte[]>(this.binaryserializator); } else { return new abstracthandler<string>(this.textserializator); } } }
so how create abstracthandler again has 2 constructors (one takes binaryserializator , textserializator). here fun part: both serializators have method getserializedtype(object object), binary returns byte[] text returns string. want method in abstracthandler return either byte[] or string depending on type of serializator have:
public t createserializable(object object) { if (binaryserializer != null) { return (t) binaryserializer.getserializedtype(object); } if (textserializer != null) { return (t) textserializer.getserializedtype(object); } }
my question - way it? there workaround 2 constructors , how supposed deal 2 different abstracthandlers, know it's possible done generics instead of havign 2 classes (one string, 1 byte[]). taking time read long post.
i have 2 solutions:
interfaces:
interface iserializatorstategy { object getserializedtype(object obj); } // if want generic classes interface iserializatorstategy<t> : iserializatorstategy { new t getserializedtype(object obj); }
serializators:
// if don't want generic classes // can inherit base interface , implement method class textserializator : iserializatorstategy<string> { public string getserializedtype(object obj) { return "hello world"; } object iserializatorstategy.getserializedtype(object obj) { return getserializedtype(obj); } } class intserializator : iserializatorstategy<int> { public int getserializedtype(object obj) { return 42; } object iserializatorstategy.getserializedtype(object obj) { return getserializedtype(obj); } }
managers:
class abstractmanager { protected iserializatorstategy serializatorstrategy { get; set; } public abstractmanager(iserializatorstategy serializatorstrategy) { serializatorstrategy = serializatorstrategy; } public abstracthandler createhandler() { return new abstracthandler(serializatorstrategy); } } // if want generic classes class abstractmanager<t> : abstractmanager { // note, if allow constructor, // encounter runtime errors public abstractmanager(iserializatorstategy serializatorstrategy) : base(serializatorstrategy) { } public abstractmanager(iserializatorstategy<t> serializatorstrategy) : base(serializatorstrategy) { } public new abstracthandler<t> createhandler() { return new abstracthandler<t>(serializatorstrategy); } }
handlers:
class abstracthandler { private readonly iserializatorstategy _serializatorstrategy; public abstracthandler(iserializatorstategy serializatorstrategy) { _serializatorstrategy = serializatorstrategy; } public object createserializable(object obj) { return _serializatorstrategy.getserializedtype(obj); } public void getserializable<t>(object obj, out t serializebleobject) { serializebleobject = (t)_serializatorstrategy.getserializedtype(obj); } } // if want generic classes class abstracthandler<t> : abstracthandler { // note, if allow constructor, // encounter runtime errors public abstracthandler(iserializatorstategy serializatorstrategy) : base(serializatorstrategy) { } public abstracthandler(iserializatorstategy<t> serializatorstrategy) : base(serializatorstrategy) { } public new t createserializable(object obj) { return (t)base.createserializable(obj); } }
usage:
generic classes
class program { static void main(string[] args) { var textmanager = new abstractmanager<string>(new textserializator()); var texthandler = textmanager.createhandler(); string textresult = texthandler.createserializable(null); // or abstracthandler<string> type console.writeline(textresult); var intmanager = new abstractmanager<int>(new intserializator()); var inthandler = intmanager.createhandler(); // or abstracthandler<int> type int intresult = inthandler.createserializable(null); console.writeline(intresult); console.readkey(); } }
generic methods
class program { static void main(string[] args) { abstractmanager manager = new abstractmanager(new textserializator()); abstracthandler handler = manager.createhandler(); string textresult; handler.getserializable(null, out textresult); console.writeline(textresult); manager = new abstractmanager(new intserializator()); handler = manager.createhandler(); int intresult; handler.getserializable(null, out intresult); console.writeline(intresult); console.readkey(); } }
now, shouldn't have troubles apply on issue.
Comments
Post a Comment