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
}