Bootstrap

设计模式-工厂模式(Java)

简单工厂模式

一个抽象接口,多个抽象接口实现类,一个工厂类

/**
简单工厂模式
一个抽象接口,多个抽象接口实现类,一个工厂类
**/
public abstract class Car{
    public abstract void run();
    public abstract void stop(); 
}
public class Ford extends Car{
    public void run(){
        System.out.println("Ford is running...");
    }
    public void stop(){
        System.out.println("Ford is stopping...");
    }
}
public class Benz extends Car{
    public void run(){
        System.out.println("Benz is running...");
    }
    public void stop(){
        System.out.println("Benz is stopping...");
    }
}
public class Factory{
    public static Car getCarInstance(String type){
        Car c = null;
        if("Benz".equals(type)){
            c = new Benz();
        }else if("Ford".equals(type)){
            c= new Ford();
        }
        return c;
    } 
}
public class Test{
    
;