您的位置:首页 > 编程语言 > C#

Agile Principles,Patterns,And Practices in C# 之OCP源代码

2008-08-11 10:06 459 查看
Code

//

public interface Shape

{

void Draw();

}

public class Circle:Shape

{

public void Draw()

{Console.WriteLine("Draw Circle");}

}

public class Square:Shape

{

public void Draw()

{Console.WriteLine("Draw Square");}

}

public class MyComparer:IComparer

{

private Hashtable typelist = new Hashtable();

public void Add(Type type,int order)

{ typelist.Add(type, order); }

#region IComparer 成员

public int Compare(object x, object y)

{

int i1 = (int)typelist[x.GetType()];

int i2 = (int)typelist[y.GetType()];

//if it store the value type ,it don't implement the interface IComparable

return i1.CompareTo(i2);

}

#endregion

}

public class App

{

static void Main()

{

MyComparer compare = new MyComparer();

compare.Add(typeof(Circle), 1);

compare.Add(typeof(Square), 2);

ArrayList list = new ArrayList();

list.Add(new Square());

list.Add(new Circle());

list.Add(new Square());

list.Sort(compare);

foreach (Shape shape in list)

{

shape.Draw();

}

Console.Read();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: