xyzjr 发表于 2015-6-17 09:02:03

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

Code
//
public interface Shape
{
    void Draw();
}
public class Circle:Shape
{
    public void Draw()
    {Console.WriteLine("Draw Circle");}
}
public classSquare:Shape
{
    public void Draw()
    {Console.WriteLine("Draw Square");}
}

public class MyComparer:IComparer
{
    privateHashtable typelist = new Hashtable();
      publicvoid Add(Type type,int order)
      { typelist.Add(type, order); }
      #region IComparer 成员

      public   int Compare(object x, object y)
      {
            int i1 = (int)typelist;
            int i2 = (int)typelist;
            //if it store the value type ,it don't implement the interface IComparable
            returni1.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();      
    }
}
页: [1]
查看完整版本: Agile Principles,Patterns,And Practices in C# 之OCP源代码