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

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328479
            • 排名 - 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 閱讀(477) 評論(0)  編輯 收藏 引用 所屬分類: JavaDesign Pattern
            久久精品国产亚洲AV不卡| 97精品国产97久久久久久免费| 国内精品久久久久久99蜜桃| 99久久国语露脸精品国产| 国产视频久久| 久久精品国产亚洲AV蜜臀色欲| 久久精品中文字幕无码绿巨人| 国产精品青草久久久久福利99| 欧美一区二区久久精品| 狠狠色婷婷综合天天久久丁香| 亚洲国产精品成人久久蜜臀| 国产成人精品久久一区二区三区| 久久综合九色综合欧美就去吻| 久久99精品国产自在现线小黄鸭 | 久久久久18| 久久综合精品国产二区无码| 一本久久综合亚洲鲁鲁五月天| 国产精品美女久久久久久2018| 亚洲va久久久久| 久久e热在这里只有国产中文精品99| 久久精品国产男包| 亚洲国产精品成人久久蜜臀| 国产午夜精品理论片久久| 精品久久久噜噜噜久久久| 久久婷婷国产剧情内射白浆| 热综合一本伊人久久精品| 久久久久免费精品国产| 性做久久久久久久| 国产成年无码久久久免费| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 亚洲午夜久久久久久噜噜噜| 欧洲国产伦久久久久久久| 欧美粉嫩小泬久久久久久久| 久久99亚洲综合精品首页| 狠狠精品干练久久久无码中文字幕| 久久无码av三级| 一本久久久久久久| 国产精品成人99久久久久| 久久精品国产亚洲5555| 久久久WWW成人| 久久久久久精品无码人妻|