VB.Net Tutorial/Design Patterns/Singleton

Материал из VB Эксперт
Версия от 15:54, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Singleton Example

<source lang="vbnet">Public Class Singleton

 Private Shared SP As Singleton
 Private InnerList as New Collections.ArrayList()
 Private Sub New()
 End Sub 
 
 Public Shared Function Create() As Singleton
   If SP is Nothing Then SP = New Singleton() 
   Return SP
 End Function
 Public ReadOnly Property List As Collections.ArrayList
   Get
     Return InnerList
   End Get
 End Property

End Class Module SingletonExample

 Sub Main
   Dim CountValue as Integer
   Dim SP As Singleton = Singleton.Create()
   Dim SP2 As Singleton = Singleton.Create()
   SP.List.Add("First")
   SP.List.Add("Second")
   SP.List.Add("Third")
   For CountValue = 0 To SP2.List.Count - 1
     Console.WriteLine(SP2.List.Item(CountValue).ToString())
   Next
 End Sub

End Module</source>

First
Second
Third