• <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
            <2007年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            My Tech blog

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

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

            應(yīng)用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
                {
                
                }
                    

            應(yīng)用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;
                        
                        }
                    }
                }
                

            }

             應(yīng)用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é)語(yǔ)
            重構(gòu)的節(jié)奏:測(cè)試、小修改、測(cè)試、小修改、測(cè)試、小修改。。。
            正是這種節(jié)奏讓重構(gòu)得以快速而安全的前進(jìn)。
             


             

            posted on 2007-06-20 21:42 littlegai 閱讀(190) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 我的讀書(shū)筆記
            国产高潮国产高潮久久久91 | 日韩亚洲欧美久久久www综合网 | 囯产极品美女高潮无套久久久| 久久精品成人欧美大片| 久久狠狠高潮亚洲精品| 国产精品99久久不卡| 久久婷婷色香五月综合激情| 久久精品夜夜夜夜夜久久| 办公室久久精品| 亚洲成色www久久网站夜月| 久久久久这里只有精品| 久久精品国产亚洲AV无码麻豆| 久久人人爽人人爽人人片AV不 | 久久精品国产精品亚洲艾草网美妙| 亚洲香蕉网久久综合影视| 久久久国产精品网站| av色综合久久天堂av色综合在| 久久久久免费精品国产| 亚洲精品乱码久久久久久久久久久久 | 久久91精品国产91久久户| 亚洲AV日韩AV永久无码久久| 国产精品亚洲美女久久久| 国产精品久久久久9999| 97久久精品人妻人人搡人人玩| 亚洲国产精品无码久久久久久曰| 久久免费小视频| 无码人妻久久一区二区三区免费| 日本高清无卡码一区二区久久| 99久久精品这里只有精品| 国产精品久久毛片完整版| 狠狠综合久久AV一区二区三区| 久久最新免费视频| 日产久久强奸免费的看| 九九久久精品国产| 中文成人无码精品久久久不卡 | 亚洲va久久久噜噜噜久久男同| 久久久久亚洲av综合波多野结衣| 久久精品国产色蜜蜜麻豆| 亚洲人成电影网站久久| 亚洲欧美国产精品专区久久| 亚洲精品无码成人片久久|