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

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328978
            • 排名 - 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)  編輯 收藏 引用 所屬分類: Java 、Design Pattern
            色妞色综合久久夜夜| 久久久久人妻精品一区二区三区 | 韩国三级大全久久网站| 精品久久久久久无码专区不卡 | 精品午夜久久福利大片| 欧美激情精品久久久久久久| 中文字幕久久久久人妻| 狠狠人妻久久久久久综合| 亚洲va久久久噜噜噜久久狠狠| 国产999精品久久久久久| 久久人人爽人人爽人人片AV东京热 | 精品人妻久久久久久888| 欧美午夜精品久久久久久浪潮| 色欲久久久天天天综合网精品| 精品久久久久久99人妻| AV狠狠色丁香婷婷综合久久| 精品国产日韩久久亚洲| 成人精品一区二区久久久| 国产精品禁18久久久夂久| 亚洲国产成人精品91久久久| 99久久婷婷国产一区二区 | 精品久久久久久国产牛牛app| 久久久无码人妻精品无码| 久久久久久久久久久| 亚洲欧洲精品成人久久曰影片| 精品久久久久久无码国产| av无码久久久久久不卡网站| 人妻少妇久久中文字幕| 超级碰碰碰碰97久久久久| 性做久久久久久久久久久| 合区精品久久久中文字幕一区| 精品免费久久久久国产一区| 国产99久久久国产精品~~牛| 色综合久久久久| 国产精品成人99久久久久| 国产激情久久久久影院| 久久精品国产精品亜洲毛片| 99久久无码一区人妻| 欧美一级久久久久久久大片| 中文字幕精品久久| 一本色道久久88精品综合|