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();
}
}
//
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();
}
}
相关文章推荐
- 《Agile Principles, Patterns, and Practices in C#》(chap 1 - chap 10)读后感
- Agile Principles, Patterns, and Practices in C#
- Agile Principles,Patterns,And Practices in C# 之敏捷软件开发宣言
- Agile Principles,Patterns,And Practices in C# 之Strategy Pattern
- Agile Principles,Patterns,And Practices in C# 之保龄球积分
- Agile Principles, Patterns, and Practices in C#
- Applying Domain-Driven Design and Patterns(ADDDP) With examples in C# and .NET
- 期待已久的一本电子书出来了:Applying Domain-Driven Design and Patterns: With Examples in C# and .NET
- Data Structures And Algorithms With Object-Oriented+Design+Patterns in C#
- Applying Domain-Driven Design and Patterns: With Examples in C# and .NET
- Applying Domain-Driven Design and Patterns(ADDDP) With examples in C# and .NET
- [Study Note] Patterns and Practices in NHibernate
- Applying Domain-Driven Design and Patterns(ADDDP) With examples in C# and .NET
- Thread and Sync In C# (C#中的线程与同步)
- List of Microsoft patterns and practices for Application Architecture and Design
- Secure Coding Best Practices for Memory Allocation in C and C++
- Professional ASP.NET 3.5: In C# and VB
- [coding] Threading differences in C# and Java
- Using Weka and Libsvm with C# in Visual Studio
- 7 Easy Steps to Learn C#: Silverlight C# Compiler and Loading DLLs from a server at runtime in Silverlight