OptionExplicit Public Count AsInteger PrivateSub Class_Initialize() If gSingleton IsNothingThen Set gSingleton = Me EndIf End Sub PublicFunction GetInstance() As SingletonClass Set GetInstance = gSingleton End Function
模块声明
Public gSingleton As SingletonClass
2、方法继承 Class:IMethod
PublicFunction SetName(Name AsString) SetName =Trim(UCase(Name)) End Function
Class:NewMethod
Implements IMethod Private Base As IMethod PrivateSub Class_Initialize() Set Base =New IMethod End Sub PrivateSub Class_Terminate() Set Base =Nothing End Sub PrivateFunction IMethod_SetName(Name AsString) As Variant IMethod_SetName = Base.SetName(Name) IMethod_SetName = IMethod_SetName &"0001" End Function
3:工厂模式:
CreateObject
4:ComUnit的一个设计模式
Implements ITestContainer PublicPropertyGet ITestContainer_TestCaseNames() As Variant() ITestContainer_TestCaseNames =Array("TestString") End Property PublicSub ITestContainer_RunTestCase(oTestCase As ITestCase, oTestResult As TestResult) CallByName Me, oTestCase.Name, VbMethod, oTestResult End Sub PublicSub TestString(oTestResult As TestResult) End Sub
使用TestCaseNames向外暴露自身扩展的成员。
使用类似于TestString的方法(接口参数一致),来扩展自身功能。
借助TestResult来贯穿类处理的总线。
使用TestRunner来处理符合ITestContainer接口的类。
5:观察者模式
OptionExplicit 'Ineteface Subject PublicSub Register(obs As Observer) End Sub
OptionExplicit 'Interface Observer PublicSub Notify(msg AsString) End Sub
'frmMain Implements Subject Dim cc As Collection PrivateSub Command1_Click() Dim c As Observer ForEach c In cc c.Notify InputBox("Caption:") Next End Sub PrivateSub Form_Load() Set cc =New Collection Dim o As frm1 Set o =New frm1 o.Ini Me o.Show Dim oo As frm2 Set oo =New frm2 oo.Ini Me oo.Show End Sub PrivateSub Subject_Register(obs As Observer) cc.Add obs End Sub 'frm1 Implements Observer PublicSub Ini(s As Subject) s.Register Me End Sub PrivateSub Observer_Notify(msg AsString) Me.Caption = msg End Sub 'frm2 Implements Observer PublicSub Ini(s As Subject) s.Register Me End Sub PrivateSub Observer_Notify(msg AsString) Me.Caption = msg End Sub