重构-保护

重构-保护

重构前的代码,使用字符串处理状态
package  org.zsk.refact;

public   class  SystemPermission  {
    
private String state;
    
private boolean granted;
    
    
public final static String REQUESTED = "REQUESTED"
    
public final static String CLAIMED = "CLAIMED";
    
public final static String DENIED = "DENIED";
    
public final static String GRANTED = "GRANTED";

    
public SystemPermission() {
        
// TODO Auto-generated constructor stub
        state = REQUESTED;
        granted 
= false;
    }

    
    
public void claimed(){
        
if (state.equals(REQUESTED)){
            state 
= CLAIMED;
        }

    }

    
    
public void denied(){
        
if (state.equals(CLAIMED))
        state 
= DENIED;
    }

    
    
public void granted(){
        
if (state.equals(CLAIMED)){
            state 
= GRANTED;
            granted 
= true;
        }

    }

    
    
public boolean isGranted(){
        
return granted;
    }

    
    
public String getState(){
        
return state;
    }


    
/** *//**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        SystemPermission permission = new SystemPermission();
        System.out.println(permission.getState());
        
        
    }


}


重构后的代码,使用类来保护状态不会被吴改。
package  org.zsk.refact;

public   class  SystemPermissionRf1  {
        
private boolean granted;
        
private PermissionState permission;
        
//        public final static String REQUESTED = "REQUESTED"; 
//        public final static String CLAIMED = "CLAIMED";
//        public final static String DENIED = "DENIED";
//        public final static String GRANTED = "GRANTED";

        
public SystemPermissionRf1() {
            setState(PermissionState.REQUESTED);
            granted 
= false;
        }

        
        
public void claimed(){
            
if (getState().equals(permission.REQUESTED)){
                setState(permission.CLAIMED);
            }

        }

        
        
public void denied(){
            
if (getState().equals(permission.CLAIMED)){
                setState(permission.DENIED);
            }

        }

        
        
public void granted(){
            
if (getState().equals(permission.CLAIMED)){
                setState(permission.GRANTED);
                granted 
= true;
            }

        }

        
        
public boolean isGranted(){
            
return granted;
        }

        
        
private void setState(PermissionState permission){
            
this.permission = permission;
        }

        
        
public String getState(){
            
return permission.toString();
        }


        
        
/** *//**
         * 
@param args
         
*/

        
public static void main(String[] args) {
            
// TODO Auto-generated method stub
            SystemPermissionRf1 permission1 = new SystemPermissionRf1();
            System.out.println(permission1.getState());
            
            
        }

        
        
}





package  org.zsk.refact;


public   final   class  PermissionState {
    
private final String name;
    
    PermissionState(String name)
{
        
this.name = name;
    }

    
    
public String toString(){
        
return name;
    }

    
    
public final static PermissionState REQUESTED = new PermissionState("REQUESTED");
    
public final static PermissionState CLAIMED = new PermissionState("CLAIMED");
    
public final static PermissionState DENIED = new PermissionState("DENIED");
    
public final static PermissionState GRANTED = new PermissionState("GRANTED");
}
    

你可能感兴趣的:(重构-保护)