委任された使用シーン
整数バブルソートの例を挙げます.
それからもう一つの社員の給料がバブルで並べ替えられた例を挙げます.
この二つの例
共通点:並べ替えアルゴリズムが同じ
相違点:比較オブジェクトの相違/比較アルゴリズムの相違
コード再利用を実現し、oop思想を体現するために、以下の再構築を行う.
再構築?
ここではSortDelエージェントを導入する、2つのオブジェクトを比較するためのものであり、整数クラスのバブルソートを行う際にこのエージェントが整数クラスを指す比較方法である、カスタム職員クラスのバブルソートを行う際にカスタム職員クラスを指す比較方法である、バブルソートアルゴリズムを抽象化する汎用的な方法となっている.今後,学生単科成績/学生総合成績のバブルソートを行う必要がある場合は,1つの学生クラスを追加し,もう1つの単科成績比較の方法と1つの総合成績比較の方法を提供すればよい,バブルソートのアルゴリズムをもう一度書く必要がなく,コードの再利用を実現し,OOPを体現する.
/// <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エージェントを導入する、2つのオブジェクトを比較するためのものであり、整数クラスのバブルソートを行う際にこのエージェントが整数クラスを指す比較方法である、カスタム職員クラスのバブルソートを行う際にカスタム職員クラスを指す比較方法である、バブルソートアルゴリズムを抽象化する汎用的な方法となっている.今後,学生単科成績/学生総合成績のバブルソートを行う必要がある場合は,1つの学生クラスを追加し,もう1つの単科成績比較の方法と1つの総合成績比較の方法を提供すればよい,バブルソートのアルゴリズムをもう一度書く必要がなく,コードの再利用を実現し,OOPを体現する.