适配器模式

将一个类的接口转换成客户希望的另一个接口。
Adapter模式使原来由于接口不兼容而不能一起工作的那些类能够一起工作。
                                                         GOF设计模式


Adapter模式主要应用于“希望复用一些现存的类,但是接口又与复
用环境要求不一致的情况“,在遗留代码复用,类库迁移等方面非常有用。
None.gif using  System.Collections;
None.gif
using  System;
None.gif
None.gif
interface  IStack    // 客户期望的接口
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
void Push(Object item);
InBlock.gif    Object Pop();
InBlock.gif    Object Peek();
ExpandedBlockEnd.gif}


None.gif // 对象适配器
None.gif
class  Adapter : IStack   // 适配对象
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    ArrayList adpatee;
//被适配的对象1
InBlock.gif    
//可以适配多个
InBlock.gif
    ArrayList adpatee2;//被适配的对象2
InBlock.gif
    
InBlock.gif    
public Adapter()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        adpatee 
= new ArrayList();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public void Push(Object item)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        adpatee.Add(item);
ExpandedSubBlockEnd.gif    }

InBlock.gif    Object Pop()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        adpatee.RemoveAt(adpatee.Count 
- 1);
ExpandedSubBlockEnd.gif    }

InBlock.gif    Object Peek()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return adpatee(adpatee.Count - 1);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


None.gif // 类适配器(违反了类应该具有单一职责的原则)
None.gif
class  Adapter : ArrayList,IStack
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public void Push(Object item)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.Add(item);
ExpandedSubBlockEnd.gif    }

InBlock.gif    Object Pop()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return this.RemoveAt(list.Count - 1);
ExpandedSubBlockEnd.gif    }

InBlock.gif    Object Peek()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return this(this.Count - 1);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}




None.gif 集合类中对现有对象的非序
None.gif   现有对象未实现IComparable接口
None.gif   实现一个排序适配器(继随IComparer接口),然后在
None.gif   其Compare方法中对两个对象进行比较
None.gif
class  Employee
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
int age;
InBlock.gif    
string name;
InBlock.gif    
InBlock.gif    
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gif{return this.age;}
ExpandedSubBlockStart.gifContractedSubBlock.gif        
setdot.gif{this.age = value;}
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
class  EmployeeSortAdapter : IComparer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public int Compare(Object obj1, Object obj2)
InBlock.gif    
//不一定按适配器模式来,把适配对象作为参数传递同样,例如此处的Object
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
if(obj1.GetType() != typeOf(Employee) || obj2.GetType() != typeOf(Employee))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//throw a Exception
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
InBlock.gif        Employee e1 
= (Employee)obj1;
InBlock.gif        Employee e2 
= (Employee)obj2;
InBlock.gif        
if(e1.Age == e2.Age)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return 0 ;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if(e1.Age > e2.Age)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return 1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if(e1.Age < e2.Age)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return -1;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
class  App
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static void Mian()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Employeep[] employee 
= new Employee[100];
InBlock.gif        
//dot.gif.赋值
InBlock.gif
        //排序
InBlock.gif        Array.Sort(employee, 
new EmployeeSortAdapter());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/sunsjorlin/archive/2006/02/25/337344.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值