浏览 1873 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (5)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-24
最后修改:2009-06-24
抽象的比萨商品类 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); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-07-24
这个叫工厂方法模式吧……
|
|
返回顶楼 | |