好文档 - 专业文书写作范文服务资料分享网站

设计模式课设之米线订购子系统

天下 分享 时间: 加入收藏 我要投稿 点赞

//抽象工厂 public abstract class RNoddleStore { public abstract RiceNoodle createRiceNoddle(String name); public abstract PeiLiao AddPeiLiao(RiceNoodle riceNoddle,String name); public final RiceNoodle OrderRiceNoddle(String name){ RiceNoodle riceNoddle = createRiceNoddle(name); return riceNoddle; } } //具体工厂(干浆米线) public class GanNoddleStore extends RNoddleStore{ public RiceNoodle createRiceNoddle(String name) { RiceNoodle riceNoddle = null; if(name.equals(\)) { riceNoddle = new GanNoodle_B(); }else if(name.equals(\)) { riceNoddle = new GanNoodle_M(); }else if(name.equals(\)) { riceNoddle = new GanNoodle_S(); } return riceNoddle; } public PeiLiao AddPeiLiao(RiceNoodle riceNoddle, String name) { if(name.equals(\)) { riceNoddle = new Beef(riceNoddle); }else if(name.equals(\)) { riceNoddle = new Egg(riceNoddle); } return (PeiLiao)riceNoddle; } }

6

//具体工厂(酸浆米线) public class SuanNoddleStore extends RNoddleStore{ public RiceNoodle createRiceNoddle(String name) { RiceNoodle riceNoddle = null; if(name.equals(\)) { riceNoddle = new SuanNoddle_B(); }else if(name.equals(\)) { riceNoddle = new SuanNoddle_M(); }else if(name.equals(\)) { riceNoddle = new SuanNoddle_S(); }return riceNoddle; } public PeiLiao AddPeiLiao(RiceNoodle riceNoddle,String name) { if(name.equals(\)) { riceNoddle = new Beef(riceNoddle); }else if(name.equals(\)) { riceNoddle = new Egg(riceNoddle); }return (PeiLiao)riceNoddle; } } //具体工厂(水米线) public class WaterNoddleStore extends RNoddleStore{ public RiceNoodle createRiceNoddle(String name) { RiceNoodle riceNoddle = null; if(name.equals(\)) { riceNoddle = new WaterNoodle_B(); }else if(name.equals(\)) { riceNoddle = new WaterNoodle_M(); }else if(name.equals(\)) { riceNoddle = new WaterNoodle_S(); }return riceNoddle; } public PeiLiao AddPeiLiao(RiceNoodle riceNoddle,String name) { if(name.equals(\)) { riceNoddle = new Beef(riceNoddle); }else if(name.equals(\)) { riceNoddle = new Egg(riceNoddle); }return (PeiLiao)riceNoddle; } }

7

2.3、装饰模式:

不同的米线可能搭配不同的配料,配料的价格要单独收费,并且米线的包装风格必须一致,这里装饰模式可以很好的解决这个问题。 结构图:

代码实现: //配料抽象类 public abstract class PeiLiao extends RiceNoodle { public PeiLiao(double price, String RiceNoddleSize, String species) { super(price, RiceNoddleSize, species); } public abstract String getSpecies(); } //具体配料(牛肉) public class Beef extends PeiLiao { RiceNoodle riceNoodle; public Beef(RiceNoodle riceNoodle) { super(riceNoodle.getPrice(), riceNoodle.getRicenoodelSize(), riceNoodle.getSpecies()); this.riceNoodle = riceNoodle; } public double money() { return 40 + riceNoodle.money(); } public String getSpecies() { return \牛肉\ + riceNoodle.getSpecies(); }} 8

public class Egg extends PeiLiao { RiceNoodle riceNoodle; public Egg(RiceNoodle riceNoodle) { super(riceNoodle.getPrice(), riceNoodle.getRicenoodelSize(), riceNoodle.getSpecies()); this.riceNoodle = riceNoodle; } public double money() { return 30 + riceNoodle.money(); } public String getSpecies() { return \鸡蛋\ + riceNoodle.getSpecies(); } } 抽象米线类 // public abstract class RiceNoodle { double price; String ricenoodelSize; String species; public abstract double money(); public RiceNoodle(double price,String ricenoodelSize,String species) { super(); this.price = price; this.ricenoodelSize = ricenoodelSize; this.species = species; } public double getPrice(){ return price; } public String getRicenoodelSize(){ return ricenoodelSize; } public String getSpecies(){ return species; } }

//具体配料(鸡蛋) 9

public abstract class GanNoodle extends RiceNoodle{ public GanNoodle(double price, String ricenoodelSize, String species) { super(price, ricenoodelSize, species); } public GanNoodle(double price,String ricenoodelSize){ this(price,ricenoodelSize,\干浆\); } public String getSpecies() { return \; } } //酸浆米线 public abstract class SuanNoddle extends RiceNoodle{ public SuanNoddle(double price,String ricenoddleSize,String species){ super(price,ricenoddleSize,species); } public SuanNoddle(double price,String ricenoddleSize){ this(price,ricenoddleSize,\酸浆\); } public String getSpecies() { return \酸浆\; } } //水米线 public abstract class WaterNoodle extends RiceNoodle{ public WaterNoodle(double price, String ricenoodelSize, String species) { super(price, ricenoodelSize, species); } public WaterNoodle(double price,String ricenoodelSize){ this(price,ricenoodelSize,\水米线\); } public String getSpecies() { return \水米线\; } } //干浆米线 10

设计模式课设之米线订购子系统

//抽象工厂publicabstractclassRNoddleStore{publicabstractRiceNoodlecreateRiceNoddle(Stringname);publicabstractPeiLiaoAddPeiLiao(RiceNoodlericeNoddle,Stringname);publicfinal
推荐度:
点击下载文档文档为doc格式
95pxr2grtg6x2111f20r4n7xz5ee5l00bjl
领取福利

微信扫码领取福利

微信扫码分享