`
pan_java
  • 浏览: 286172 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

hand first 设计模式 - 工厂模式

阅读更多
工厂模式定义,创建一个对象接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推迟到子类.

抽象的比萨商品类
public abstract class PiazzStore {
	
	public Piazz getPiazz(String name){
	
		Piazz piazz = createPiazz(name);
		
		piazz.make();
		
		return piazz;
		
	}
	
      //让子类去实现
      public abstract Piazz createPiazz(String name);

}


中国比萨商店
public class ChinaPiazzStore extends PiazzStore {

	@Override
	public Piazz createPiazz(String name) {
		// TODO Auto-generated method stub
		if(name.equals(PiazzEnum.ChinaPiazzOnHuAn.getVal())){
			return new HuAnPiazz();
		}else if(name.equals(PiazzEnum.ChinaPiazzOnHuBei.getVal())){
			return new HuBeiPiazz();
		}else{
			throw new RuntimeException("piazz name is error");
		}
	}

}



台湾比萨商店
public class TaiWanPiazzStore extends PiazzStore {

	@Override
	public Piazz createPiazz(String name) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		if (name.equals(PiazzEnum.TaiWanPiazzOnTaiNan.getVal())) {
			return new TaiNanPiazz();
		} else if (name.equals(PiazzEnum.TaiWanPiazzOnTaiZhong.getVal())) {
			return new TaiZhongPiazz();
		} else {
			throw new RuntimeException("piazz name is error");
		}
	}
}



比萨种类

public enum PiazzEnum {
	
	//湖南比萨
	ChinaPiazzOnHuAn("china_huan"),
	
	//湖北比萨
	ChinaPiazzOnHuBei("china_hubei"),
	
	//台中比萨
	TaiWanPiazzOnTaiZhong("taiwan_taizhong"),
	
	//台南比萨
	TaiWanPiazzOnTaiNan("taiwan_tainan");
	
	
	private String val; 
	
	PiazzEnum(String val){
		this.val = val;
	}

	public String getVal() {
		return val;
	}
}



所有比萨父类
public abstract class Piazz {

	private String name;

	private double price;

	public abstract void make();

	public void step1() {
          System.out.println("make piazz step1");
	}

	public void step2() {
		System.out.println("make piazz step2");
	}

	public void step3() {
		System.out.println("make piazz step3");
	}

	@Override
	public String toString() {
		return "Name is " + name + " Price is " + price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	
    
}


湖南比萨
public class HuAnPiazz extends Piazz {
	
	public HuAnPiazz(){
		this.setName("HuAnPiazz");
		this.setPrice(11.98);
	}

	@Override
	 //不同地方制作步骤不一样
          public void make() {
		// TODO Auto-generated method stub
		step1();
		step2();
		step3();
	}
	
	
}


湖北比萨
public class HuBeiPiazz extends Piazz {
	
	public HuBeiPiazz(){
		this.setName("HuBeiPiazz");
		this.setPrice(12.96);
	}

	@Override
         //不同的制作方法
	public void step1() {
		System.out.println(" this is HuBeiPiazz step1");
	}

	@Override
         //不同地方制作步骤不一样
	public void make() {
		// TODO Auto-generated method stub
		step1();
		step3();
		step2();

	}
}


台中比萨
public class TaiZhongPiazz extends Piazz {

	
	public TaiZhongPiazz(){
		this.setName("TaiZhongPiazz");
		this.setPrice(17.96);
	}
	
	
	@Override
	public void make() {
		// TODO Auto-generated method stub
		step1();
		step2();
		step3();

	}

}


台南比萨
public class TaiNanPiazz extends Piazz {
	
	public TaiNanPiazz(){
		this.setName("TaiNanPiazz");
		this.setPrice(19.96);
	}

	@Override
	public void step3() {
		System.out.println("this is TaiNanPiazz step3");
	}

	@Override
	public void make() {
		// TODO Auto-generated method stub
		step3();
		step2();
		step1();
	}

}


测试类
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		PiazzStore chinaStore = new ChinaPiazzStore();
		
		PiazzStore taiwanStore = new TaiWanPiazzStore();
		
		
		Piazz huanPiazz = chinaStore.getPiazz("china_huan");
		
		System.out.println(huanPiazz);
		
		
        
		Piazz taizhongPiazz = taiwanStore.getPiazz("taiwan_tainan");
		
		System.out.println(taizhongPiazz);

	}

}
分享到:
评论
1 楼 NK_HansWang 2009-07-24  
这个叫工厂方法模式吧……

相关推荐

Global site tag (gtag.js) - Google Analytics