java GOF23设计模式-简单工厂模式

public class CarFactory {
//简单工厂模式1
    public Car createCar(String type)
    {
        if(type.equals("奥迪"))
        {
            return new Audi();
        }else if(type.equals("比亚迪"))
        {
            return new Byadi();
        }
        return null;

    }
    //或简单工厂模式2
    public static Car createAudi()
    {
        return new Audi();
    }
    public static Car createByadi()
    {
        return new Byadi();
    }
}

猜你喜欢

转载自blog.51cto.com/14437184/2440411