summaryrefslogtreecommitdiffstats
path: root/java/PatternFactory.java
blob: e137efe49f4c0734b89d0bcf27ae8bbebf215144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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();
    }
}