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

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 329040
            • 排名 - 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)  編輯 收藏 引用 所屬分類: JavaDesign Pattern
            久久99精品久久久久久秒播| 久久精品99久久香蕉国产色戒| 国产午夜精品久久久久免费视 | 国产成人香蕉久久久久| 久久国产精品免费一区| 久久久91人妻无码精品蜜桃HD| 99久久精品免费看国产一区二区三区| 人妻无码中文久久久久专区| 久久男人中文字幕资源站| 久久久久亚洲AV无码网站| 欧美日韩中文字幕久久伊人| 99精品国产99久久久久久97 | 国产午夜精品理论片久久| 亚洲精品成人网久久久久久| 久久这里只精品国产99热| 久久免费视频1| 国产精品九九久久精品女同亚洲欧美日韩综合区| 国产福利电影一区二区三区久久老子无码午夜伦不 | 国产91色综合久久免费| 精品久久久久久久无码| 日韩久久久久中文字幕人妻| 国产高潮国产高潮久久久91 | 久久狠狠色狠狠色综合| 精品久久久久久国产| 国产精品久久久久a影院| 色天使久久综合网天天| 久久免费精品一区二区| 久久精品国产亚洲AV嫖农村妇女| 久久国产精品无码HDAV| 中文字幕无码免费久久| 久久99毛片免费观看不卡| 久久久亚洲AV波多野结衣 | 久久99精品国产99久久6男男| 色8激情欧美成人久久综合电| 伊人久久大香线蕉影院95| 久久综合综合久久狠狠狠97色88| 久久人妻无码中文字幕| 色妞色综合久久夜夜| 久久精品国产AV一区二区三区| 伊人精品久久久久7777| 亚洲AⅤ优女AV综合久久久|