• JAVA设计模式学习笔记-工厂模式


    案例1
    披萨

    不好的设计

    所有披萨的父类

    package com.java.test.pizzastore.pizza;
    
    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;
    	}
    }
    

    一种披萨

    package com.java.test.pizzastore.pizza;
    
    public class PepperPizza extends Pizza {
    
    	@Override
    	public void prepare() {
    		// TODO Auto-generated method stub
    		super.setname("PepperPizza");
    		
    		System.out.println(name+" preparing;");
    	}
    
    }
    

    另一种披萨

    package com.java.test.pizzastore.pizza;
    
    public class NYPepperPizza extends Pizza {
    
    	@Override
    	public void prepare() {
    		// TODO Auto-generated method stub
    		super.setname("NYPepperPizza");
    		
    		System.out.println(name+" preparing;");
    	}
    
    }
    

    OrderPizza
    这里不好的问题在于每加一种新的披萨,这个类里面也要相应的修改

    package com.java.test.pizzastore;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import com.java.test.pizzastore.pizza.CheesePizza;
    import com.java.test.pizzastore.pizza.ChinesePizza;
    import com.java.test.pizzastore.pizza.GreekPizza;
    import com.java.test.pizzastore.pizza.PepperPizza;
    import com.java.test.pizzastore.pizza.Pizza;
    
    public class OrderPizza {
    
    	public OrderPizza() {
    		Pizza pizza = null;
    		String ordertype;
    
    		do {
    			ordertype = gettype();
    
    			if (ordertype.equals("cheese")) {
    				pizza = new CheesePizza();
    			} else if (ordertype.equals("greek")) {
    				pizza = new GreekPizza();
    			} else if (ordertype.equals("pepper")) {
    				pizza = new PepperPizza();
    			} else if (ordertype.equals("chinese")) {
    				pizza = new ChinesePizza();
    			} else {
    				break;
    			}
    			pizza.prepare();
    			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 type:");
    			String str = strin.readLine();
    
    			return str;
    		} catch (IOException e) {
    			e.printStackTrace();
    			return "";
    		}
    	}
    
    }
    

    简单工厂模式方案

    工厂类

    package com.java.test.pizzastore.simplefactory;
    
    import com.java.test.pizzastore.pizza.CheesePizza;
    import com.java.test.pizzastore.pizza.GreekPizza;
    import com.java.test.pizzastore.pizza.PepperPizza;
    import com.java.test.pizzastore.pizza.Pizza;
    
    public class SimplePizzaFactory {
    
    	public Pizza CreatePizza(String ordertype) {
    		Pizza pizza = null;
    
    		if (ordertype.equals("cheese")) {
    			pizza = new CheesePizza();
    		} else if (ordertype.equals("greek")) {
    			pizza = new GreekPizza();
    		} else if (ordertype.equals("pepper")) {
    			pizza = new PepperPizza();
    		}
    		return pizza;
    
    	}
    
    }
    

    新的OrderPizza

    package com.java.test.pizzastore.simplefactory;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import com.java.test.pizzastore.pizza.CheesePizza;
    import com.java.test.pizzastore.pizza.GreekPizza;
    import com.java.test.pizzastore.pizza.PepperPizza;
    import com.java.test.pizzastore.pizza.Pizza;
    
    public class OrderPizza {
    	SimplePizzaFactory mSimplePizzaFactory;
    
    	public OrderPizza(SimplePizzaFactory mSimplePizzaFactory) {
    
    		setFactory(mSimplePizzaFactory);
    	}
    
    	public void setFactory(SimplePizzaFactory mSimplePizzaFactory) {
    		Pizza pizza = null;
    		String ordertype;
    
    		this.mSimplePizzaFactory = mSimplePizzaFactory;
    
    		do {
    			ordertype = gettype();
    			pizza = mSimplePizzaFactory.CreatePizza(ordertype);
    			if (pizza != null) {
    				pizza.prepare();
    				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 type:");
    			String str = strin.readLine();
    
    			return str;
    		} catch (IOException e) {
    			e.printStackTrace();
    			return "";
    		}
    	}
    
    }
    

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

    披萨加盟店需求
    工厂方法模式设计方案:将披萨项目里的披萨对象实例化功能抽象成抽象方法,在
    不同加盟店具体实现功能。

    新的OrderPizza

    package com.java.test.pizzastore.method;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import com.java.test.pizzastore.pizza.CheesePizza;
    import com.java.test.pizzastore.pizza.ChinesePizza;
    import com.java.test.pizzastore.pizza.GreekPizza;
    import com.java.test.pizzastore.pizza.PepperPizza;
    import com.java.test.pizzastore.pizza.Pizza;
    /*变成了抽象类*/
    public abstract class OrderPizza {
    
    	public OrderPizza() {
    		Pizza pizza = null;
    		String ordertype;
    
    		do {
    			ordertype = gettype();
    			pizza = createPizza(ordertype);
    
    			pizza.prepare();
    			pizza.bake();
    			pizza.cut();
    			pizza.box();
    		} while (true);
    	}
    
    	abstract Pizza createPizza(String ordertype);
    
    	private String gettype() {
    		try {
    			BufferedReader strin = new BufferedReader(new InputStreamReader(
    					System.in));
    			System.out.println("input pizza type:");
    			String str = strin.readLine();
    
    			return str;
    		} catch (IOException e) {
    			e.printStackTrace();
    			return "";
    		}
    	}
    
    }
    

    一家披萨店
    LDOrderPizza

    package com.java.test.pizzastore.method;
    
    import com.java.test.pizzastore.pizza.CheesePizza;
    import com.java.test.pizzastore.pizza.GreekPizza;
    import com.java.test.pizzastore.pizza.LDCheesePizza;
    import com.java.test.pizzastore.pizza.LDPepperPizza;
    import com.java.test.pizzastore.pizza.PepperPizza;
    import com.java.test.pizzastore.pizza.Pizza;
    
    public class LDOrderPizza extends OrderPizza {
    
    	@Override
    	Pizza createPizza(String ordertype) {
    		Pizza pizza = null;
    
    		if (ordertype.equals("cheese")) {
    			pizza = new LDCheesePizza();
    		} else if (ordertype.equals("pepper")) {
    			pizza = new LDPepperPizza();
    		}
    		return pizza;
    
    	}
    
    }
    

    另一家披萨店
    NYOrderPizza

    package com.java.test.pizzastore.method;
    
    import com.java.test.pizzastore.pizza.CheesePizza;
    import com.java.test.pizzastore.pizza.GreekPizza;
    import com.java.test.pizzastore.pizza.NYCheesePizza;
    import com.java.test.pizzastore.pizza.NYPepperPizza;
    import com.java.test.pizzastore.pizza.PepperPizza;
    import com.java.test.pizzastore.pizza.Pizza;
    
    public class NYOrderPizza extends OrderPizza {
    
    	@Override
    	Pizza createPizza(String ordertype) {
    		Pizza pizza = null;
    
    		if (ordertype.equals("cheese")) {
    			pizza = new NYCheesePizza();
    		} else if (ordertype.equals("pepper")) {
    			pizza = new NYPepperPizza();
    		}
    		return pizza;
    
    	}
    
    }
    

    抽象工厂模式

    抽象工厂模式:定义了一个接口用于创建相关或有依赖关系的对象族,而无需明确
    指定具体类

    就是把工厂给抽象了一层
    AbsFactory

    package com.java.test.pizzastore.absfactory;
    
    import com.java.test.pizzastore.pizza.Pizza;
    
    public interface AbsFactory {
    	public Pizza CreatePizza(String ordertype) ;
    }
    

    一家工厂

    package com.java.test.pizzastore.absfactory;
    
    import com.java.test.pizzastore.pizza.LDCheesePizza;
    import com.java.test.pizzastore.pizza.LDPepperPizza;
    import com.java.test.pizzastore.pizza.Pizza;
    
    public class LDFactory implements AbsFactory {
    
    	@Override
    	public Pizza CreatePizza(String ordertype) {
    		Pizza pizza = null;
    
    		if (ordertype.equals("cheese")) {
    			pizza = new LDCheesePizza();
    		} else if (ordertype.equals("pepper")) {
    			pizza = new LDPepperPizza();
    		}
    		return pizza;
    
    	}
    
    }
    

    另一家工厂

    package com.java.test.pizzastore.absfactory;
    
    import com.java.test.pizzastore.pizza.NYCheesePizza;
    import com.java.test.pizzastore.pizza.NYPepperPizza;
    import com.java.test.pizzastore.pizza.Pizza;
    
    public class NYFactory implements AbsFactory {
    
    	
    	@Override
    	public Pizza CreatePizza(String ordertype) {
    		Pizza pizza = null;
    
    		if (ordertype.equals("cheese")) {
    			pizza = new NYCheesePizza();
    		} else if (ordertype.equals("pepper")) {
    			pizza = new NYPepperPizza();
    		}
    		return pizza;
    
    	}
    
    }
    

    依赖抽象原则
    变量不要持有具体类的引用
    不要让类继承自具体类,要继承自抽象类或接口
    不要覆盖基类中已实现的方法

  • 相关阅读:
    十大排序
    算法11----判断是否为回文词(双端队列判断)
    算法10-----分糖果
    算法9-----输出全排列(递归)---移除K个数,剩下最小数。
    算法8-----罗马字转整数(分治法)
    Python数据结构2-----队列和堆
    Python数据结构1-----基本数据结构和collections系列
    Python笔记22-----高阶函数
    10、TV UI
    9、创建向后兼容的用
  • 原文地址:https://www.cnblogs.com/kristse/p/factory.html
Copyright © 2020-2023  润新知