一个实际项目的重构例子

为了加深大家对重构的直观认识,我在这里贴上自己开发项目的一个实际例子.
如果能让大家对重构这项优化代码的技术多一点认同,心满意足.

代码下载:here

重构前:

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Drawing;
using  System.Data;
using  System.Text;
using  System.Windows.Forms;

using  Pinnacle.Prison.Model;
using  Pinnacle.Prison.Client;
using  Pinnacle.Prison.IServer;

namespace  Pinnacle.Prison.PrisonWin
{
   
public  partial  class  Logout : UserControl
   {
      String icCardStr 
=   null ;
      TICCard icCard 
=   new  TICCard();
      GroupInfo groupTemp 
=   null ;
      List
< VisitorInfo >  listVisitor  =   null ;

      
public  Logout()
      {
         InitializeComponent();
         btnPass.Enabled 
=   false ;
      }

        
///   <summary>
        
///  全组人资料处理
        
///   </summary>
        
///   <param name="group"></param>
         internal   void  HandleGroup(GroupInfo group)
        {
            
if  (group  !=   null )
            {
                
this .icCardStr  =  group.ICCard.ICCardValue;
                
this .groupTemp  =  group;
                
if  (group.ICCard.ICCardOwner  ==  ICCardOwner.GuiderPliceMan)
                    listVisitor 
=  group.Visitors;

                ShowInfo(group);

            }
        }

      
//  验证,注销IC卡状态
       private   void  btnPass_Click(  object  sender, EventArgs e )
      {
         Boolean icCardStatus 
=   false ;

         
if (groupTemp.ICCard.ICCardOwner  ==  ICCardOwner.Visitor  &&  groupTemp.ICCard.Status  ==  ICCardStatus.OutPrison) // 访问者
         {
            
if ( ! string .IsNullOrEmpty( txtIDNumber.Text )) // 判断带头警员有没打卡,如果有。。。
            {
               
for ( int  i  =   0  ; i  <  listVisitor.Count ; i ++ )
               {
                  
if (dgVisitorList.Rows[i].Cells[ 3 ].Value.ToString()  !=  groupTemp.Visitors[ 0 ].IdNumber) // 判断打卡人在不在访问人员列表中,如果不在。。。
                  {
                     lblICCardMsg.Text 
=   " 验证失败!查无此人!!! " ;
                     btnPass.Enabled 
=   false ;
                  }
                  
if (dgVisitorList.Rows[i].Cells[ 3 ].Value.ToString()  ==  groupTemp.Visitors[ 0 ].IdNumber) // 判断打卡人在不在访问人员列表中,如果在。。。
                  {
                     icCardStatus 
=  icCard.UpICCardStatus( icCardStr ); // 发送验证注销请求
                      if (icCardStatus  ==   true ) // 如果成功。。。
                     {
                        listVisitor.Remove( listVisitor[i] );
                        i
-- ;
                        BindingSource bindingsource 
=   new  BindingSource();
                        bindingsource.DataSource 
=  listVisitor;
                        dgVisitorList.AutoGenerateColumns 
=   false ;
                        dgVisitorList.DataSource 
=  bindingsource;
                        clearInfo( groupTemp );
                        
return ;
                     }
                  }
               }
            }
                
else // 判断带头警员有没打卡,如果没有。。。
            {
               lblICCardMsg.Text 
=   " 错误!请带头警员先打卡!!! " ;
               btnPass.Enabled 
=   false ;
            }
         }
         
else   if (groupTemp.ICCard.ICCardOwner  ==  ICCardOwner.CommonPliceMan  &&  groupTemp.ICCard.Status  ==  ICCardStatus.OutPrison) // 普通警员
         {
            icCardStatus 
=  icCard.UpICCardStatus( icCardStr );
            
if (icCardStatus  ==   true )
               clearInfo( groupTemp );
            
else
            {
               lblICCardMsg.Text 
=   " 验证失败!请重试!!! " ;
               btnPass.Enabled 
=   true ;
            }
         }
         
else
         {
            lblICCardMsg.Text 
=   " 验证失败!请重试!!! " ;
            btnPass.Enabled 
=   true ;
         }
// end if
      }

        
// 显示IC卡和身份证资料界面
         private   void  ShowInfo(GroupInfo group)
        {
            
if  (group.ICCard.ICCardOwner  ==  ICCardOwner.Visitor  &&  group.ICCard.Status  ==  ICCardStatus.OutPrison) // 访问者
            {
                showVisitorInfo(group);

                
for  ( int  i  =   0 ; i  <  dgVisitorList.RowCount; i ++ )
                {
                    
if  (dgVisitorList.Rows[i].Cells[ 0 ].Value.ToString()  ==  txtVisitorName.Text)
                        dgVisitorList.Rows[i].DefaultCellStyle.BackColor 
=  Color.Green;
                }
                txtProperty.Text 
=   " 访问者 " ;
                lblVisitorMsg.Text 
=   "" ;
                lblICCardMsg.Text 
=   " 资料读取成功!请验证! " ;
                txtState.Text 
=   " 使用中 " ;
                txtLogOut.Text 
=   " 未注销 " ;
                btnPass.Enabled 
=   true ;
            }
            
else   if  (group.ICCard.ICCardOwner  ==  ICCardOwner.CommonPliceMan  &&  group.ICCard.Status  ==  ICCardStatus.OutPrison) // 普通警员
            {
                showPliceManInfo(group);

                txtProperty.Text 
=   " 工作警员 " ;
                txtState.Text 
=   " 使用中 " ;
                txtLogOut.Text 
=   " 未注销 " ;
                btnPass.Enabled 
=   true ;
            }
            
else   if  (group.ICCard.ICCardOwner  ==  ICCardOwner.GuiderPliceMan  &&  group.ICCard.Status  ==  ICCardStatus.OutPrison) // 带头警员
            {
                showPliceManInfo(group);

                dgVisitorList.AutoGenerateColumns 
=   false ;
                dgVisitorList.DataSource 
=  listVisitor;

                grpIDData.Text 
=   " 带头警员身份证资料 " ;
                txtProperty.Text 
=   " 带头警员 " ;
                lblVisitorMsg.Text 
=   " 其他访问人员请打卡! " ;
                txtState.Text 
=   " 使用中 " ;
                txtLogOut.Text 
=   " 未注销 " ;
                lblVisitorsMsg.Text 
=   "" ;
                btnPass.Enabled 
=   false ;

                clearVisitroInfo();
            }
// end if
            lblICCardMsg.Text  =   "" ;
        }
        
// 显示警员身份资料
         private   void  showPliceManInfo(GroupInfo group)
        {
            txtPliceNum.Text 
=  group.PliceMan.PliceManId.ToString();
            txtName.Text 
=  group.PliceMan.Name;
            txtSex.Text 
=  group.PliceMan.Sex;
            txtFolk.Text 
=  group.PliceMan.Folk;
            txtYear.Text 
=  group.PliceMan.Birthday;
            txtAddress.Text 
=  group.PliceMan.Address;
            txtIDNumber.Text 
=  group.PliceMan.IdNumber;
            txtDepartment.Text 
=  group.PliceMan.SignDepartment;
            txtValidTerm.Text 
=  group.PliceMan.ValidTerm;
            imgPhoto.Image 
=  group.PliceMan.Photo;
        }
        
// 显示访问者身份资料
         private   void  showVisitorInfo(GroupInfo group)
        {
            txtVisitorName.Text 
=  group.Visitors[ 0 ].Name;
            txtVisitorSex.Text 
=  group.Visitors[ 0 ].Sex;
            txtVisitorFolk.Text 
=  group.Visitors[ 0 ].Folk;
            txtVisitorYear.Text 
=  group.Visitors[ 0 ].Birthday;
            txtVisitorAddress.Text 
=  group.Visitors[ 0 ].Address;
            txtVisitorIDNumber.Text 
=  group.Visitors[ 0 ].IdNumber;
            txtVisitorDepartment.Text 
=  group.Visitors[ 0 ].SignDepartment;
            txtVisitorValidTerm.Text 
=  group.Visitors[ 0 ].ValidTerm;
            imgVisitorPhoto.Image 
=  group.Visitors[ 0 ].Photo;
        }


      
//  清除资料界面显示
         private   void  clearInfo(GroupInfo group)
        {
            
if  (group.ICCard.ICCardOwner  ==  ICCardOwner.Visitor) // 访问者
            {
                clearVisitroInfo();
                lblICCardMsg.Text 
=   " 验证通过! " ;
                
if  (dgVisitorList.Rows.Count  !=   0 )
                    lblICCardMsg.Text 
=   " 验证通过!请其他访问人员继续打卡! " ;
            }
            
else // 警员
            {
                clearIDInfo();
                lblICCardMsg.Text 
=   " 验证通过! " ;
            }
            txtProperty.Text 
=   "" ;
            txtState.Text 
=   " 未使用 " ;
            txtLogOut.Text 
=   " 已注销 " ;
            btnPass.Enabled 
=   false ;
        }
        
// 清除警员身份资料
         private   void  clearIDInfo()
        {
            txtPliceNum.Text 
=   "" ;
            txtName.Text 
=   "" ;
            txtSex.Text 
=   "" ;
            txtFolk.Text 
=   "" ;
            txtYear.Text 
=   "" ;
            txtAddress.Text 
=   "" ;
            txtIDNumber.Text 
=   "" ;
            txtDepartment.Text 
=   "" ;
            txtValidTerm.Text 
=   "" ;
            imgPhoto.Image 
=   null ;
        }
        
// 清除访问者身份资料
         private   void  clearVisitroInfo()
        {
            txtVisitorName.Text 
=   "" ;
            txtVisitorSex.Text 
=   "" ;
            txtVisitorFolk.Text 
=   "" ;
            txtVisitorYear.Text 
=   "" ;
            txtVisitorAddress.Text 
=   "" ;
            txtVisitorIDNumber.Text 
=   "" ;
            txtVisitorDepartment.Text 
=   "" ;
            txtVisitorValidTerm.Text 
=   "" ;
            imgVisitorPhoto.Image 
=   null ;
        }
   }
}


重构后:

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Drawing;
using  System.Data;
using  System.Text;
using  System.Windows.Forms;

using  Pinnacle.Prison.Model;
using  Pinnacle.Prison.Client;
using  Pinnacle.Prison.IServer;

namespace  Pinnacle.Prison.PrisonWin
{
   
public  partial  class  Logout : UserControl
   {
      String icCardStr 
=   null ;
      TICCard icCard 
=   new  TICCard();
      GroupInfo groupTemp 
=   null ;
      List
< VisitorInfo >  listVisitor  =   null ;

      
public  Logout()
      {
         InitializeComponent();
         btnPass.Enabled 
=   false ;
         dgVisitorList.AutoGenerateColumns 
=   false ;
      }

      
///   <summary>
      
///  全组人资料处理
      
///   </summary>
      
///   <param name="group"></param>
       internal   void  HandleGroup( GroupInfo group )
      {
         
if (group  !=   null )
         {
            
this .icCardStr  =  group.ICCard.ICCardValue;

            
this .groupTemp  =  group;

            
if (group.ICCard.ICCardOwner  ==  ICCardOwner.GuiderPliceMan)
               listVisitor 
=  group.Visitors;

            ShowInfo( group );
         }
      }





      


      
//  验证,注销IC卡状态
       private   void  btnPass_Click(  object  sender, EventArgs e )
      {    
         
// 访问者
          if (IsVisitor( groupTemp.ICCard ))
         {
            
// 带头警员没打卡,
             if (txtProperty.Text  !=   " 带头警员 " )
            {
               ShowMsgHandle( 
" 错误!请带头警员先打卡!!! " false  );
               
return ;
            }

            
int  visitorIndex  =   0 ;

            
// 如果带头警员有打卡
             for ( int  i  =   0  ; i  <  listVisitor.Count ; i ++ )
            {
               
// 打卡人在访问人员列表中
                if (dgVisitorList.Rows[i].Cells[ 5 ].Value.ToString()  ==  groupTemp.Visitors[ 0 ].IdNumber)
               {
                  visitorIndex 
=  i;
               }
            }
// --------end for-------------

            
// 如果打卡人不在访问人员列表中
             if (visitorIndex  ==   0 )
            {
               ShowMsgHandle( 
" 验证失败!查无此人!!! " false  );
               
return ;
            }

            
// ----a.打卡人在访问人员列表中
            
// 验证注销失败
             if (icCard.UpICCardStatus( icCardStr )  ==   false )
            {
               ValidateFailed();
               
return ;
            }

            
// 验证成功
            listVisitor.Remove( listVisitor[visitorIndex] );

            
// 重新绑定数据
            ReBindGridView( listVisitor );

            
// 清除
            ClearInfo( groupTemp.ICCard.ICCardOwner );           

            
// ----a

         }
         
else   if (IsCommonPliceMan( groupTemp.ICCard )) // 普通警员
         {
            
// 验证注销失败
             if (icCard.UpICCardStatus( icCardStr )  ==   false )
            {
               ValidateFailed();
               
return ;
            }

            
// 清除
            ClearInfo( groupTemp.ICCard.ICCardOwner );

         }
         
else
         {
            ValidateFailed();
         }
// end if
      }

      
// 显示IC卡和身份证资料界面
       private   void  ShowInfo( GroupInfo group )
      {
         
if (IsVisitor( group.ICCard )) // 访问者
         {
            ShowVisitorInfo( group.Visitors[
0 ] );

            
for ( int  i  =   0  ; i  <  dgVisitorList.RowCount ; i ++ )
            {
               
if (dgVisitorList.Rows[i].Cells[ 0 ].Value.ToString()  ==  txtVisitorName.Text)
                  dgVisitorList.Rows[i].DefaultCellStyle.BackColor 
=  Color.Green;
            }

            ShowMsgHandle( 
" 资料读取成功!请验证! " true  );
         }
         
else   if (IsCommonPliceMan( group.ICCard )) // 普通警员
         {
            ShowPliceManInfo( group.PliceMan );            
            txtProperty.Text 
=   " 普通警员 " ;
            txtState.Text 
=   " 使用中 " ;
            btnPass.Enabled 
=   true ;
         }
         
else   if (IsGuiderPliceMan( group.ICCard )) // 带头警员
         {
            ShowPliceManInfo( group.PliceMan );
            txtProperty.Text 
=   " 带头警员 " ;
            txtState.Text 
=   " 使用中 " ;
            grpIDData.Text 
=   " 带头警员信息 " ;
         
            dgVisitorList.DataSource 
=  listVisitor; 

            ShowMsgHandle( 
" 其他访问人员请打卡! " false  );

            ClearVisitroInfo();
         }
// end if
      }

      
// 显示警员身份资料
       private   void  ShowPliceManInfo( PliceManInfo pliceMan )
      {
         txtName.Text 
=  pliceMan.Name;
         txtSex.Text 
=  pliceMan.Sex;
         txtFolk.Text 
=  pliceMan.Folk;
         txtYear.Text 
=  pliceMan.Birthday;
         txtAddress.Text 
=  pliceMan.Address;
         txtIDNumber.Text 
=  pliceMan.IdNumber;
         txtDepartment.Text 
=  pliceMan.SignDepartment;
         txtValidTerm.Text 
=  pliceMan.ValidTerm;
         imgPhoto.Image 
=  pliceMan.Photo;
         txtPrison.Text 
=  pliceMan.PrisonID.ToString(); // 显示警员的所在监区
      }

      
// 显示访问者身份资料
       private   void  ShowVisitorInfo( VisitorInfo visitor )
      {
         txtVisitorName.Text 
=  visitor.Name;
         txtVisitorSex.Text 
=  visitor.Sex;
         txtVisitorFolk.Text 
=  visitor.Folk;
         txtVisitorYear.Text 
=  visitor.Birthday;
         txtVisitorAddress.Text 
=  visitor.Address;
         txtVisitorIDNumber.Text 
=  visitor.IdNumber;
         txtVisitorDepartment.Text 
=  visitor.SignDepartment;
         txtVisitorValidTerm.Text 
=  visitor.ValidTerm;
         imgVisitorPhoto.Image 
=  visitor.Photo;
         txtVisitorPrison.Text 
=  visitor.PrisonID.ToString(); // 显示访问者访问的所在监区
         txtVisitorProperty.Text  =   " 访问者 " ;
         txtVisitorState.Text 
=   " 使用中 " ;
      }


      
//  清除资料界面显示
       private   void  ClearInfo( ICCardOwner owner )
      {
         String msg 
=   "" ;
         
if (owner  ==  ICCardOwner.Visitor) // 访问者
         {
            ClearVisitroInfo();
            txtVisitorProperty.Text 
=   "" ;
            txtVisitorState.Text 
=   " 已注销 " ;
            txtVisitorPrison.Text 
=   "" ;
            msg 
=   " 验证通过! " ;
            
if (dgVisitorList.Rows.Count  !=   0 )
               msg 
=   " 验证通过!请其他访问人员继续打卡! " ;
         }
         
else // 警员
         {
            ClearIDInfo();
            msg 
=   " 验证通过! " ;
            txtProperty.Text 
=   "" ;
            txtState.Text 
=   " 已注销 " ;
            txtPrison.Text 
=   "" ;
         }

         ShowMsgHandle( msg, 
false  );         
      }

      
// 清除警员身份资料
       private   void  ClearIDInfo()
      {
         txtName.Text 
=   "" ;
         txtSex.Text 
=   "" ;
         txtFolk.Text 
=   "" ;
         txtYear.Text 
=   "" ;
         txtAddress.Text 
=   "" ;
         txtIDNumber.Text 
=   "" ;
         txtDepartment.Text 
=   "" ;
         txtValidTerm.Text 
=   "" ;
         imgPhoto.Image 
=   null ;
      }

      
// 清除访问者身份资料
       private   void  ClearVisitroInfo()
      {
         txtVisitorName.Text 
=   "" ;
         txtVisitorSex.Text 
=   "" ;
         txtVisitorFolk.Text 
=   "" ;
         txtVisitorYear.Text 
=   "" ;
         txtVisitorAddress.Text 
=   "" ;
         txtVisitorIDNumber.Text 
=   "" ;
         txtVisitorDepartment.Text 
=   "" ;
         txtVisitorValidTerm.Text 
=   "" ;
         imgVisitorPhoto.Image 
=   null ;
      }

      
// 是否合法状态
       private   static   bool  IsRightStatus( ICCardStatus status )
      {
         
return  status  ==  ICCardStatus.OutPrison;
      }

      
// 是否访问者
       private   bool  IsVisitor( ICCardInfo icCard )
      {
         
return  icCard.ICCardOwner  ==  ICCardOwner.Visitor  &&  IsRightStatus( icCard.Status );
      }

      
// 显示提示信息
       private   void  ShowMsg(  string  msg )
      {
         lblMsg.Text 
=  msg;
      }

      
// 是否普通警员
       private   bool  IsCommonPliceMan( ICCardInfo icCard )
      {
         
return  icCard.ICCardOwner  ==  ICCardOwner.CommonPliceMan  &&  IsRightStatus( icCard.Status );
      }

      
// 是否带头警员
       private   bool  IsGuiderPliceMan( ICCardInfo icCard )
      {
         
return  icCard.ICCardOwner  ==  ICCardOwner.GuiderPliceMan  &&  IsRightStatus( icCard.Status );
      }

      
// 验证失败
       private   void  ValidateFailed()
      {
         ShowMsgHandle( 
" 验证失败!请重试!!! " true  );
      }

      
// 验证结果处理
       private   void  ShowMsgHandle( String msg, Boolean enablePassButton )
      {
         ShowMsg( msg );
         btnPass.Enabled 
=  enablePassButton;
      }

      
// 重新绑定数据
       private   void  ReBindGridView( List < VisitorInfo >  listVisitor )
      {
         BindingSource bindingsource 
=   new  BindingSource();
         bindingsource.DataSource 
=  listVisitor;
         dgVisitorList.DataSource 
=  bindingsource;
      }
   }
}

 

转载于:https://www.cnblogs.com/sinkzephyr/archive/2007/09/01/878288.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值