文章詳情頁(yè)
使用UML編寫(xiě)Java 設(shè)計(jì)模式例子 FactoryMethod Pattern
瀏覽:2日期:2024-06-30 14:20:02
內(nèi)容: 看了論壇上的文章,讀FactoryMethod Pattern UML圖,寫(xiě)了個(gè)小例子程序。做為文章的補(bǔ)充!//Creator.javapublic abstract class Creator{ /** * looks like a factory * contains some products and some process methods */ protected Product duct; abstract String processProduct(); abstract String processProductSpecial(); public String Operation() { return processProduct(); // FactoryMethod for implemention; }}//ConcreteProduct.javapublic class ConcreteProduct extends Product{ public ConcreteProduct() { System.out.println('construct the ConcreteProduct object'); } public String makeString(String str) { return 'This is the product after processing:'+str; }}//Product.javapublic abstract class Product{ public abstract String makeString(String str);}//ConcreteCreatorA.javapublic class ConcreteCreatorA extends Creator{ /** * You can write some other class like this to override * the same methods do diffrent thing; */ public ConcreteCreatorA() { duct=new ConcreteProduct(); System.out.println('instanced a product'); } public String processProduct() { System.out.println('product A:'); return duct.makeString('Make @#a product'); } public String processProductSpecial() { System.out.println('special product B:'); //add another method here return duct.makeString('Make Special @#B product'); }}//TestFactoryMethod.javapublic class TestFactoryMethod{ public TestFactoryMethod() { } public static void main(String[] arg) { ConcreteCreatorA cc=new ConcreteCreatorA(); System.out.println(cc.Operation()); System.out.println(cc.processProductSpecial()); }} Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
標(biāo)簽:
Java
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. php設(shè)計(jì)模式之職責(zé)鏈模式實(shí)例分析【星際爭(zhēng)霸游戲案例】3. WML的簡(jiǎn)單例子及編輯、測(cè)試方法第1/2頁(yè)4. python之PySide2安裝使用及QT Designer UI設(shè)計(jì)案例教程5. php設(shè)計(jì)模式之組合模式實(shí)例詳解【星際爭(zhēng)霸游戲案例】6. 探討JDBC 4.0在設(shè)計(jì)和性能方面的改進(jìn)7. PHP設(shè)計(jì)模式(七)組合模式Composite實(shí)例詳解【結(jié)構(gòu)型】8. 深入分析PHP設(shè)計(jì)模式9. Java之單例設(shè)計(jì)模式示例詳解10. php設(shè)計(jì)模式之狀態(tài)模式實(shí)例分析【星際爭(zhēng)霸游戲案例】
排行榜
