• <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)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328978
            • 排名 - 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 閱讀(478) 評論(0)  編輯 收藏 引用 所屬分類: Java 、Design Pattern
            青草影院天堂男人久久| 久久精品国产亚洲AV蜜臀色欲| 亚洲成av人片不卡无码久久| 久久se精品一区精品二区国产| 亚洲国产天堂久久综合| 亚洲伊人久久大香线蕉综合图片| 久久久精品国产sm调教网站 | 青青青国产精品国产精品久久久久| 国产精品久久久天天影视| 怡红院日本一道日本久久 | 欧美久久综合九色综合| 99久久国产宗和精品1上映| 国产精品久久一区二区三区| 亚洲人成无码网站久久99热国产| 久久天天躁狠狠躁夜夜躁2O2O| 久久亚洲国产成人影院网站| 国产一区二区精品久久| 亚洲va久久久噜噜噜久久天堂| 无码乱码观看精品久久| AV无码久久久久不卡网站下载| 色偷偷88888欧美精品久久久| 精品久久久久久中文字幕| 伊人色综合九久久天天蜜桃| 精品人妻伦九区久久AAA片69| 九九精品99久久久香蕉| 欧美日韩精品久久免费| 色8久久人人97超碰香蕉987| 久久久久亚洲AV综合波多野结衣| 精品熟女少妇a∨免费久久| 国产精品久久久久久久人人看| 久久精品国产亚洲一区二区| 久久精品国产第一区二区三区 | 香蕉aa三级久久毛片| 国产69精品久久久久9999| 久久精品草草草| 久久精品99久久香蕉国产色戒| 久久精品国产男包| 亚洲午夜久久久久妓女影院| 精品国产乱码久久久久久呢 | 99久久精品国产一区二区| 久久久久噜噜噜亚洲熟女综合|