适配器模式(集合变堆栈)
程序代码:
'''Stack.cls Public Function Peeking() As Variant End Function Public Function Poping() As Variant End Function Public Sub Puting(ByVal S As Variant) End Sub Public Sub Dispose() End Sub
程序代码:
'''IntStack.cls Implements Stack Private StackArr As New Collection Private Sub Stack_Dispose() Set StackArr = Nothing End Sub Private Function Stack_Peeking() As Variant If StackArr.Count > 0 Then Stack_Peeking = StackArr.Count End If End Function Private Function Stack_Poping() As Variant Stack_Poping = CInt(StackArr.Item(StackArr.Count)) StackArr.Remove (StackArr.Count) End Function Private Sub Stack_Puting(ByVal S As Variant) StackArr.Add CInt((S)) End Sub
程序代码:
'''StrStack.cls Implements Stack Private StackArr As New Collection Private Sub Stack_Dispose() Set StackArr = Nothing End Sub Private Function Stack_Peeking() As Variant If StackArr.Count > 0 Then Stack_Peeking = StackArr.Count End If End Function Private Function Stack_Poping() As Variant Stack_Poping = CStr(StackArr.Item(StackArr.Count)) StackArr.Remove (StackArr.Count) End Function Private Sub Stack_Puting(ByVal S As Variant) StackArr.Add CStr((S)) End Sub
程序代码:
Private Sub Form_Click() Dim S1 As Stack Set S1 = New IntStack S1.Puting (3) S1.Puting (2) S1.Puting (6) Debug.Print S1.Poping Debug.Print S1.Poping Debug.Print S1.Peeking S1.Dispose Set S1 = New StrStack S1.Puting ("XXX") S1.Puting ("YYY") S1.Puting ("ZZZ") Debug.Print S1.Poping Debug.Print S1.Poping Debug.Print S1.Poping End Sub