• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            為生存而奔跑

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328403
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

            用java接口實現工廠模式如下:

            interface Service{
                
            void method1();
                
            void method2();
            }

            interface ServiceFactory{
                Service getService();
            }

            class Implementation1 implements Service{
                Implementation1(){};
                
            public void method1(){System.out.println("Implemention1 method1");}
                
            public void method2(){System.out.println("Implemention1 method2");}
            }

            class Implementation2 implements Service{
                Implementation2(){}
                
            public void method1(){System.out.println("Implementation2 method1");}
                
            public void method2(){System.out.println("Implementation2 method2");}
            }

            class Implementation1ServiceFactory implements ServiceFactory{
                
            public Service getService(){
                    
            return new Implementation1();
                }
            }

            class Implementation2ServiceFactory implements ServiceFactory{
                
            public Service getService(){
                    
            return new Implementation2();
                }
            }

            public class TopCoder{
                
            public static void main(String[] args){
                    serviceConsumer(
            new Implementation1ServiceFactory());
                    serviceConsumer(
            new Implementation2ServiceFactory());
                }
                
                
            public static void serviceConsumer(ServiceFactory sf){
                    Service s
            =sf.getService();
                    s.method1();
                    s.method2();
                }
            }


            該實現有一個小小的缺憾,那就是Implementation1 和 Implementation2的構造函數都是public。下面用Java的內部類實現工廠模式,可以彌補這個缺憾

            interface Service{
                
            void method1();
                
            void method2();
            }

            interface ServiceFactory{
                Service getService();
            }

            class Implementation1 implements Service{
                Implementation1(){};
                
            public void method1(){System.out.println("Implemention1 method1");}
                
            public void method2(){System.out.println("Implemention1 method2");}
                
                
            public static ServiceFactory factory=new ServiceFactory(){
                    
            public Service getService(){
                        
            return new Implementation1(); 
                    }
                };
            }

            class Implementation2 implements Service{
                Implementation2(){}
                
            public void method1(){System.out.println("Implementation2 method1");}
                
            public void method2(){System.out.println("Implementation2 method2");}
                
                
            public static ServiceFactory factory=new ServiceFactory(){
                    
            public Service getService(){
                        
            return new Implementation2(); 
                    }
                };
            }


            public class TopCoder{
                
            public static void main(String[] args){
                    serviceConsumer(Implementation1.factory);
                    serviceConsumer(Implementation2.factory);
                }
                
                
            public static void serviceConsumer(ServiceFactory sf){
                    Service s
            =sf.getService();
                    s.method1();
                    s.method2();
                }
            }
            posted on 2010-04-10 10:32 baby-fly 閱讀(476) 評論(0)  編輯 收藏 引用 所屬分類: JavaDesign Pattern
            91久久精一区二区三区大全| 99热成人精品热久久669| 色综合合久久天天综合绕视看 | AV色综合久久天堂AV色综合在 | 久久久久亚洲AV成人网| 久久久久久久久久久免费精品 | 激情五月综合综合久久69| 伊人久久大香线蕉综合网站| 国产三级久久久精品麻豆三级| 久久精品国产精品青草| 国内精品久久久久影院薰衣草| 乱亲女H秽乱长久久久| 欧美久久综合九色综合| 人人狠狠综合久久亚洲88| 亚洲人成无码www久久久| 99久久99久久| 亚洲午夜久久久影院伊人| 久久精品国产一区二区电影| 久久天天躁狠狠躁夜夜网站| 香蕉久久影院| 国产精品熟女福利久久AV| 99久久精品国产一区二区| 久久综合久久久| 久久男人Av资源网站无码软件| 一本色道久久综合狠狠躁| 久久精品无码一区二区三区日韩| 久久久久久亚洲精品成人| 久久精品综合网| 伊人久久大香线蕉无码麻豆| 久久婷婷五月综合成人D啪| 久久国产高清一区二区三区| 午夜欧美精品久久久久久久| 久久精品青青草原伊人| 久久久99精品成人片中文字幕 | 日本高清无卡码一区二区久久| 精品国产91久久久久久久 | 国产精品久久新婚兰兰| 青青草原综合久久大伊人| 色偷偷88欧美精品久久久| 久久一区二区三区99| 久久久无码精品亚洲日韩软件|