Skip to content

Instantly share code, notes, and snippets.

@PetrGlad
Created December 30, 2010 16:50
Show Gist options
  • Save PetrGlad/759993 to your computer and use it in GitHub Desktop.
Save PetrGlad/759993 to your computer and use it in GitHub Desktop.
"this instanceof" antipattern
class Animal {
void action() {
bark();
}
}
class Cat extends Animal {
@Override
void action() {
meow();
}
}
abstract class Animal {
abstract void action();
}
class Dog extends Animal {
@Override
void action() {
bark();
}
}
class Cat extends Animal {
@Override
void action() {
meow();
}
}
interface Animal {
void action();
}
class Dog implements Animal {
@Override
void action() {
bark();
}
}
class Cat implements Animal {
@Override
void action() {
meow();
}
}
class BogusAnimal {
void action() {
if (this instanceof Cat) meow();
else bark();
}
}
class Cat extends BogusAnimal {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment