c# - Hiding implementational detail from parent classes -


suppose i'm designing robot can pickup various tools , use them. create robot class has pickup method pick tools wanted use. each tools create class it, say, knife class has cut method. after invoking pickup method on robot want tell robot cut. oop concept have tell robot, not knife? , cut method on knife how can invoke that? have implement kind of usetoolcurrentlyheld() on robot propagate commands knife. or directly call knife (that robot hold) directly using this: myrobot.toolcurrentlyinhand.cut()

i feel it's weird parent method must have take care of classes contain. have duplicate methods, knife has cut() , robot must have usecutonknife() invoke cut() on knife because oop practice abstract knife out , make feel ordering robot without worrying internal information knife.

another question, if composing music create music class contains many measure class store note information. in 1 measure there can many note class inside note class have information like, in measure note reside, or how long note plays. want add 1 note on measure 45 placed on middle of measure. create measure have call createmeasure(45) on music , call createnote(0.5f) on measure? method create on parent that? , if want change note @ 0.25 on measure instead, 1 responsible have method change note note class or measure class? or must implement method change notes on topmost music class?

this overview of classes :

class music {      list<measure> measures = new list<measure>();       //methods... }  class measure {      int measurenumber;      list<note> notes = new list<note>();       //methods... }  class note {      float positioninmeasure; //from 0 1 } 

as music class on top of have issue through music? , chaining method call innermost class?

what need here common interface tools implement.

e.g.:

interface itool {     void use(); } 

now knife can implement interface:

class knife : itool {     public void use() {         //do cutting logic     } } 

now use tool through itool interface on robot, not knowing tool is:

class robot {     private itool currenttool = null;      public void pickup(itool tool)     {         currenttool = tool;     }      public void usetool() {         currenttool.use();     } }  

and can invoke pickup that:

 robot robot = new robot();  robot.pickup(new knife());  robot.usetool(); 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -