快速业务通道

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

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-22
?£ê?à′éè???a???ó?ú£o

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

class DataInfo {} interface Local { ????DataInfo[] loadDB(String filename); } interface Remote extends Local{ ??????void connect2WWW(String url); } class LocalMode implements Local { ??public DataInfo[] loadDB(String name) { ????System.out.print("Load from a local database "); ????return null; ??} } class RemoteMode implements Remote { ??public void connect2WWW(String url) { ????System.out.println("Connect to a remote site "); ??}

????//????ê???loadDB·?·¨μ?áíò???êμê???public DataInfo[] loadDB(String name) { ???? System.out.println("Load from a remote database "); ???? return null; ??} }

????//?????íê?3é?ó1¤3§á?interface ConnectionFactory { ??Local getLocalConnection(); ??Remote getRemoteConnection(); } class DataManager implements ConnectionFactory { ????boolean local = false; ????DataInfo[] data; ????//... ????public Local getLocalConnection() { ????????return new LocalMode(); ????} ????public Remote getRemoteConnection() { ????????return new RemoteMode(); ????} ????public??void loadData() { ???????? if(local){ ????????????Local conn = getLocalConnection(); ????????????data = conn.loadDB("db.db"); ???????? }else { ????????????Remote conn = getRemoteConnection(); ????????????conn.connect2WWW("www.some.where.com"); ????????????data = conn.loadDB("db.db"); ???????? } ???? } ???? // work on data ????public void setConnection(boolean b) { ?????? local = b; ????} }

//ó?ò???2aê?·?·¨à′2aê?é?ê?μ?ààclass Test { ???? public static void main(String[] args) { ???????? DataManager dm = new DataManager(); ???????? DataInfo[] di = null; ???????? String dbFileName = "db.db"$ ???????? if (args.length == 1) { ???????????? //assume local is set to true ???????? dm.setConnection(true); ???????????? LocalMode lm = (LocalMode)dm.getLocalConnection(); ???????? di = lm.loadDB(dbFileName); ???????? } else {?? ?????????? //Note: dm.local = false is default setting ?????????? RemoteMode rm = (RemoteMode)dm.getRemoteConnection(); ?????????? rm.connect2WWW("www.javacamp.org/db/"); ?????????? di = rm.loadDB(dbFileName); ???????? } ???????? //ò????í?éò?????ê1ó?ò?ì×·?·¨à′′|àíó?é?ê?·?·¨?ó??μ?êy?Y ???????? //Like di.find(), di.search() etc. ???? //′ó′?£?2?Dè?ù?aêy?Y?aμ?á??ó??μ£D? ???? } }

????ê?′ó?üá?DDμ÷ó?Testàà????DD2aê?μ??é??£?×¢òa£?òò2?êyμ?2?í?£???3???μ?2?í???DD?á1??£

C:> java Test Connect to a remote site Load from a remote database C:> java Test local Load from a local database C:??Comma

?a?ùμ?éè??ò?°??áó??ú?úSun1????a·¢è??±μ?è??¤??ê??D?£è?1????éò?′ó2?í?μ?μ?·??ó??êy?Y£?????Dè?úconnection1¤3§?ó?ú?D?óè?ò???·?·¨£??úo?êêμ?????êμê??ü£???2?Dè??????3ìDò?á11óD??oáμ???±??£

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站: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号