[转载] C#面向对象设计模式纵横谈——7. Adapter适配器模式
生活随笔
收集整理的這篇文章主要介紹了
[转载] C#面向对象设计模式纵横谈——7. Adapter适配器模式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
主講:李建忠
來源:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/consyscourse/CsharpOOD.aspx
?
?
Adapterinterface IStack //客戶期望的接口 {void Push(object item);object Pop();object Peek(); }//對象適配器 class MyStack:IStack //adapter,適配對象 {ArrayList list; //adaptee,被適配對象public MyStack(){list=new ArrayList();} public void Push(object item){list.Add(item);}object Pop(){return list.RemoveAt(list.Count-1);}object Peek(){return list[list.Count-1];} }//類適配器 class MyStack:ArrayList,IStack //adapter,適配對象 {public void Push(object item){this.Add(item);}object Pop(){ return this.RemoveAt(this.Count-1);}object Peek(){return this[this.Count-1];} }? EmployeeAdapterclass Employee {int age;string name; public int Age{get{return this.age;}set{this.age=value;}} }class EmployeeAdapter:IComparable {public int Compare(object obj1,object obj2){if(obj1.GetType()!=typeof(Employee)||obj2.GetType()!=typeof(Employee)){throw new Exception();}Employee e1=(Employee)obj1;Employee e2=(Employee)obj2;if(e1.Age==e2.Age){return 0;}else if(e1.Age>e2.Age){return 1;}else if(e1.Age<e2.Age){return -1;}} }class App {public static void Main(){Employee[] employees=new Employee[100];//...ArrayList.Sort(employees,new EmployeeAdapter())} }
轉(zhuǎn)載于:https://www.cnblogs.com/6DAN_HUST/archive/2011/06/14/2081008.html
總結(jié)
以上是生活随笔為你收集整理的[转载] C#面向对象设计模式纵横谈——7. Adapter适配器模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SONICWALL E-Class NS
- 下一篇: CLR Via C# 学习笔记(5) 静