简单工厂

何时使用:我们明确地计划不同条件下创建不同实例时。

如何解决:让其子类实现工厂接口,返回的也是一个抽象的产品。

优点: 1、一个调用者想创建一个对象,只要知道其名称就可以了。 2、扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。 3、屏蔽产品的具体实现,调用者只关心产品的接口。

缺点:每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。这并不是什么好事。

 

此处的例子

父类

//披萨父类
public abstract class Pizza {
    private String name;//名字
    private double price;//价格
    private double size;//大小

    public abstract void show();

    public Pizza(String name, double price, double size) {
        this.name = name;
        this.price = price;
        this.size = size;
    }

    public Pizza() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getSize() {
        return size;
    }

    public void setSize(double size) {
        this.size = size;
    }
}

 

培根披萨类

//培根披萨类
class PgPizza extends Pizza{

    private double weight;//培根重量

    @Override
    public void show() {
        System.out.println("名字:"+this.getName()+"
价格:"+this.getPrice()+"大洋
大小:"+this.getSize()+"寸
培根重量:"+this.weight);
    }

    public PgPizza(String name, double price, double size, double weight) {
        super(name, price, size);
        this.weight = weight;
    }

    public PgPizza(double weight) {
        this.weight = weight;
    }

    public PgPizza(){}

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }
}

海鲜披萨类

//海鲜披萨类
class HxPizza extends Pizza{

    private String type;//海鲜种类

    @Override
    public void show() {
        System.out.println("名字:"+this.getName()+"
价格:"+this.getPrice()+"大洋
大小:"+this.getSize()+"寸
海鲜种类:"+this.type);
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public HxPizza(String name, double price, double size, String type) {
        super(name, price, size);
        this.type = type;
    }

    public HxPizza(String type) {
        this.type = type;
    }

    public HxPizza(){}
}

生产披萨的类

//生产Pizza类
class CreatePizzaFactory{
    public static Pizza getPizza(int choose){
        Pizza pizza=null;
        //生产什么披萨
        if(choose==1){
            pizza=new PgPizza("豪华培根披萨",99.8,35.5,1.3);
        }else if(choose==2){
            pizza=new HxPizza("豪华海鲜披萨",199.8,25.5,"基围虾");
        }else{
            return null;
        }
        return pizza;
    }
}

根据输入的数字输出相应的披萨信息

class Test{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要选择的披萨");
        System.out.println("1.培根披萨  2.海鲜披萨");
        try {
            int choose=input.nextInt();
            Pizza pizza = CreatePizzaFactory.getPizza(choose);
            if(pizza!=null){
                pizza.show();
            }else{
                System.out.println("没有该种类的披萨");
            }
        } catch (Exception e){
            System.out.println("无效输入");
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/xhlwjy/p/11323346.html