Step by step. NET code refactoring study Note 6

Source: Internet
Author: User

1. Extract Class (refining Class)

Motivation)

A class should be a clear abstract to deal with some clear responsibilities. However, in actual work, the class will continue to grow and expand. This class will become too complex, and soon the class will become messy.

Example

Public class Person
{
Private string _ name;
Private string _ officeAreaCode;
Private string _ officeNumber;

Public string Name
{
Get {return _ name ;}
Set {_ name = value ;}
}

Public string OfficeAreaCode
{
Get {return _ officeAreaCode ;}
Set {_ officeAreaCode = value ;}
}

Public string OfficeNumber
{
Get {return _ officeNumber ;}
Set {_ officeNumber = value ;}
}

Public string TelephoneNumber
{
Get {return "(" + _ officeAreaCode + ")" + _ officeNumber ;}
}
}

 

Change

Public class Person
{
Private string _ name;
Private Telephone _ telephoneNumber;

Public Telephone TelephoneNumber
{
Get {return _ telephoneNumber ;}
Set {_ telephoneNumber = value ;}
}

Public string Name
{
Get {return _ name ;}
Set {_ name = value ;}
}

Public string TemphoneNumber
{
Get {return _ telephoneNumber. TelephoneNumber ;}
}

}
Public class Telephone
{
Private string _ officeAreaCode;
Private string _ officeNumber;
Public string OfficeAreaCode
{
Get {return _ officeAreaCode ;}
Set {_ officeAreaCode = value ;}
}

Public string OfficeNumber
{
Get {return _ officeNumber ;}
Set {_ officeNumber = value ;}
}

Public string TelephoneNumber
{
Get {return "(" + _ officeAreaCode + ")" + _ officeNumber ;}
}
}

 

Ii. Inline Class (associating classes)

Motivation)

A class is no longer responsible and has no reason to exist independently. It moves all the features of the class to another class and then removes the original class.

Example

Public class User
{
Private int _ ID;
Private string _ name;

Public int ID
{
Get {return _ ID ;}
Set {_ ID = value ;}
}

Public string Name
{
Get {return _ name ;}
Set {_ name = value ;}
}


}
Public class UserInfo
{
Private int _ ID;
Private string _ Age;

Public string ID
{
Get {return _ ID ;}
Set {_ ID = value ;}
}

Public string Age
{
Get {return _ Age ;}
Set {_ Age = value ;}
}
}

 

Change

Public class User
{
Private int _ ID;
Private string _ name;
Private string _ Age;

Public int ID
{
Get {return _ ID ;}
Set {_ ID = value ;}
}

Public string Name
{
Get {return _ name ;}
Set {_ name = value ;}
}

Public string Age
{
Get {return _ Age ;}
Set {_ Age = value ;}
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.