您现在的位置是:首页 >技术教程 >设计模式 - 工厂模式网站首页技术教程

设计模式 - 工厂模式

逆流丶石头 2024-06-24 06:01:02
简介设计模式 - 工厂模式

简单工厂模式

基本介绍:

  • 简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一 个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族 中最简单实用的模式
  • 简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行 为
  • 在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会 使用到工厂模式

定义抽象类:

public abstract class Pizza {
    protected String name;
    public abstract void prepare();
    public void bake() {
        System.out.println(name + " baking;");
    }
    public void cut() {
        System.out.println(name + " cutting;");
    }
    public void box() {
        System.out.println(name + " boxing;");
    }
    public void setName(String name) {
        this.name = name;
    }
}

定义 Pizza 继承类:

public class CheesePizza extends Pizza{
    @Override
    public void prepare() {
        setName("奶酪pizza");
        System.out.println(name + " preparing;");
    }
}

public class GreekPizza extends Pizza {
    @Override
    public void prepare() {
        setName("GreekPizza");
        System.out.println(name + " preparing;");
    }
}

public class PepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("PepperPizza");
        System.out.println(name + " preparing;");
    }
}

定义 工厂类:

public class SimplePizzaFactory {
    public Pizza createPizza(String type){
        System.out.println(" 使用简单工厂模式");
        Pizza pizza = null;
        if (type.equals("cheese")) {
            pizza = new CheesePizza();
        }else if(type.equals("greek")){
            pizza = new GreekPizza();
        } else if (type.equals("pepper")) {
            pizza = new PepperPizza();
        }
        return pizza;
    }
}

使用:

public class OrderPizza {
  SimplePizzaFactory simpleFactory;
  Pizza pizza;
  String orderType;

    public OrderPizza(SimplePizzaFactory simpleFactory, String orderType) {
        this.orderType = orderType;
        this.setFactory(simpleFactory);
    }

    public void setFactory(SimplePizzaFactory simpleFactory) {
        this.simpleFactory = simpleFactory;
        pizza = simpleFactory.createPizza(orderType);
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
    }

    public static void main(String[] args) {
        SimplePizzaFactory simpleFactory = new SimplePizzaFactory();
        OrderPizza orderPizza = new OrderPizza(simpleFactory, "greek");
    }
}

工厂方法模式

基本介绍:

定义了一个创建对象的抽象方法,由子类决定要实例化的类。工厂方 法模式将对象的实例化推迟到子类

定义抽象方法,让各个工厂子类自己实现:

public abstract class OrderPizza {
    Pizza pizza;

    public OrderPizza(String orderType) {
        pizza = createPizza(orderType);//抽象方法,由工厂子类完成
        //输出 pizza 制作过程
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
    }

    public OrderPizza() {

    }

    //定义一个抽象方法,createPizza,让各个工厂子类自己实现
    abstract Pizza createPizza(String orderlype);

    public static void main(String[] args) {
        OrderPizza pizza = new BJOrderPizza("cheese");
    }
}

子类实现一:

public class BJCheesePizza extends Pizza {
    @Override
    public void prepare() {
        setName("BJ奶酪pizza");
        System.out.println(name + " preparing;");
    }
}

public class BJPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("BJPepperPizza");
        System.out.println(name + " preparing;");
    }
}

public class BJOrderPizza extends OrderPizza{

    public BJOrderPizza(String orderType) {
        super(orderType);
    }

    @Override
    Pizza createPizza(String orderType) {
        Pizza pizza = null;
        if(orderType.equals("cheese")) {
            pizza = new BJCheesePizza();
        } else if(orderType.equals("pepper")) {
            pizza = new BJPepperPizza();
        }
        return pizza;
    }
}

子类实现二:

public class SHCheesePizza extends Pizza {
    @Override
    public void prepare() {
        setName("SH奶酪pizza");
        System.out.println(name + " preparing;");
    }
}

public class SHPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("SHPepperPizza");
        System.out.println(name + " preparing;");
    }
}

public class SHOrderPizza extends OrderPizza{
    public SHOrderPizza(String orderType) {
        super(orderType);
    }
    @Override
    Pizza createPizza(String orderType) {
        Pizza pizza = null;
        if(orderType.equals("cheese")) {
            pizza = new SHCheesePizza();
        } else if(orderType.equals("pepper")) {
            pizza = new SHPepperPizza();
        }
        return pizza;
    }
}

抽象工厂模式

基本介绍:

  • 抽象工厂模式:定义了一个interface用于创建相关或有依赖关系的对象簇,而无需 指明具体的类
  • 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合
  • 从设计层面看,抽象工厂模式就是对简单工厂模式的改进
  • 将工厂抽象成两层,AbsFactory(抽象工厂) 和 具体实现的工厂子类。程序员可以 根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇, 更利于代码的维护和扩展

抽象工厂的抽象层:

/**
 * 声明AbsFactory接口
 */
public interface AbsFactory {
    //让不同的工厂类来实现createPizza方法
    public Pizza createPizza(String type);
}

具体实现一:

public class BJCheesePizza extends Pizza {
    @Override
    public void prepare() {
        setName("BJ奶酪pizza");
        System.out.println(name + " preparing;");
    }
}

public class BJPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("BJPepperPizza");
        System.out.println(name + " preparing;");
    }
}

public class BJFactory implements AbsFactory {
    @Override
    public Pizza createPizza(String orderType) {
        Pizza pizza = null;
        if(orderType.equals("cheese")) {
            pizza = new BJCheesePizza();
        } else if(orderType.equals("pepper")) {
            pizza = new BJPepperPizza();
        }
        return pizza;
    }
}

具体实现二:

public class SHCheesePizza extends Pizza {
    @Override
    public void prepare() {
        setName("SH奶酪pizza");
        System.out.println(name + " preparing;");
    }
}

public class SHPepperPizza extends Pizza {
    @Override
    public void prepare() {
        setName("SHPepperPizza");
        System.out.println(name + " preparing;");
    }
}

public class SHFactory implements AbsFactory {
    @Override
    public Pizza createPizza(String orderType) {
        Pizza pizza = null;
        if(orderType.equals("cheese")) {
            pizza = new SHCheesePizza();
        } else if(orderType.equals("pepper")) {
            pizza = new SHPepperPizza();
        }
        return pizza;
    }
}

执行类:

public class OrderPizza {
    AbsFactory factory;
    String orderType;
    // 构造器
    public OrderPizza(AbsFactory factory, String orderType) {
        this.orderType = orderType;
        setFactory(factory);
    }

    public void setFactory(AbsFactory factory){
        Pizza pizza = null;
        this.factory = factory;

        pizza = factory.createPizza(orderType);
        //输出 pizza 制作过程
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
    }

    public static void main(String[] args) {
        BJFactory factory = new BJFactory();
        OrderPizza orderPizza = new OrderPizza(factory, "greek");
    }
}

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。