c# factory of classes implementing the same interface -


i'd create factory creates objects using text parameters. classes need created not derive single base class implement same interface.

public class square: iprintable { ... }  public class triangle: iprintable { ... }  interface iprintable {    void print(); } 

i able create objects in following way:

printersettings settings = new printersettings("something"); shapefactory.instance().create("triangle", settings); 

i thought of creating factory contains map handle string ids in following way:

public class shapefactory {     static shapefactory singleton;     dictionary<string, xxx > map;      private shapefactory(){};      public static shapefactory instance()     {        ...        return singleton;     }      public void register(string id, xxx obj);      public iprintable create(string id, printersettings obj); }  public squarefactory {...}  public trianglefactory {...} 

i struggling find class use values of map make work. please let me know if there serious design issues above.

it considered code smell pass type information string in strongly-typed language c#. often, you'd see written as:

shapefactory.instance().create(typeof(triangle), settings); 

or simply

shapefactory.instance().create<triangle>(settings); 

what asking accomplished, better pass type information along rather looking type when came time instantiate object. if need create type class name, @ activator.createinstance see how that. can non-trivial resolve types, may want read on reflection well, see how type resolution accomplished framework.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -