委托的使用场景

举一个整数冒泡排序的例子:

/// <summary>
/// 整数排序
/// </summary>
class SortInt
{
/// <summary>
/// 比较大小
/// </summary>
/// <param name="i1"></param>
/// <param name="i2"></param>
/// <returns></returns>
public bool compare(int i1, int i2)
{
if (i1 > i2)
{
return true;
}
else
{
return false;
}
}

/// <summary>
/// 排序
/// </summary>
/// <param name="tosort"></param>
public void sort(int[] tosort)
{
for (int i = 0; i < tosort.Length; i++)
{
for (int j = 0; j < tosort.Length - (i + 1); j++)
{
if (compare(tosort[j], tosort[j + 1]))
{
int temp = tosort[j];
tosort[j] = tosort[j + 1];
tosort[j + 1] = temp;
}
}
}
}
}

class Program
{
static void Main(string[] args)
{
int[] tosortint = new int[] { 8, 7, 6, 5 };
SortInt si = new SortInt();
si.sort(tosortint);
for (int i = 0; i < tosortint.Length; i++)
{
Console.WriteLine(tosortint[i]);
}
}
}


然后再举一个职员薪水冒泡排序的例子

/// <summary>
/// 职员
/// </summary>
class Employee
{
private int id;
private string name;
private double salary;

public Employee(int id, string name, double salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}

public int ID
{
get { return id; }
set { id = value; }
}

public string Name
{
get { return name; }
set { name = value; }
}

public double Salary
{
get { return salary; }
set { salary = value; }
}
}

/// <summary>
/// 职员薪水排序
/// </summary>
class SortEmployee
{
public bool compare(Employee e1, Employee e2)
{
if (e1.Salary > e2.Salary)
{
return true;
}
else
{
return false;
}
}

public void sortEmployee(Employee[] tosort)
{
for (int i = 0; i < tosort.Length; i++)
{
for (int j = 0; j < tosort.Length - (i + 1); j++)
{
if (compare(tosort[j], tosort[j + 1]))
{
Employee e = tosort[j];
tosort[j] = tosort[j + 1];
tosort[j + 1] = e;
}
}
}
}
}

class Program
{
static void Main(string[] args)
{
Employee[] tosortemployee = new Employee[] { new Employee(2, "zh", 600), new Employee(1, "pm", 200) };
SortEmployee se = new SortEmployee();
se.sortEmployee(tosortemployee);
for (int i = 0; i < tosortemployee.Length; i++)
{
Console.WriteLine(tosortemployee[i].Salary);
}
}
}


这两个例子
相同之处:排序算法相同
不同之处:比较对象不同/比较算法不同
为了实现代码重用,体现oop思想,我们进行如下重构:
class Program
{
static void Main(string[] args)
{
object[] tosortint = new object[] { 8, 7, 6, 5 };
SortInt si = new SortInt();
SortDel sd = new SortDel(si.compare);
Sort s = new Sort();
s.sort(tosortint, sd);
for (int i = 0; i < tosortint.Length; i++)
{
Console.WriteLine(tosortint[i]);
}

Console.WriteLine("*******************************************************");

object[] tosortemployee = new object[] { new Employee(2, "zh", 600), new Employee(1, "pm", 200) };
SortEmployee se = new SortEmployee();
sd = new SortDel(se.compare);
s = new Sort();
s.sort(tosortemployee, sd);
for (int i = 0; i < tosortemployee.Length; i++)
{
Console.WriteLine(((Employee)tosortemployee[i]).Salary);
}
}
}

/// <summary>
/// 整数比较
/// </summary>
class SortInt
{
public bool compare(object o1, object o2)
{
int i1 = Convert.ToInt32(o1);
int i2 = Convert.ToInt32(o2);
if (i1 > i2)
{
return true;
}
else
{
return false;
}
}
}


/// <summary>
/// 职员
/// </summary>
class Employee
{
private int id;
private string name;
private double salary;

public Employee(int id, string name, double salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}

public int ID
{
get { return id; }
set { id = value; }
}

public string Name
{
get { return name; }
set { name = value; }
}

public double Salary
{
get { return salary; }
set { salary = value; }
}
}

/// <summary>
/// 职员薪水比较
/// </summary>
class SortEmployee
{
public bool compare(object o1, object o2)
{
Employee e1 = (Employee)o1;
Employee e2 = (Employee)o2;
if (e1.Salary > e2.Salary)
{
return true;
}
else
{
return false;
}
}
}

/// <summary>
/// 比较代理
/// </summary>
/// <param name="o1"></param>
/// <param name="o2"></param>
/// <returns></returns>
public delegate bool SortDel(object o1, object o2);

/// <summary>
/// 通用排序
/// </summary>
class Sort
{
public void sort(object[] tosort, SortDel sd)
{
for (int i = 0; i < tosort.Length; i++)
{
for (int j = 0; j < tosort.Length - (i + 1); j++)
{
if (sd(tosort[j], tosort[j + 1]))
{
object e = tosort[j];
tosort[j] = tosort[j + 1];
tosort[j + 1] = e;
}
}
}
}
}

重构?
在此引入了SortDel代理,用来进行比较两个对象,在进行整数类冒泡排序时此代理指向整数类的比较方法,而在进行自定义职员类的冒泡排序时指向自定义职员类的比较方法,从而使冒泡排序算法被抽象出来,成为一个通用的方法.今后再需要进行学生单科成绩/学生综合成绩的冒泡排序时,只需添加一个学生类,再提供一个单科成绩比较的方法和一个综合成绩比较的方法,就可以了,不需再把冒泡排序的算法再写一遍,实现了代码的重用,体现了OOP.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值