Created
December 30, 2010 16:50
-
-
Save PetrGlad/759993 to your computer and use it in GitHub Desktop.
"this instanceof" antipattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal { | |
void action() { | |
bark(); | |
} | |
} | |
class Cat extends Animal { | |
@Override | |
void action() { | |
meow(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Animal { | |
abstract void action(); | |
} | |
class Dog extends Animal { | |
@Override | |
void action() { | |
bark(); | |
} | |
} | |
class Cat extends Animal { | |
@Override | |
void action() { | |
meow(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Animal { | |
void action(); | |
} | |
class Dog implements Animal { | |
@Override | |
void action() { | |
bark(); | |
} | |
} | |
class Cat implements Animal { | |
@Override | |
void action() { | |
meow(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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