1. Introduce Foreign Method (introduces an additional function)
Motivation)
Create a function in the client class and use a server class entity as the first reference (argument ).
Example
DateTime newStart = DateTime. Now. AddDays (1 );
Change
Public DateTime NextDate ()
{
Return DateTime. Now. AddDays (1 );
}
Ii. Introduce Local Extension (introducing Local extensions)
Motivation)
Create a new class to include these additional functions. Make this extension a subclass (subclass) or wrapper (override class) of the source class ).
Example
Protected void Main ()
{
Computer _ computer;
StringBuilder strCom = new StringBuilder ();
StrCom. AppendLine ("your computer configuration is as follows :");
StrCom. AppendLine ("the main board is:" + _ computer. MainBoard ());
StrCom. AppendLine ("processor:" + _ computer. Cpu ());
StrCom. AppendLine ("video card is:" + _ computer. PhenoType ());
StrCom. AppendLine ("Memory is:" + _ computer. Memory ());
StrCom. AppendLine ("hard disk is:" + _ computer. HardDisk ());
StrCom. AppendLine ("Display is:" + _ computer. Display ());
StrCom. AppendLine ("assembled ");
Console. WriteLine (strCom. ToString );
}
Change
Protected void Main ()
{
Console. WriteLine (ShowComputerConfigure ());
}
Public string ShowComputerConfigure ()
{
Computer _ computer;
StringBuilder strCom = new StringBuilder ();
StrCom. AppendLine ("your computer configuration is as follows :");
StrCom. AppendLine ("the main board is:" + _ computer. MainBoard ());
StrCom. AppendLine ("processor:" + _ computer. Cpu ());
StrCom. AppendLine ("video card is:" + _ computer. PhenoType ());
StrCom. AppendLine ("Memory is:" + _ computer. Memory ());
StrCom. AppendLine ("hard disk is:" + _ computer. HardDisk ());
StrCom. AppendLine ("Display is:" + _ computer. Display ());
StrCom. AppendLine ("assembled ");
Return strCom. ToString ();
}
Iii. Self Encapsulate Field (Self-encapsulated value range)
Motivation)
Create a value/set function (getting/setting methods) for this value field, and only use these functions to access the value field.
Example
Public int _ low, _ high;
Public bool pair des (int arg)
{
Return arg >=_ low & arg <= _ high;
}
Change
Private int _ low, _ high;
Public int Low
{
Get {return _ low ;}
Set {_ low = value ;}
}
Public int High
{
Get {return _ high ;}
Set {_ high = value ;}
}
Public bool pair des (int arg)
{
Return arg> = Low & arg <= High;
}
Iv. Replace Data Value with Object (Replace Data Value with Object)
Motivation)
Convert a data item into an object
Example
Public class Customer
{
Private string _ name;
Public string Name
{
Get {return _ name ;}
Set {_ name = value ;}
}
}
Change
Public class Customer
{
Private string _ name;
Public string Name
{
Get {return _ name ;}
Set {_ name = value ;}
}
Public Customer (string name)
{
This. _ name = name;
}
}
Reference
String name = new Customer ("spring yang ");
5. Change Value to Referencce (Change the real Value object to the reference object)
Motivation)
Convert a value object into a reference object)
Example
Public void GetCustomers ()
{
String [] UserName = {new Customer ("Spring Yang"), new Customer ("Lemon Car"), new Customer ("Associated Coffee ")};
}
Change
Private Dictionary <string, Customer> dicUserName = new Dictionary <string, Customer> ();
Public void GetCustomers ()
{
& Nb