• Java设计模式(五) 工厂模式


    1,定义抽象产品类

    package com.pattern.factory;
    
    import java.util.ArrayList;
    
    public abstract class Pizza {
    	String name;
    	String dough;
    	String sauce;
    	ArrayList toppings = new ArrayList();
    	
    	void prepare(){
    		System.out.println("Prepareing "+name);
    		System.out.println("Tossing dough...");
    		System.out.println("Adding sauce...");
    		System.out.println("Adding toppings: ");
    		for(int i=0;i<toppings.size();i++){
    			System.out.println(" "+toppings.get(i));
    		}
    	}
    	
    	void bake(){
    		System.out.println("Bake for 25 minutes at 350");
    	}
    	
    	void cut(){
    		System.out.println("Cutting the pizza into diagonal slices");
    	}
    	
    	void box(){
    		System.out.println("Place pizza in official PizzaStore box");
    	}
    	
    	public String getName(){
    		return name;
    	}
    }
    

    2,具体产品类

    package com.pattern.factory;
    public class ChicagoStyleCheesePizza extends Pizza {
      public ChicagoStyleCheesePizza(){
      name = "Chicago Style Deep Dish Cheese Pizza";
      dough = "Extra Thick Crust Dough";
      sauce = "Plum Tomato Sauce";
      
      toppings.add("Shredded Mozzarella Cheese");
     }
     
     void cut(){
      System.out.println("Cutting the pizza into square slices");
     }
    }
    package com.pattern.factory;
    
    public class NYStyleCheesePizza extends Pizza {
    	
    	public NYStyleCheesePizza(){
    		name = "NY Style Sauce and Cheese Pizza";
    		dough = "Thin Crust Dough";
    		sauce = "Marinara Sauce";
    		
    		toppings.add("Grated Reggiano Cheese");
    	}
    }

    3,定义抽象工厂类

    package com.pattern.factory;
    
    public abstract class PizzaStore {
    	
    	public Pizza orderPizza(String type){
    		Pizza pizza ;
    		pizza = createPizza(type);
    		pizza.prepare();
    		pizza.bake();
    		pizza.cut();
    		pizza.box();
    		
    		return pizza;
    	}
    
    	protected abstract Pizza createPizza(String type) ;
    
    }


    4,定义具体工厂类

    package com.pattern.factory;
    
    import com.pattern.factory.bean.ChicagoStyleClamPizza;
    import com.pattern.factory.bean.ChicagoStylePepperoniPizza;
    import com.pattern.factory.bean.ChicagoStyleVeggiePizza;
    
    public class ChicagoPizzaStore extends PizzaStore {
    
    	/** 
    	 * @see com.pattern.factory.PizzaStore#createPizza(java.lang.String)
    	 */
    	@Override
    	protected Pizza createPizza(String type) {
    		if(type.equals("cheese")){
    			return new ChicagoStyleCheesePizza();
    		} else if(type.equals("veggie")){
    			return new ChicagoStyleVeggiePizza();
    		} else if(type.equals("clam")){
    			return new ChicagoStyleClamPizza();
    		} else if(type.equals("pepperoni")){
    			return new ChicagoStylePepperoniPizza();
    		} else {
    			return null;
    		} 
    	}
    
    }
    package com.pattern.factory;
    
    import com.pattern.factory.bean.NYStyleClamPizza;
    import com.pattern.factory.bean.NYStylePepperoniPizza;
    import com.pattern.factory.bean.NYStyleVeggiePizza;
    
    public class NYPizzaStore extends PizzaStore {
    
    	/** 
    	 * @see com.pattern.factory.PizzaStore#createPizza(java.lang.String)
    	 */
    	@Override
    	protected Pizza createPizza(String type) {
    		if(type.equals("cheese")){
    			return new NYStyleCheesePizza();
    		} else if(type.equals("veggie")){
    			return new NYStyleVeggiePizza();                   
    		} else if(type.equals("clam")){
    			return new NYStyleClamPizza();
    		} else if(type.equals("pepperoni")){
    			return new NYStylePepperoniPizza();
    		} else {
    			return null;
    		} 
    	}
    
    }

    5,测试类

    package com.pattern.factory;
    
    public class PizzaTestDrive {
    	
    	public static void main(String[] args) {
    		PizzaStore nyStore = new NYPizzaStore();
    		PizzaStore chicagoStore = new ChicagoPizzaStore();
    		
    		Pizza pizza = nyStore.orderPizza("cheese");
    		System.out.println("Ethan ordered a "+pizza.getName()+"
    ");
    		
    		pizza = chicagoStore.orderPizza("cheese");
    		System.out.println("Joel order a "+ pizza.getName()+"
    ");
    	}
    }

    测试结果:

    Prepareing NY Style Sauce and Cheese Pizza
    Tossing dough...
    Adding sauce...
    Adding toppings:
     Grated Reggiano Cheese
    Bake for 25 minutes at 350
    Cutting the pizza into diagonal slices
    Place pizza in official PizzaStore box
    Ethan ordered a NY Style Sauce and Cheese Pizza

    Prepareing Chicago Style Deep Dish Cheese Pizza
    Tossing dough...
    Adding sauce...
    Adding toppings:
     Shredded Mozzarella Cheese
    Bake for 25 minutes at 350
    Cutting the pizza into square slices
    Place pizza in official PizzaStore box
    Joel order a Chicago Style Deep Dish Cheese Pizza

    类图如下所示:






     

  • 相关阅读:
    敏捷开发 之 计划、测试 与 重构
    敏捷开发 之 理论概述篇
    第二十章 排查和调试Web程序 之 设计异常处理策略
    第十九章 排查和调试Web程序 之 防止和排查运行时问题
    第十八章 提升用户体验 之 减少带宽占用
    第十七章 提升用户体验 之 使用MVC扩展功能控制程序行为
    第十六章 提升用户体验 之 设计实现routes
    第十五章 提升用户体验 之 设计实现MVC controllers 和 actions
    第十四章 提升用户体验 之 设计实现国际化和本地化
    Nginx系统学习笔记(3)同一端口下设置Alias(虚拟目录)
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986818.html
Copyright © 2020-2023  润新知