标签:com blog class div code java log ext c string color
| 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | publicclassFactory2 {    /**     * @param args  工厂模式     */    publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        //工厂模式        Ifactory ifactory =newpeople1factory();        leifeng student=ifactory.creatLeifeng();        student.wash();        student.buy();                System.out.println("***************");                //简单工厂模式        leifeng student1=simplyfactory.creLeifeng("people1");        student1.wash();        student1.buy();    }}//******************************************************************************classleifeng{//雷锋类        publicvoidwash() {        System.out.println("洗");    }    publicvoidbuy() {        System.out.println("买");    }}classpeople1 extendsleifeng{    publicvoidwash() {    System.out.println("洗1");    }    }//人员1classpeople2 extendsleifeng{    publicvoidwash() {    System.out.println("洗2");    }    }//人员2//******************************************************************************//工厂模式interfaceIfactory{//接口工厂        leifeng creatLeifeng();}//1工厂实现了工厂接口,生产人员1classpeople1factory implementsIfactory{    @Override    publicleifeng creatLeifeng() {        // TODO Auto-generated method stub        returnnewpeople1();    }}//2工厂实现了工厂接口,生产人员2classpeople2factory implementsIfactory{    @Override    publicleifeng creatLeifeng() {        // TODO Auto-generated method stub        returnnewpeople2();    }}//***********************************************************************************//简单工厂模式classsimplyfactory{        publicstaticleifeng creLeifeng(String type) {        leifeng resuLeifeng=null;        switch(type) {        case"people1":            resuLeifeng=newpeople1();            break;        case"people2":            resuLeifeng=newpeople2();            break;        }        returnresuLeifeng;    }} | 
简单工厂和工厂模式的合并对比代码,码迷,mamicode.com
标签:com blog class div code java log ext c string color
原文地址:http://www.cnblogs.com/daifei/p/3696794.html