About the design pattern in. NET Framework -- The Application policy pattern is sorted by List

Source: Internet
Author: User

Simple sort

Sorting is common during programming. However, the most common use of. Net is to sort the generic List <T>. If T is a simple data type, it is very simple.

Copy codeThe Code is as follows:
Public List <int> SortSimpleList (List <int> list)
{
List. Sort ();
Return list;
}

This is also true for sorting of simple types such as string <T>. If the objects to be sorted are complicated, we know that List <T> sort () is implemented by fast sorting, the comparison results between items in the list need to be known for any sorting. If it is a simple int type, you can directly judge it. For objects that implement the IComparable interface, you can call CompareTo () to compare the size of items. Below is a fast sorting method.

Copy codeThe Code is as follows:
Void Sort <T> (T [] array, int left, int right, IComparer_sly <T> comparer) where T: IComparable
{
If (left <right)
{
T middle = array [(left + right)/2];
Int I = left-1;
Int j = right + 1;
While (true)
{
While (array [++ I]. CompareTo (middle) <0 );

While (array [-- j]. CompareTo (middle)> 0 );

If (I> = j)
Break;

T temp = array [I];
Array [I] = array [j];
Array [j] = temp;
}

Sort (array, left, I-1, comparer );
Sort (array, j + 1, right, comparer );
}
}

Problem

The first two cases can be sorted, but we cannot require all objects to be sorted to implement the IComparable interface. Even if each object can implement the IComparable interface, if you want to sort multiple fields in an object, such as a Student object, you sometimes want to sort by name, sometimes by score, or sometimes by age, how can this problem be solved?

According to the object-oriented thinking, we need to separate the changes and encapsulate the changes. What changes occur when we sort the List <T> is how to compare the size of the two objects, if we can take this algorithm out, sorting will be much simpler. No matter what sort, the algorithm is from. What we want to encapsulate is how to compare the size of two items, to achieve scalability, we need to follow another important principle of object-oriented design, for interface programming, rather than for implementation programming.

Compile a general List <T> sorting method

First, define an interface, which contains a method to compare the item size. It is passed in as a parameter during sorting, and of course its implementation class. With this idea, we can write a List <T> sorting method by ourselves.

Copy codeThe Code is as follows:
Public interface mparer_sly <T> {
Int Compare (T x, T y );
}

Then, in order to test, we add a package for the List <T> and write our own Sort method, which is also implemented by fast sorting internally. We are always confused about our change part-compare the size algorithm, and we will block it and pass it as a parameter

Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;

Namespace Test. Stategy
{Public class ListTest <T>
{
Public List <T> list = new List <T> ();
Public void Sort (IComparer_sly <T> comparer)
{
T [] array = list. ToArray ();
Int left = 0;
Int right = array. Length-1;
QuickSort (array, left, right, comparer );
List = new List <T> (array );
}

Private void QuickSort <S> (S [] array, int left, int right, IComparer_sly <S> comparer)
{
If (left <right)
{
S middle = array [(left + right)/2];
Int I = left-1;
Int j = right + 1;
While (true)
{
While (comparer. Compare (array [++ I], middle) <0 );

While (comparer. Compare (array [-- j], middle)> 0 );

If (I> = j)
Break;

S temp = array [I];
Array [I] = array [j];
Array [j] = temp;
}

QuickSort (array, left, I-1, comparer );
QuickSort (array, j + 1, right, comparer );
}
}
}
}

For example, we now have a Student entity.

Copy codeThe Code is as follows:
Public class Student
{
Public Student (int id, string name)
{
This. ID = id;
This. Name = name;
}
Public int ID {get; set ;}
Public string Name {get; set ;}
}

If you want to sort the List <T> composed of this object, you only need a class StudentComparer that implements IComparer_sly <Student> and internally implements the Compare () method for comparing the size of the object (), at the same time, we can add the increment or decrease sort control.

Copy codeThe Code is as follows:
Class StudentComparer: IComparer_sly <Student>
{
Private string expression;
Private bool isAscending;
Public StudentComparer (string expression, bool isAscending)
{
This. expression = expression;
This. isAscending = isAscending;
}

Public int Compare (Student x, Student y)
{
Object v1 = GetValue (x), v2 = GetValue (y );
If (v1 is string | v2 is string)
{
String s1 = (v1 = null )? "": V1.ToString (). Trim ());
String s2 = (v2 = null )? "": V2.ToString (). Trim ());
If (s1.Length = 0 & s2.Length = 0)
Return 0;
Else if (s2.Length = 0)
Return-1;
Else if (s1.Length = 0)
Return 1;
}

// Here, the system method is called, which is not implemented by yourself. In fact, it is difficult to compare the data sizes of two arbitrary projection types.
If (! IsAscending)
Return Comparer. Default. Compare (v2, v1 );
Return Comparer. Default. Compare (v1, v2 );
}

Private object GetValue (Student stu)
{
Object v = null;
Switch (expression)
{
Case "id ":
V = stu. ID;
Break;
Case "name ":
V = stu. Name;
Break;
Default:
V = null;
Break;
}
Return v;
}
}

Test whether to enable

Copy codeThe Code is as follows:
Static void Main (string [] args)
{
ListTest <Student> test = new ListTest <Student> ();
For (int I = 0; I <10; I ++)
{
Student stu = new Student (I, string. Format ("N _" + (9-i )));
Test. list. Add (stu );
}
Console. WriteLine ("metadata ");
For (int I = 0; I <test. list. Count; I ++)
{
Console. writeLine (string. format ("ID: {0}, Name: {1}", test. list [I]. ID, test. list [I]. name ));
}

Console. WriteLine ("increasing Name ");
Test. Sort (new StudentComparer ("name", true ));
For (int I = 0; I <test. list. Count; I ++)
{
Console. writeLine (string. format ("ID: {0}, Name: {1}", test. list [I]. ID, test. list [I]. name ));
}
}

View results

How to sort the sort of. NET List

Using ILSpy decompilation, you can see this. sort (0, this. count, null); and then dig into it, after a series of exception processing will call Array. sort <T> (this. _ items, index, count, comparer); this. _ items converts the List content to an array. It also goes through some column Exception Processing and calls the ArraySortHelper method. <T>. default. sort (array, index, length, comparer); it is similar to the method we wrote above, but Microsoft adds a lot of Exception Processing and algorithm optimization.

Rule Mode

After reading the above example, we can start to discuss our policy model. Policy patterns define a series of algorithms, encapsulate each algorithm, and make them replaceable. The rule mode allows algorithms to change independently of customers who use it. (Original article: The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it .)

This mode involves three roles:

Context Role: Hold a reference to the Strategy class. Abstract Policy (Strategy) Role: This is an abstract role, usually implemented by an interface or abstract class. This role provides all the interfaces required for specific policy classes. ConcreteStrategy: encapsulates related algorithms or actions.

I believe that you can easily map the class in the above example to the role in the policy mode. The IComparer interface is our abstract policy role, ListTest <T>The class holds an abstract policy reference as an environment (in the Sort method, you can actually define an interface as a Class Attribute and assign a value to the constructor, but it is not suitable for this scenario, after all, not all lists need to be sorted and cannot be forced to accept an interface that may not be used. Of course, it is appropriate for each instance to use a certain policy ), undoubtedly, the class that implements the IComparer abstract policy is the specific policy.

Use Cases

The rule mode is easy to understand, but we can use it to better understand the encapsulation changes and the object-oriented design principles for interface programmers. Let's take a look at when we will use the policy mode.

1. Multiple classes only differ in performance behavior. You can use the Strategy Mode to dynamically select the behavior to be executed during running.

2. Different policies (algorithms) must be used in different situations. These policies have unified interfaces.

3. Hiding implementation details of specific policies (algorithms) from customers is completely independent of each other.

Advantages and disadvantages of the Rule Mode

Advantages:

1. It provides an alternative to inheritance, and maintains the advantages of inheritance (code reuse) and is more flexible than inheritance (algorithms are independent and can be expanded at will ).

2. Use combinations to avoid the use of multi-condition transfer statements in the program, making the system more flexible and easy to expand.

3. comply with most GRASP principles and common design principles, with high cohesion and low coupling.

Disadvantages:

1. Because each specific policy class generates a new class, the number of classes to be maintained by the system is increased.

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.