Step by step. NET code refactoring study note 9

Source: Internet
Author: User
Tags in domain

1. Duplicate Observed Data (copy [monitored Data])

Motivation)

Copy the data to a domain object. Create an Observer mode to synchronize and control repeated data in domain object and GUI object (sync ).

Example

Html "> step-by-step. NET Design Mode Study note 12, Observer (Observer Mode)

2. Change Unidirectional Association to Bidirectional (Change one-way Association to two-way Association)

Motivation)

Add a counter pointer and enable the modifiers function to update two connections at the same time.

Example

Public class Order
{
Private Customer _ customer;

Public Customer NewCustomer
{
Get {return _ customer ;}
Set {_ customer = value ;}
}
}

Public class Customer
{
}

Change

Public class Order
{
Public List <Customer> CustomerList = new List <Customer> ();

Public void AddCustomer (Customer arg)
{
Arg. orderlist. Add (this );
CustomerList. Add (arg );
}

Public void RemoveCustomer (Customer arg)
{
Arg. orderlist. Remove (this );
CustomerList. Remove (arg );
}
}

Public class Customer
{
Public List <Order> orderlist = new List <Order> ();
Public void AddOrder (Order arg)
{
Arg. CustomerList. Add (this );
Orderlist. Add (arg );
}
Public void RemoveOrder (Order arg)
{
Arg. CustomerList. Remove (this );
Orderlist. Remove (arg );
}
}

 

3. Change Bidirectional Association to Unidirectional (Change Bidirectional to Unidirectional)

Motivation)

There is a two-way association between the two classes, but one class does not need the features of the other class.

Example

Public class Order
{
Public List <Customer> CustomerList = new List <Customer> ();

Public void AddCustomer (Customer arg)
{
Arg. orderlist. Add (this );
CustomerList. Add (arg );
}

Public void RemoveCustomer (Customer arg)
{
Arg. orderlist. Remove (this );
CustomerList. Remove (arg );
}
}

Public class Customer
{
Public List <Order> orderlist = new List <Order> ();
Public void AddOrder (Order arg)
{
Arg. CustomerList. Add (this );
Orderlist. Add (arg );
}
Public void RemoveOrder (Order arg)
{
Arg. CustomerList. Remove (this );
Orderlist. Remove (arg );
}
}

 

Change

Public class Order
{
Private Customer _ customer;

Public Customer NewCustomer
{
Get {return _ customer ;}
Set {_ customer = value ;}
}
}

Public class Customer
{
}

 

Iv. Replace Magic Number with Symbolic Constant (use a Symbolic or literal Constant to obtain the Number of tokens Magic)

Motivation)

Create a constant, name it according to its meaning, and replace the preceding literal value with this constant.

Example

Public double PotentialEnergy (double mass, double height)
{
Return mass * 9.51 * height;
}

 

Change

Private double GRAVITATIONAL_CONSTANT = 9.51;
Public double PotentialEnergy (double mass, double height)
{
Return mass * GRAVITATIONAL_CONSTANT * height;
}

 

 

5. Encapsulate Field (encapsulation value range)

Motivation)

Declare it as private and provide the corresponding accessors)

Example

Public string _ name;

 

Change

Private string _ name;

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

 

6. Replace Type Code with Class (Replace Type Code with Class)

Motivation)

Replace the Value type code with a new class)

Example

Public class Person
{
Public int O = 0;
Public int A = 1;
Public int B = 2;
& N

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.