1、单件模式

Class:SingletonClass
VB6-设计模式点滴Option Explicit
VB6-设计模式点滴
VB6-设计模式点滴
Public Count As Integer
VB6-设计模式点滴
VB6-设计模式点滴
Private Sub Class_Initialize()
VB6-设计模式点滴    
If gSingleton Is Nothing Then
VB6-设计模式点滴        
Set gSingleton = Me
VB6-设计模式点滴    
End If
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
Public Function GetInstance() As SingletonClass
VB6-设计模式点滴    
Set GetInstance = gSingleton
VB6-设计模式点滴
End Function

模块声明
VB6-设计模式点滴Public gSingleton As SingletonClass


2、方法继承
Class:IMethod

VB6-设计模式点滴Public Function SetName(Name As String)
VB6-设计模式点滴    SetName 
= Trim(UCase(Name))
VB6-设计模式点滴
End Function
VB6-设计模式点滴

Class:NewMethod

VB6-设计模式点滴Implements IMethod
VB6-设计模式点滴
VB6-设计模式点滴
Private Base As IMethod
VB6-设计模式点滴
VB6-设计模式点滴
Private Sub Class_Initialize()
VB6-设计模式点滴    
Set Base = New IMethod
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
Private Sub Class_Terminate()
VB6-设计模式点滴    
Set Base = Nothing
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
Private Function IMethod_SetName(Name As StringAs Variant
VB6-设计模式点滴    IMethod_SetName 
= Base.SetName(Name)
VB6-设计模式点滴    IMethod_SetName 
= IMethod_SetName & "0001"
VB6-设计模式点滴
End Function

3:工厂模式:

CreateObject

4:ComUnit的一个设计模式
VB6-设计模式点滴Implements ITestContainer
VB6-设计模式点滴
VB6-设计模式点滴
Public Property Get ITestContainer_TestCaseNames() As Variant()
VB6-设计模式点滴    ITestContainer_TestCaseNames 
= Array("TestString")
VB6-设计模式点滴
End Property
VB6-设计模式点滴
VB6-设计模式点滴
Public Sub ITestContainer_RunTestCase(oTestCase As ITestCase, oTestResult As TestResult)
VB6-设计模式点滴    CallByName Me, oTestCase.Name, VbMethod, oTestResult
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
Public Sub TestString(oTestResult As TestResult)
VB6-设计模式点滴
End Sub

使用TestCaseNames向外暴露自身扩展的成员。

使用类似于TestString的方法(接口参数一致),来扩展自身功能。

借助TestResult来贯穿类处理的总线。

使用TestRunner来处理符合ITestContainer接口的类。

5:观察者模式

VB6-设计模式点滴Option Explicit
VB6-设计模式点滴
'Ineteface Subject
VB6-设计模式点滴
Public Sub Register(obs As Observer)
VB6-设计模式点滴
End Sub
VB6-设计模式点滴

VB6-设计模式点滴Option Explicit
VB6-设计模式点滴
VB6-设计模式点滴
'Interface Observer
VB6-设计模式点滴
Public Sub Notify(msg As String)
VB6-设计模式点滴
End Sub

VB6-设计模式点滴'frmMain
VB6-设计模式点滴

VB6-设计模式点滴Implements Subject
VB6-设计模式点滴
VB6-设计模式点滴
Dim cc As Collection
VB6-设计模式点滴
VB6-设计模式点滴
Private Sub Command1_Click()
VB6-设计模式点滴    
Dim c As Observer
VB6-设计模式点滴    
For Each c In cc
VB6-设计模式点滴        c.Notify 
InputBox("Caption:")
VB6-设计模式点滴    
Next
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
Private Sub Form_Load()
VB6-设计模式点滴    
Set cc = New Collection
VB6-设计模式点滴    
Dim o As frm1
VB6-设计模式点滴    
Set o = New frm1
VB6-设计模式点滴    o.Ini Me
VB6-设计模式点滴    o.Show
VB6-设计模式点滴    
VB6-设计模式点滴    
Dim oo As frm2
VB6-设计模式点滴    
Set oo = New frm2
VB6-设计模式点滴    oo.Ini Me
VB6-设计模式点滴    oo.Show
VB6-设计模式点滴
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
Private Sub Subject_Register(obs As Observer)
VB6-设计模式点滴    cc.Add obs
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
VB6-设计模式点滴
'frm1
VB6-设计模式点滴
Implements Observer
VB6-设计模式点滴
VB6-设计模式点滴
Public Sub Ini(s As Subject)
VB6-设计模式点滴    s.Register Me
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
Private Sub Observer_Notify(msg As String)
VB6-设计模式点滴    Me.Caption 
= msg
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
'frm2
VB6-设计模式点滴

VB6-设计模式点滴Implements Observer
VB6-设计模式点滴
VB6-设计模式点滴
Public Sub Ini(s As Subject)
VB6-设计模式点滴    s.Register Me
VB6-设计模式点滴
End Sub
VB6-设计模式点滴
VB6-设计模式点滴
Private Sub Observer_Notify(msg As String)
VB6-设计模式点滴    Me.Caption 
= msg
VB6-设计模式点滴
End Sub
VB6-设计模式点滴

相关文章:

  • 2022-03-04
  • 2022-01-22
  • 2021-12-02
  • 2022-02-20
  • 2021-12-15
  • 2021-11-04
  • 2021-05-21
  • 2021-11-24
猜你喜欢
  • 2021-08-11
  • 2022-01-01
  • 2021-11-28
  • 2021-09-05
  • 2021-11-27
  • 2021-09-09
  • 2021-08-08
相关资源
相似解决方案