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();
}
}