c# - Advise on abstraction -
i working on code whereby have abstract class has few core properties , run(int index) method. create new types inherit this. these new types can have multiple methods can called according index passed in.
public abstract class baseclass { public abstract void run(int index); } public class class1 : baseclass { public override void run(int index) { if (index == 0) { methoda(); } else if (index == 1) { methodb(); } } private void methoda() { //do stuff } private void methodb() { //do stuff } }
i'm wondering there better way this. these types , methods called ui, - menu click example. might have class1 , class2. class1 might have 3 methods call run(0) ... run(2) on it. class2 might have 1 internal method call run(0). maybe need keep collection of ints each class guess map methods. might have add string collection hold friendly name menu items etc..
can think of way implement type of mapping while maintaining abstraction possible? there better way go current idea?
one way:
you use interface instead:
public interface irunnablesomething { void run(); } public class myrunnablea :irunnablesomething { public void run() { // stuff } } public class myrunnableb :irunnablesomething { public void run() { // stuff } }
then in main class...
public override void run(irunnable runnable) { runnable.run(); }
example of calling it:
myinstanceofmainclass.run(new myrunnablea());
this seems fitting, since know index
passing in original version. moves int
based interface
based (less code in end).
Comments
Post a Comment