快速业务通道

Java设计模式(二) - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-22
4个方法的具体实施和建一层房子的不同

class TwoStoryHouse extends HouseBuilder {    public TwoStoryHouse(String features) {      house.setType(this.getClass() + " " + features);    }    public void buildFoundation() {      //doEngineering()      //doExcavating()      //doPlumbingHeatingElectricity()      //doSewerWaterHookUp()      //doFoundationInspection()      house.setProgress("foundation is done");    }    public void buildFrame() {      //doHeatingPlumbingRoof()      //doElectricityRoute()      //doDoorsWindows()      //doFrameInspection()      house.setProgress("frame is under construction");    }    public void buildExterior() {      //doOverheadDoors()      //doBrickWorks()      //doSidingsoffitsGutters()      //doDrivewayGarageFloor()      //doDeckRail()      //doLandScaping()      house.setProgress("Exterior is waiting to start");    }    public void buildInterior() {      //doAlarmPrewiring()      //doBuiltinVacuum()      //doInsulation()      //doDryWall()      //doPainting()      //doLinoleum()      //doCabinet()      //doTileWork()      //doLightFixtureBlinds()      //doCleaning()      //doInteriorInspection()      house.setProgress("Interior is not started yet");    } } class House {    private String type = null;    private List features = new ArrayList();    public House() {    }    public House(String type) {      this.type = type;    }    public void setType(String type) {      this.type = type;    }    public String getType() {      return type;    }    public void setProgress(String s) {      features.add(s);    }    public String toString() {      StringBuffer ff = new StringBuffer();      String t = type.substring(6);      ff.append(t + "n ");      for (int i = 0; i < features.size(); i ++) {        ff.append(features.get(i) + "n ");      }      return ff.toString();    } } class TestBuilder {    public static void main(String[] args) {     HouseBuilder one = new OneStoryHouse("2 bedrooms, 2.5 baths, 2-car garage, 1500 sqft");     HouseBuilder two = new TwoStoryHouse("4 bedrooms, 4 baths, 3-car garage, 5000 sqft");     WorkShop shop = new WorkShop();     shop.construct(one);     shop.construct(two);     System.out.println("Check house building progress: n");     System.out.println(one.showProgress());     System.out.println(two.showProgress());   } }

Java设计模式(二)(4)

时间:2011-01-01 yeeyan 翻译:易晓斓

//需要用jdk1.5以上来编译

C:> javac TestBuilder.java C:> java TestBuilder

//运行结果

Check house building progress: OneStoryHouse 2 bedrooms, 2.5 baths, 2-car garage, 1500 sqft foundation is done frame is done Exterior is done Interior is under going TwoStoryHo

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号