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

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328403
            • 排名 - 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 閱讀(476) 評論(0)  編輯 收藏 引用 所屬分類: JavaDesign Pattern
            久久久九九有精品国产| 久久综合精品国产一区二区三区| 狠狠色丁香久久婷婷综合蜜芽五月| 香蕉99久久国产综合精品宅男自 | 国内精品伊人久久久久av一坑 | 区亚洲欧美一级久久精品亚洲精品成人网久久久久| 久久精品一区二区| 亚洲国产精品综合久久一线| 亚洲精品蜜桃久久久久久| 久久最近最新中文字幕大全| 欧美激情精品久久久久久| 999久久久无码国产精品| 久久久久这里只有精品| 久久精品无码专区免费青青| 青青青青久久精品国产h久久精品五福影院1421 | 久久精品中文字幕无码绿巨人| 国产AV影片久久久久久| 日日噜噜夜夜狠狠久久丁香五月| 久久av免费天堂小草播放| 国产精品免费福利久久| 久久精品国产亚洲av麻豆图片| 狠狠色综合网站久久久久久久| 狠狠精品久久久无码中文字幕| 久久精品无码一区二区三区免费| 国产精品久久久天天影视| 精产国品久久一二三产区区别| 久久免费观看视频| 精品国产婷婷久久久| 好属妞这里只有精品久久| 久久精品国产亚洲AV香蕉| 一本一道久久综合狠狠老 | 人妻少妇久久中文字幕一区二区| 久久天天躁狠狠躁夜夜av浪潮| 国产精品99久久久久久猫咪 | 久久国产精品99国产精| 99久久精品国产一区二区 | 日韩中文久久| 香蕉久久夜色精品国产尤物| 日韩中文久久| 久久99精品国产麻豆宅宅| 亚洲午夜无码久久久久|