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