zoukankan      html  css  js  c++  java
  • 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

    类图如下所示:






     

  • 相关阅读:
    UITableViewCell 获取当前位置
    iOS图片拉伸
    TCP/IP基础
    AFNetworking报错"_UTTypeCopyPreferredTagWithClass", referenced from: _AFContentTypeForPathExtens
    iOS 后台处理
    统计iOS项目的总代码行数的方法
    iOS自定义model排序
    iOS开发 适配iOS10
    中文 iOS/Mac 开发博客列表
    C#--静态构造函数
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986818.html
Copyright © 2011-2022 走看看