Implementation

[Previous] [Next]

By now it should be clear that an Adapter design pattern is implemented either through multiple interface inheritance (Class Adapter) or by the creation of an object reference to the adaptee instance (Object Adapter and Function Adapter). To better appreciate the Adapter design pattern, let's go through the steps required to implement the Transparent Class Adapter as shown in the object model in Figure 4-3. Next, let's assume that the adaptee exists in an ActiveX DLL. (You create a reference to the DLL in your project by selecting References from the Project menu in Visual Basic.) In your project, you would create a class module and change the name to ExpectedInterface. The ExpectedInterface class is abstract, so the functions defined in this class module will contain no implementation, as shown in the code extract below:

 ' Class Name: ExpectedInterface ' Note: This is the abstract interface expected ' by the client and implemented by the ' concrete Adapter class. Option Explicit Public Function Func1(sVal As String) As Boolean End Function Public Function Func2(iVal As Integer) As String End Function 

Now create another class module and change the name to Adapter, and then inherit both the ExpectedInterface class and the Adaptee class using the Implements keyword. Next, you must define a private object variable named m_adaptee of type Adaptee. In the Adapter.Class_Initialize event handler, construct an instance of the Adaptee class and store a reference to it in the m_adaptee private object variable. The Adaptee class is concrete; therefore, it contains implementation for all the functions of its interface. Regardless, classes in Visual Basic can inherit only the interface. Hence, in order to improvise for the lack of implementation inheritance, the m_adaptee variable is defined to provide the equivalent benefit. Finally, the Adapter class must implement all functions from both the ExpectedInterface interface and the Adaptee interface. You, the designer of the Adapter class, determine the extent to which work is delegated to the Adaptee instance. All work that results from invocations via the Adaptee interface should be delegated appropriately to the Adaptee instance. The following code extract is a possible implementation of the Adapter class:

 ' Class Name: Adapter ' Note: Concrete Adapter class inherits both the ' ExpectedInterface interface and the Adaptee ' interface and provides implementation ' for all functions in both interfaces. Option Explicit ' Interface expected by client Implements ExpectedInterface ' Interface of concrete class being adapted Implements Adaptee ' Visual Basic doesn't support implementation ' inheritance, so this Adaptee object variable is a ' workaround that provides the same result by simply ' delegating one-for-one all invocations via the Adaptee interface ' through this Adaptee object variable. Private m_adaptee As Adaptee ' When an instance of this Adapter class is instantiated, ' the Initialize event handler gets called. This is the ' perfect time to instantiate the Adaptee. You are now ' guaranteeing that all invocations that delegate work ' to the Adaptee will occur without the client having to ' explicitly create an Adaptee. This type of arrangement ' is automatic with implementation inheritance, so it is ' only fair that you provide the same behavior. Private Sub Class_Initialize() Set m_adaptee = New Adaptee End Sub ' Implement all functions of the ExpectedInterface ' interface. Notice the delegation of work to the Adaptee ' via the m_adaptee object variable. Also notice that it is ' up to the programmer of this Adapter to delegate and ' interpret the results accordingly. ' ExpectedInterface.Func1 Private Function ExpectedInterface_Func1(sVal As String) As Boolean If m_adaptee.FuncB(sVal) > 37 Then ExpectedInterface_Func1 = True Else ExpectedInterface_Func1 = False End If End Function ' ExpectedInterface.Func2 Private Function ExpectedInterface_Func2(iVal As Integer) As String If m_adaptee.FuncA(CLng(iVal)) < 10.9 Then ExpectedInterface_Func2 = "Great Job!" Else ExpectedInterface_Func2 = "Needs Improvement." End If End Function ' Implement all functions of the Adaptee interface. ' Notice the one-for-one delegation of work. ' Adaptee.FuncA Private Function Adaptee_FuncA(lVal As Long) As Double Adaptee_FuncA = m_adaptee.FuncA(lVal) End Function ' Adaptee.FuncB Private Function Adaptee_FuncB(sVal As String) As Long Adaptee_FuncB = m_adaptee.FuncB(sVal) End Function 

The client code is completely oblivious to the processing details of the Adapter. Hence, writing the source code for the client is blatantly simple, as this code extract illustrates:

 ' Client code ' ' Declare object variable of type ExpectedInterface. Dim ei As ExpectedInterface  ' Instantiate Adapter that supports the ExpectedInterface ' interface. Set ei = New Adapter retcode = ei.Func1("45") strMsg = ei.Func2(2) MsgBox "Func1 returned " & retcode & vbCr & _ "Func2 returned " & strMsg ' Remember, this is a Transparent Class Adapter design pattern; ' therefore, an interface to the Adaptee could be ' acquired easily using the following approach: ' 'Dim wr As Adaptee 'Set wr = ei 



Microsoft Visual Basic Design Patterns
Microsoft Visual Basic Design Patterns (Microsoft Professional Series)
ISBN: B00006L567
EAN: N/A
Year: 2000
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net