重构--改善既有代码的设计读书笔记 第一章(2)

重构--改善既有代码的设计读书笔记 第一章(2)

运用多态(polymorphism)取代与价格相关的条件逻辑
在另一个对象的属性基础上运行switch语句,并不是什么好主意。如果不得不使用,也应该在对象自己的数据上而不是在别人的数据上使用。选择对象之间的传递关系的时候,应当考虑选择将稳定的对象的属性传递给易变的对象(如书中的将租期长度来自Rental(稳定,不易变)传递给Movie(不稳定,易变))。
对于类A和类B,如果A中存在因B而变化的属性或方法体,则将它们移动到B中,A中只保留移动后B暴露给A的接口(或方法)。
终于.......我们来到继承(inheritance)
此部分包含的重构方法:
Self Encapsulate Field:自封装域。
Move Method:移动方法。
Replace Conditional with Polymorphism:用多态替换条件选择。
Replace Type Code with State/Strategy:在这个方法中使用了上面三个方法作为步骤。即用状态、策略替换代码,将与类型相依的行为搬移到模式内。在使用它的时候,多使用对于以类型相依的行为进行Self Encapsulate Field作为第一步骤。从而确保任何时候都通过getter和setter两个函数来运行这些行为。第二步通常采用Move Method方法,即把代码从超类的宿主中搬移到子类中去。第三步采用Replace Conditional with Polymorphism方法,将switch,if等条件分支语句转变为多态形式。
下面是一个小实验:
一、重构之前的代码:

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 ;
        }
    }

}

结语
重构的节奏:测试、小修改、测试、小修改、测试、小修改。。。
正是这种节奏让重构得以快速而安全的前进。
 


 

你可能感兴趣的:(重构--改善既有代码的设计读书笔记 第一章(2))