c# - What passable value can I use, besides an object instance, to access multiple functions? -
i'm learning c# , i'm looking solution problem perhaps caused bad architecture , doesn't affect real-life programs.
i want pass "something" (not class) holds multiple functions , doesn't need instantiated, example, want have class list of hours , tasks in dictionary, so, example, @ 12:00 want class 'lunch', lunch may depend on other variables, have dict entry check {12, lunchtask}, lunchtask subclass/implementation/derivation of 'task' can safely pass , call sometask.start, sometask.pause, sometask.stop.
i though using dictionary (int,system.type) couldn't working, tried statics can't subclassed , delegates single functions far know. want pass in dict has functions can accessed directly without instantiating. 1 solution know work find inelegant have static class instances of different tasks.
i don't know of better way achieve such basic functionality , perhaps i'm doing terribly wrong. if guys point me in right direction i'd grateful. thank in advance.
here (pseudo-c#) code:
public abstract class task {     public abstract void executetask ();     public virtual void pausetask() {console.writeline ("task paused")}     public virtual void stoptask() {console.writeline ("task stopped")} }  public class lunch : task {     public override void executetask ()     {         console.writeline ("lunch task started");     } }  //the following gonna instantiated public class human {     dictionary<int, something> attributions =  new dictionary<int, something>(){{12, lunch}};     void tobecalledeveryhour () {         int hour = somehour();         if (attributions.containskey(hour))             attributions[hour].executetask();     } }      
in case can use either object pool pattern or factory pattern.
in case of object pool pattern can have map in key should string identify object uniquely, example can class name , value can corresponding object.
like this:
public abstract class task { public abstract void executetask (); public virtual void pausetask() {console.writeline ("task paused")} public virtual void stoptask() {console.writeline ("task stopped")} }  public class lunch : task {    public override void executetask ()    {      console.writeline ("lunch task started");    } }  public class objectcollection{     dictionary<string,task> objectstringmapper= new dictionary<string,string>();     dictionary<string,task> objecttimemapper= new dictionary<string,string>();     public objectcollection(){         objectmapper.add("lunch",new lunchtask());         objecttimemapper.add(12,new lunchtask());             }     public task getobject(string objid){         return objectmapper.get(objid);     }     public task getobject(int time){         return objecttimemapper.get(time);     }    }  public class human {     objetcollection objectsfactory = new objectcollection();     void tobecalledeveryhour () {         int hour = somehour();         if (attributions.containskey(hour))             objectsfactory.getobject(hour).executetask();       } }   or can opt factory pattern , in can have class creating objects using either reflection or switch case.
note: since i'm java developer , new c# may find syntax's wrong.
Comments
Post a Comment