问题
I have this messy code that I really want to clean up because it has about 12 else if's and each of these if statements check for the instanceof 2 objects, so something like: If (parentObj instanceof superclass && parentObj2 instanceof superclass)
Each if statement executes something different, so polymorphism I think won't make it any better. Also because 12 different super classes for this little functionality (most if statements execute 1 liners) would be a bit ridiculous. And another reason why polymorphism wouldn't work is because I do not have access to the parent class or the super classes. I know that many instanceof conditions is generally not good to do, even though I never really understood why. The methods that are executed inside each if statement are not of the parent class, but are of the super classes, so that's why I need to check for their types so that I can cast them and execute those methods. Any ideas as to how I can clean this up? Thanks!
EDIT: Sorry for the lack of detail, I wrote this on my phone. Anyway below is an example of what I am dealing with. I have looked at the strategy pattern, and my only concern with that was for the same reason that I would have to create many different classes and I felt like creating many different objects just to execute 1 liners all of the time would be a bit wasteful. Also it looks like to me that in a strategy design, I would still have to have the many instanceof checks to know which strategy to execute. Anyway, below is some code :p
if (plotBlockState instanceof Sign && UpgradeBlockState instanceof Sign) {
//do Sign Stuff
}
else if (plotBlockState instanceof Chest && UpgradeBlockState instanceof Chest) {
//do Chest Stuff
}
else if (plotBlockState instanceof Dispenser && UpgradeBlockState instanceof Dispenser) {
//do Dispenser Stuff
}
else if (plotBlockState instanceof Furnace && UpgradeBlockState instanceof Furnace) {
//do Furnace Stuff
}
else if (plotBlockState instanceof BrewingStand && UpgradeBlockState instanceof BrewingStand) {
//do Brew Stand Stuff
}
else if (plotBlockState instanceof Hopper && UpgradeBlockState instanceof Hopper) {
//do hopper Stuff
}
else if (plotBlockState instanceof Dropper && UpgradeBlockState instanceof Dropper) {
//do dropper Stuff
}
else if (plotBlockState instanceof Beacon && UpgradeBlockState instanceof Beacon) {
//do beacon Stuff
}
else if (plotBlockState instanceof CreatureSpawner && UpgradeBlockState instanceof CreatureSpawner) {
//do spawner Stuff
}
else if (plotBlockState instanceof NoteBlock && UpgradeBlockState instanceof NoteBlock) {
//do noteblock Stuff
}
else if (plotBlockState instanceof Jukebox && UpgradeBlockState instanceof Jukebox) {
//do jukebox Stuff
}
else if (plotBlockState instanceof Skull && UpgradeBlockState instanceof Skull) {
//do skull Stuff
}
else if (plotBlockState instanceof CommandBlock && UpgradeBlockState instanceof CommandBlock) {
//do commandblock Stuff
}
回答1:
The Strategy
pattern seems to suit this down to the ground. Here is the link to the wikipedia article, which should be more than enough to get you started, and because you've provided 0 code, I think I won't either.
But because I'm feeling nice
Firstly, I would have them implementing a common interface or superclass. This is so you don't need to test what type of class they are. Simply have them call the same method. I know you've said polymorphism is out of the question, but I'm not sure why. And if you don't have access to the super class.. make a common interface.
For example:
// Undesirable syntax.
if(obj instanceof Dog)
{
((Dog)obj).woof();
}
else if(obj instanceof Cat)
{
((Cat)obj).meow();
}
else if(obj instanceof Lion)
{
((Lion)obj).roar();
}
Now let's define some common interface, Animal
:
public interface Animal
{
public void speak();
}
And now your instanceof
tree looks like this:
public void makeTalk(Animal obj)
{
obj.speak();
}
来源:https://stackoverflow.com/questions/18231983/java-alternative-to-many-else-if-statements-with-instanceof-conditions