定      义:将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起共走的两个类能够在一起工作

优      点:

            1、适配器模式可以让连个没有任何关系的类在一起运行,只要适配器这个角色能够搞定他们就成

            2、增加了类的透明性

            3、提高了类的复用度

            4、灵活非常好用

使用场景:新需求中提出的改进,原设计模式无法满足,可通过适配器模式来加以解决

使用案例:

              先来看看通用类图吧:

设计模式利剑13-适配器模式_function

               首先来看看一个案例,获得连个不同系统的用户信息            

设计模式利剑13-适配器模式_class_02

  

Public Interface IUserInfo

Function getName() As String
Function getTel() As String
Function getAddress() As String
End Interface ' IUserInfo
Public Class UserInfo
Implements IUserInfo
Private _name As String
Private _tel As String
Private _address As String
Public Sub New(ByVal name As String, ByVal tel As String, ByVal address As String)
_name = name
_tel = tel
_address = address
End Sub
Public Function getName() As String Implements IUserInfo.getName
Return _name
End Function
Public Function getTel() As String Implements IUserInfo.getTel
Return _tel
End Function
Public Function getAddress() As String Implements IUserInfo.getAddress
Return _address
End Function
End Class ' UserInfo
Public MustInherit Class IOutUser
Public MustOverride Function name() As String
Public MustOverride Function phone() As String
Public MustOverride Function address() As String
End Class ' IOutUser
Public Class OutUser
Inherits IOutUser
Private _name As String
Private _phone As String
Private _address As String
Public Sub New(ByVal __name As String, ByVal __phone As String, ByVal __address As String)
_name = __name
_phone = __phone
_address = __address
End Sub
Public Overrides Function name() As String
Return _name
End Function
Public Overrides Function phone() As String
Return _phone
End Function
Public Overrides Function address() As String
Return _address
End Function
End Class ' OutUser
Public Class OutUserInfo
Inherits OutUser
Implements IUserInfo
Public Sub New(ByVal name As String, ByVal phone As String, ByVal address As String)
MyBase.New(name, phone, address)
End Sub
Public Function getName() As String Implements IUserInfo.getName
Return name()
End Function
Public Function getAddress() As String Implements IUserInfo.getAddress
Return address()
End Function
Public Function getTel() As String Implements IUserInfo.getTel
Return phone()
End Function
Public Overrides Function name() As String
Return "外借人员:" & MyBase.name()
End Function
Public Overrides Function phone() As String
Return MyBase.phone
End Function
Public Overrides Function address() As String
Return MyBase.address
End Function
End Class ' OutUserInfo
Module Client
Sub Main()
Dim _arraylist As ArrayList = New ArrayList
Dim _IuserInfo As IUserInfo
_IuserInfo = New UserInfo("张三", "101010", "深圳市")
_arraylist.Add(_IuserInfo)
_IuserInfo = New UserInfo("李四", "122212", "深圳市")
_arraylist.Add(_IuserInfo)
_IuserInfo = New UserInfo("小五", "222222", "深圳市")
_arraylist.Add(_IuserInfo)
_IuserInfo = New OutUserInfo("六字", "222222", "深圳市")
_arraylist.Add(_IuserInfo)
For i As Integer = 0 To _arraylist.Count - 1
Console.WriteLine(CType(_arraylist.Item(i), IUserInfo).getName & " " & CType(_arraylist.Item(i), IUserInfo).getTel & " " & CType(_arraylist.Item(i), IUserInfo).getAddress)
Next
Console.Read()
End Sub
End Module