首页 > 代码库 > 工厂模式

工厂模式

?
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class Factory2 {
 
    /**
     * @param args  工厂模式
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
        Ifactory ifactory =new people1factory();
        leifeng student=ifactory.creatLeifeng();
        student.wash();
        student.buy();
    }
 
}
 
//******************************************************************************
 
class leifeng{//雷锋类
     
    public void wash() {
        System.out.println("洗");
    }
    public void buy() {
        System.out.println("买");
    }
}
 
class people1 extends leifeng{
    public void wash() {
    System.out.println("洗1");
    }
     
}//人员1
class people2 extends leifeng{
    public void wash() {
    System.out.println("洗2");
    }
     
}//人员2
 
//******************************************************************************
interface Ifactory{//接口工厂
     
    leifeng creatLeifeng();
}
 
//1工厂实现了工厂接口,生产人员1
class people1factory implements Ifactory{
 
    @Override
    public leifeng creatLeifeng() {
        // TODO Auto-generated method stub
        return new people1();
    }
}
 
//2工厂实现了工厂接口,生产人员2
class people2factory implements Ifactory{
 
    @Override
    public leifeng creatLeifeng() {
        // TODO Auto-generated method stub
        return new people2();
    }
}