Step by step. NET code refactoring learning notes 8

Source: Internet
Author: User

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); changed

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

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 ();
} 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;
} Changed

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

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 ")};
} Changed

Private Dictionary <string, Customer> dicUserName = new Dictionary <string, Customer> ();

Public void GetCustomers ()
{
String [] UserName = {dicUserName. TryGetValue ("Spring Yang"), dicUserName. TryGetValue ("Lemon Car "),

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.