diff options
Diffstat (limited to 'java')
-rw-r--r-- | java/PatternFactory.java | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/java/PatternFactory.java b/java/PatternFactory.java new file mode 100644 index 0000000..e137efe --- /dev/null +++ b/java/PatternFactory.java @@ -0,0 +1,39 @@ + +abstract class Human +{ + enum Gender { MALE, FEMALE } + abstract protected Gender construct(); + private Gender g; + + public Human() { + g = construct(); + } + + public void say() + { + System.out.println("I am a " + g); + } +} + +class Female extends Human +{ + @Override + protected Gender construct() { return Gender.FEMALE; } +} + +class Male extends Human +{ + @Override + protected Gender construct() { return Gender.MALE; } +} + + +public class PatternFactory { + + public static void main (String [] args ) + { + (new Female()).say(); + (new Male()).say(); + } +} + |