VBS中的Singleton类(单例模式)的代码示例

 

 VBS中的Singleton 代码示例

 

Class Class_FSO

   Private FSO_

   Public Property Get FSO
   print "Get FSO"
       On Error Resume Next
       If  Typename(FSO_) = "FileSystemObject" Then
           Set FSO = FileSysObj  'get the old one
     print "Old one"
       Else
           ExecuteGlobal "Const Class_FSO_IsRunning = 1" 'one instance frag
           If Err = 0 Then
               ExecuteGlobal "Set Class_FSO_RunningInstance = Me"
               Set FSO = CreateObject("Scripting.FileSystemObject")
               Set FileSysObj = FSO
      print "New one"
           Else
    print Err.number
               Set FSO = Class_FSO_RunningInstance.FSO
               Set FileSysObj = FSO
      print "Get formal instance"
           End If
       End If
   End Property

   Private Property Set FileSysObj(obj)
   print "Set FileSysObj"
       Set FSO_ = obj   
   End Property

   Private Property Get FileSysObj
   print "Get FileSysObj"
       Set FileSysObj = FSO_
   End Property
End Class

'==============================

Dim x
Dim y
Dim z
Dim a
Dim b

print "'====x===="
Set x = new Class_FSO
print "'====y===="
set y = x.FSO
print "'====b===="
set b = x.FSO
print "'====z===="
set z = new Class_FSO
print "'====a===="
set a = z.FSO
print "'========="
print CStr(y Is a) & " " & CStr(y Is b) & " " & CStr(a Is b)

你可能感兴趣的:(Singleton,Singleton,vbs,vbs)