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

            為生存而奔跑

               :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 326887
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

            用java接口實現(xiàn)工廠模式如下:

            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();
                }
            }


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

            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 閱讀(472) 評論(0)  編輯 收藏 引用 所屬分類: JavaDesign Pattern
            国内精品久久久人妻中文字幕 | 一本久道久久综合狠狠爱| 久久99国产精品99久久| 国产韩国精品一区二区三区久久| 久久久久亚洲AV无码专区首JN| 人妻无码精品久久亚瑟影视| 久久九九免费高清视频| 免费一级做a爰片久久毛片潮| 久久国产成人| 久久无码中文字幕东京热| 无夜精品久久久久久| 亚洲综合熟女久久久30p| 狠狠色婷婷久久综合频道日韩 | 久久久黄色大片| 久久久久亚洲av成人网人人软件| 一97日本道伊人久久综合影院| 波多野结衣久久一区二区| 国产aⅴ激情无码久久| 97r久久精品国产99国产精| 国产精品亚洲美女久久久| 人妻少妇精品久久| 久久99精品久久久久久久不卡 | 久久久久99精品成人片| 久久综合九色综合网站| …久久精品99久久香蕉国产| 91久久精品无码一区二区毛片| 深夜久久AAAAA级毛片免费看| 久久香综合精品久久伊人| 国产成人精品久久一区二区三区| 精品久久久久久无码中文野结衣| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 久久久精品国产Sm最大网站| 免费久久人人爽人人爽av| 久久久久夜夜夜精品国产| 亚洲日本久久久午夜精品| 久久本道伊人久久| 久久精品卫校国产小美女| 精品熟女少妇aⅴ免费久久| 久久亚洲精品国产精品| 少妇久久久久久被弄到高潮| 97精品国产91久久久久久|