设计模式--工厂模式

设计模式--工厂模式,第1张

一 工厂模式定义

我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象

工厂顾名思义就是创建产品,根据产品是具体产品还是具体工厂可分为简单工厂模式和工厂方法模式,根据工厂的抽象程度可分为工厂方法模式抽象工厂模式

简单工厂模式 2.1 uml类图

2.2 具体实现

1 定义抽象产品

package com.cncloud.factory.simplefactory;

/**
 *
 * @author likun
 * @date 2022年04月29日 16:39
 */
public abstract class Pizza {
    private String pizzaName;

    public String getPizzaName() {
        return pizzaName;
    }

    public void setPizzaName(String pizzaName) {
        this.pizzaName = pizzaName;
    }

    /**
     *  材料
     */
    abstract void stuff();

    /**
     *  烘焙
     */
    void bake() {
        System.out.println(pizzaName+"烘焙中...");
    }

    void cut(){
        System.out.println(pizzaName+"切割中...");
    }

    void box(){
        System.out.println(pizzaName+"打包中...");
    }

}

2 定义产品的实现 具体的产品

public class PepperPizza extends Pizza{
    @Override
    void stuff() {
        System.out.println(super.getPizzaName()+"材料准备中...");
    }
}
public class ChessPizza extends Pizza{
    @Override
    void stuff() {
        // 准备胡椒pizaa的材料
        System.out.println(super.getPizzaName()+"材料准备中");
    }
}

Factory是用于创建具体的产品,创建的规则则是根据传入的参数不同来创建不同的产品

public class SimpleFactory {

    /**
     *  创造
     */
    Pizza generatePizaa(String pizzaType){
        switch (pizzaType){
            case "chess":
                ChessPizza chessPizza = new ChessPizza();
                chessPizza.setPizzaName("芝士pizaa");
                return chessPizza;
                // 胡椒
            case "pepper":
                PepperPizza pepperPizza = new PepperPizza();
                pepperPizza.setPizzaName("胡椒披萨");
                return pepperPizza;
            default:
                return null;
        }
    }


}

客户端调用

public class PizzaStore {
    private final SimpleFactory pizaaFactory;

    public PizzaStore(SimpleFactory pizaaFactory) {
        this.pizaaFactory = pizaaFactory;
    }

    private String type;
    private Pizza picizza;

    Pizza orderPizza(){
        do {
            type = getType();
            picizza=pizaaFactory.generatePizaa(type);
            if (picizza!=null){
                picizza.stuff();
                picizza.bake();
                picizza.cut();
                picizza.box();
            }else {
                System.out.println("没有匹配到相应的类型");
            }
        }while (true);
    }

    public String getType(){
        System.out.println("请输入你需要订购的类型");
        Scanner scanner = new Scanner(System.in);
        String type = scanner.nextLine();
        return type;
    }
}
public class Client {
    public static void main(String[] args) {
        SimpleFactory pizaaFactory = new SimpleFactory();
        PizzaStore pizzaStore = new PizzaStore(pizaaFactory);
        pizzaStore.orderPizza();
    }
}
三 工厂方法模式

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

3.1 uml类图

3.2 具体实现

1定义抽象产品

public abstract class Pizza {
    private String pizzaName;

    public String getPizzaName() {
        return pizzaName;
    }

    public void setPizzaName(String pizzaName) {
        this.pizzaName = pizzaName;
    }

    /**
     *  材料
     */
    abstract void stuff();

    /**
     *  烘焙
     */
    void bake() {
        System.out.println(pizzaName+"烘焙中...");
    }

    void cut(){
        System.out.println(pizzaName+"切割中...");
    }

    void box(){
        System.out.println(pizzaName+"打包中...");
    }

}

2定义不同类别的产品实现

public class BjChessPizza extends Pizza{
    @Override
    void stuff() {
        System.out.println(super.getPizzaName()+"材料准备中...");
    }
}

public class BjPepperPizza extends Pizza{
    @Override
    void stuff() {
        System.out.println(super.getPizzaName()+"材料准备中...");
    }
}
public class LdChessPizza extends Pizza{
    @Override
    void stuff() {
        System.out.println(super.getPizzaName()+"材料准备中...");
    }
}


public class LdPepperPizza extends Pizza{
    @Override
    void stuff() {
        System.out.println(super.getPizzaName()+"材料准备中...");
    }
}

定义创建对象的抽象方法

public abstract class OrderPizza {
    /**
     * 定义一个抽象方法,createPizza , 让各个工厂子类自己实现
     * 创建pizza
     * @param pizzaType
     * @return
     */
    abstract Pizza createPizza(String pizzaType);

    // 构造器
    public OrderPizza() {
        Pizza pizza = null;
        String orderType; // 订购披萨的类型
        do {
            orderType = getType();
            pizza = createPizza(orderType); //抽象方法,由工厂子类完成
            //输出pizza 制作过程
            pizza.stuff();
            pizza.bake();
            pizza.cut();
            pizza.box();

        } while (true);
    }



    // 写一个方法,可以获取客户希望订购的披萨种类
    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza 种类:");
            String str = strin.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

}

4 定义抽象的实现

public class BjOderPizza extends OrderPizza {
    @Override
    Pizza createPizza(String pizzaType) {
        switch (pizzaType) {
            case "chess":
                Pizza bjChessPizza = new BjChessPizza();
                bjChessPizza.setPizzaName("北京芝士披萨");
                return bjChessPizza;
            case "pepper":
                Pizza bjPepperPizza = new BjPepperPizza();
                bjPepperPizza.setPizzaName("北京胡椒披萨");
                return bjPepperPizza;
            default:
                return null;
        }
    }
}

public class LdOrderPizza extends OrderPizza {
    @Override
    Pizza createPizza(String pizzaType) {
        switch (pizzaType) {
            case "chess":
                Pizza ldChessPizza = new LdChessPizza();
                ldChessPizza.setPizzaName("伦敦芝士披萨");
                return ldChessPizza;
            case "pepper":
                Pizza ldPepperPizza = new LdPepperPizza();
                ldPepperPizza.setPizzaName("伦敦胡椒披萨");
                return ldPepperPizza;
            default:
                return null;
        }
    }
}

客户端调用

public class PizzaStore {
    public static void main(String[] args) {
        String loc = "bj";
        if (StrUtil.equals(loc, "bj")){
            BjOderPizza bjOderPizza = new BjOderPizza();

        }else if (StrUtil.equals(loc,"ld")){
            LdOrderPizza ldOrderPizza = new LdOrderPizza();
        }
    }
}
四 抽象工厂模式

将工厂抽象成两层,AbsFactory(抽象工厂) 和具体实现的工厂子类。程序员可以根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇,更利于代码的维护和扩展。

4.1 uml类图

4.2 具体实现

1定义抽象产品

public abstract class Pizza {
    private String pizzaName;

    public String getPizzaName() {
        return pizzaName;
    }

    public void setPizzaName(String pizzaName) {
        this.pizzaName = pizzaName;
    }

    /**
     *  材料
     */
    abstract void stuff();

    /**
     *  烘焙
     */
    void bake() {
        System.out.println(pizzaName+"烘焙中...");
    }

    void cut(){
        System.out.println(pizzaName+"切割中...");
    }

    void box(){
        System.out.println(pizzaName+"打包中...");
    }

}

2定义不同类别的产品实现

public class BjChessPizza extends Pizza{
    @Override
    void stuff() {
        System.out.println(super.getPizzaName()+"材料准备中...");
    }
}

public class BjPepperPizza extends Pizza{
    @Override
    void stuff() {
        System.out.println(super.getPizzaName()+"材料准备中...");
    }
}
public class LdChessPizza extends Pizza{
    @Override
    void stuff() {
        System.out.println(super.getPizzaName()+"材料准备中...");
    }
}


public class LdPepperPizza extends Pizza{
    @Override
    void stuff() {
        System.out.println(super.getPizzaName()+"材料准备中...");
    }
}

3 定义抽象接口

public interface AbstractPizzaFactory {
    Pizza createPizza(String pizzaType);
}

4 工厂实现

public class LdPizzaFactory implements AbstractPizzaFactory{
    @Override
    public Pizza createPizza(String pizzaType) {
        switch (pizzaType) {
            case "chess":
                Pizza ldChessPizza = new LdChessPizza();
                ldChessPizza.setPizzaName("伦敦芝士披萨");
                return ldChessPizza;
            case "pepper":
                Pizza ldPepperPizza = new LdPepperPizza();
                ldPepperPizza.setPizzaName("伦敦胡椒披萨");
                return ldPepperPizza;
            default:
                return null;
        }
    }
}
public class BjPizzaFactory implements AbstractPizzaFactory{
    @Override
    public Pizza createPizza(String pizzaType) {
        switch (pizzaType) {
            case "chess":
                Pizza ldChessPizza = new LdChessPizza();
                ldChessPizza.setPizzaName("伦敦芝士披萨");
                return ldChessPizza;
            case "pepper":
                Pizza ldPepperPizza = new LdPepperPizza();
                ldPepperPizza.setPizzaName("伦敦胡椒披萨");
                return ldPepperPizza;
            default:
                return null;
        }
    }
}

5客户端实现

public class OrderPizza {

	AbstractPizzaFactory factory;

	// 构造器
	public OrderPizza(AbstractPizzaFactory factory) {
		setFactory(factory);
	}

	private void setFactory(AbstractPizzaFactory factory) {
		Pizza pizza = null;
		String orderType = ""; // 用户输入
		this.factory = factory;
		do {
			orderType = getType();
			// factory 可能是北京的工厂子类,也可能是伦敦的工厂子类
			pizza = factory.createPizza(orderType);
			if (pizza != null) { // 订购ok
				pizza.stuff();
				pizza.bake();
				pizza.cut();
				pizza.box();
			} else {
				System.out.println("订购失败");
				break;
			}
		} while (true);
	}

	// 写一个方法,可以获取客户希望订购的披萨种类
	private String getType() {
		try {
			BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("input pizza 种类:");
			String str = strin.readLine();
			return str;
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}
}
public class PizzaStore {

	public static void main(String[] args) {
		new OrderPizza(new LdPizzaFactory());
	}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/799777.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存