Suppose I have this code:
public Foo {
public enum Bar {FooBar, BarFoo, FooFoo, BarBar}
public Foo (Bar bar) {
//Do something
}
}
Now, when creating an object, I have several different cases for what migth be created: FooBar, BarFoo, FooFoo, BarBar
. Depending on what these cases are, how might I execute a different method for each? I suppose I am looking to do something a bit like this:
public Foo {
public enum Bar {FooBar, BarFoo, FooFoo, BarBar}
public Foo (Bar bar) {
switch (bar) {
case FooBar: fooBar(bar); break;
case BarFoo: barFoo(bar); break;
case FooFoo: fooFoo(bar); break;
case BarBar: barBar(bar); break;
default: break;
}
}
void fooBar (Bar bar) {
//Treat this as the constructor class for case FooBar
}
void barFoo (Bar bar) {
//Treat this as the constructor class for case barFoo
}
void fooFoo (Bar bar) {
//Treat this as the constructor class for case fooFoo
}
void barBar (Bar bar) {
//Treat this as the constructor class for case barBar
}
}
In addition, is this a bad practice? Would it be better to create a separate class for each case and drop the enum?
Aucun commentaire:
Enregistrer un commentaire