• <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
            欧美久久综合九色综合| MM131亚洲国产美女久久| 久久久精品国产| 久久久久亚洲精品无码蜜桃| 97久久精品人人做人人爽| 久久久亚洲欧洲日产国码是AV | 亚洲精品无码久久一线| 亚洲精品高清久久| 人妻少妇久久中文字幕| 青草久久久国产线免观| 久久精品一区二区| 亚洲AV日韩精品久久久久久| 久久国产成人| 韩国无遮挡三级久久| 久久综合国产乱子伦精品免费| 国内精品久久久久久久久电影网| 久久久久亚洲AV成人片| 国内精品人妻无码久久久影院导航| 久久国产免费| 久久99精品久久久久久9蜜桃 | 久久ZYZ资源站无码中文动漫| 久久九九久精品国产免费直播| av午夜福利一片免费看久久 | 国产91久久综合| 国产欧美久久一区二区| 97超级碰碰碰久久久久| 国产精品免费看久久久| 天天躁日日躁狠狠久久| 久久久久久精品久久久久| 久久无码高潮喷水| 四虎国产精品成人免费久久| 国产精品99久久久久久宅男小说| 青草久久久国产线免观| 久久er国产精品免费观看8| 国产精品美女久久久免费| 久久精品成人国产午夜| 国产成人综合久久综合| 99久久国产亚洲高清观看2024 | 一级做a爰片久久毛片看看| 欧美色综合久久久久久| 久久综合偷偷噜噜噜色|