• <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>
            隨筆 - 181  文章 - 15  trackbacks - 0
            <2008年12月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            My Tech blog

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            運用多態(tài)(polymorphism)取代與價格相關(guān)的條件邏輯
            在另一個對象的屬性基礎上運行switch語句,并不是什么好主意。如果不得不使用,也應該在對象自己的數(shù)據(jù)上而不是在別人的數(shù)據(jù)上使用。選擇對象之間的傳遞關(guān)系的時候,應當考慮選擇將穩(wěn)定的對象的屬性傳遞給易變的對象(如書中的將租期長度來自Rental(穩(wěn)定,不易變)傳遞給Movie(不穩(wěn)定,易變))。
            對于類A和類B,如果A中存在因B而變化的屬性或方法體,則將它們移動到B中,A中只保留移動后B暴露給A的接口(或方法)。
            終于.......我們來到繼承(inheritance)
            此部分包含的重構(gòu)方法:
            Self Encapsulate Field:自封裝域。
            Move Method:移動方法。
            Replace Conditional with Polymorphism:用多態(tài)替換條件選擇。
            Replace Type Code with State/Strategy:在這個方法中使用了上面三個方法作為步驟。即用狀態(tài)、策略替換代碼,將與類型相依的行為搬移到模式內(nèi)。在使用它的時候,多使用對于以類型相依的行為進行Self Encapsulate Field作為第一步驟。從而確保任何時候都通過getter和setter兩個函數(shù)來運行這些行為。第二步通常采用Move Method方法,即把代碼從超類的宿主中搬移到子類中去。第三步采用Replace Conditional with Polymorphism方法,將switch,if等條件分支語句轉(zhuǎn)變?yōu)槎鄳B(tài)形式。
            下面是一個小實驗:
            一、重構(gòu)之前的代碼:

            public class ClassA {
                
            public int getValue(TheType type)
                {
                    
            switch(type)
                    {
                    
            case SmallValue:return 100;
                    
            case MidValue:return 200;
                    
            case BigValue:return 300;
                    
            default:return 0;
                    
                    }
                }
                
                
            public static void main(String args[])
                {
                    ClassA instanceA
            =new ClassA();
                    System.out.println(
            "theValueIs:"+instanceA.getValue(TheType.SmallValue));
                }
            }

            應用Self Encapsulate Field之后的效果:

            public class ClassAModified {

                
            /**
                 * 
            @param args
                 
            */
                
            public TheValue _theValue;
                
            public int getValue(TheType type)
                {
                    
            switch(type)
                    {
                    
            case SmallValue:return 100;
                    
            case MidValue:return 200;
                    
            case BigValue:return 300;
                    
            default:return 0;
                    
                    }
                }

                    
                
            public TheValue get_theValue() {
                    
            return _theValue;
                }

                
            public void set_theValue(TheType type) {
                        }
                    
            }
            public class TheValue
                {
                
                }
                    

            應用Move Method之后的效果:

            public class ClassAModified {

                
            /**
                 * @param args
                 
            */
                
            public TheValue _theValue;
                
            public int getValue(TheType type)
                {
                    
            return _theValue.getValue(type);
                }

                
            public static void main(String[] args) {
                    
            // TODO Auto-generated method stub

                }
                
                
            public TheValue get_theValue() {
                    
            return _theValue;
                }

                
            public void set_theValue(TheType type) {
                    _theValue
            =new TheValue();
                }
                
            public class TheValue
                {
                    
            public int getValue(TheType type)
                    {
                        
            switch(type)
                        {
                        
            case SmallValue:return 100;
                        
            case MidValue:return 200;
                        
            case BigValue:return 300;
                        
            default:return 0;
                        
                        }
                    }
                }
                

            }

             應用Replace Conditional with Polymorphism之后的效果:

            public class ClassAModified {

                
            /**
                 * @param args
                 
            */
                
            public TheValue _theValue;
                
            public int getValue(TheType type)
                {
                    
            return _theValue.getValue();
                }

                
            public static void main(String[] args) {
                    
            // TODO Auto-generated method stub

                }
                
                
            public TheValue get_theValue() {
                    
            return _theValue;
                }

                
            public void set_theValue(TheType type) {
                    
            switch(type)
                    {
                        
            case SmallValue:_theValue= new SmallValue();
                        
            case MidValue:_theValue= new MidValue();
                        
            case BigValue:_theValue= new BigValue();
                        
            default:_theValue=new TheValue();
                    
                    }
                }
                
            public class TheValue
                {
                    
            public int getValue()
                    {
                        
            return 0;
                    }
                }
                
            public class SmallValue extends TheValue
                {
                    
            public int getValue()
                    {
                        
            return 100;
                    }
                }
                
            public class MidValue extends TheValue
                {
                    
            public int getValue()
                    {
                        
            return 200;
                    }
                }
                
            public class BigValue extends TheValue
                {
                    
            public int getValue()
                    {
                        
            return 300;
                    }
                }

            }

            結(jié)語
            重構(gòu)的節(jié)奏:測試、小修改、測試、小修改、測試、小修改。。。
            正是這種節(jié)奏讓重構(gòu)得以快速而安全的前進。
             


             

            posted on 2007-06-20 21:42 littlegai 閱讀(197) 評論(0)  編輯 收藏 引用 所屬分類: 我的讀書筆記
            亚洲国产精品久久66| 国产美女久久久| 99精品国产综合久久久久五月天| 久久精品国产99久久久古代| 国产精品久久久久久福利漫画| 精品久久久久中文字| 天堂久久天堂AV色综合| 久久美女网站免费| 亚洲精品乱码久久久久久蜜桃图片 | 99久久免费国产精品特黄| 久久综合香蕉国产蜜臀AV| 久久久久亚洲AV成人网人人网站| 久久人人爽人人爽人人片AV麻烦 | 亚洲综合精品香蕉久久网| 久久精品国产一区二区三区不卡| 久久精品无码专区免费青青| 亚洲国产综合久久天堂| 国产精品成人久久久久三级午夜电影| 久久综合给合久久国产免费| 久久久久国产精品嫩草影院| 久久se精品一区二区影院| 99久久免费只有精品国产| 久久99精品久久久久婷婷| 婷婷综合久久中文字幕蜜桃三电影| 久久青青草原精品国产不卡| 国产—久久香蕉国产线看观看| 国产成人久久精品激情 | 日韩久久久久久中文人妻| 久久99久久99精品免视看动漫| 久久综合色老色| 精品久久久久久久国产潘金莲 | 久久99精品国产99久久| 欧美一区二区精品久久| 青青青国产成人久久111网站| 久久精品国产99国产精偷| 久久香蕉国产线看观看99| 91精品国产综合久久香蕉| 精品水蜜桃久久久久久久| 欧美国产精品久久高清| 中文字幕热久久久久久久| 久久精品国产亚洲精品2020|