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