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

            為生存而奔跑

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

            留言簿(5)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            積分與排名

            • 積分 - 326945
            • 排名 - 74

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            用java接口實(shí)現(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();
                }
            }


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

            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 閱讀(473) 評(píng)論(0)  編輯 收藏 引用 所屬分類: JavaDesign Pattern
            亚洲午夜久久久影院伊人| 国产精品成人99久久久久| 思思久久99热只有频精品66| 久久笫一福利免费导航 | 久久人爽人人爽人人片AV| 久久无码AV中文出轨人妻| 国产香蕉久久精品综合网| 久久九九久精品国产免费直播| 2021久久国自产拍精品| 大蕉久久伊人中文字幕| 久久久SS麻豆欧美国产日韩| 日韩精品久久久久久免费| 久久精品国产亚洲网站| 2021国产精品久久精品| 狠狠色噜噜狠狠狠狠狠色综合久久| 精品久久久久久无码免费| 天天爽天天狠久久久综合麻豆| 狠狠久久综合伊人不卡| 久久亚洲精品国产精品| 久久精品中文字幕第23页| 久久国产亚洲高清观看| 久久久久国产一区二区| 久久精品国产福利国产秒| 精品多毛少妇人妻AV免费久久| 国产精品无码久久久久| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 日本免费久久久久久久网站| 久久久黄片| 国产成人精品综合久久久| 日本久久久久亚洲中字幕| 色天使久久综合网天天| 久久99久久无码毛片一区二区| 久久久久久免费一区二区三区| 无码人妻久久一区二区三区免费 | 97久久国产露脸精品国产| 久久国产午夜精品一区二区三区| 精品久久一区二区| 天堂久久天堂AV色综合| 无码超乳爆乳中文字幕久久| 少妇精品久久久一区二区三区| 一本色道久久99一综合|