Another Blog to add to your list
I just discovered the blog of my good friend Michael Duff. He has invented a new blogging term "Blirgin". It means someone who has never blogged :)
Check out his blog at Duff Blog
Public MustInherit Class AbstractFileEncryptor
Public Property Key() As String
'code for the property
End Property
Public Property Source() As String
'code for the property
End Property
Public Property Target() As String
'code for the property
End Property
'An abstract method
Public MustOverride Sub Encrypt()
End Class
Public Class RSAFileEncryptor : Inherits AbstractFileEncryptor
Public Overrides Sub Encrypt()
'code to implement RSA encryption
End Sub
End Class
Public Interface InterfaceX
Property Command() As String
Sub Execute()
End Interface
Dim myVar As New InterfaceX
myVar.Command = "Select * from Whatever"
myVar.Execute()
Public Class ClassX : Implements InterfaceX
Public Property Command() As String Implements InterfaceX.Command
'code goes here
End Property
Public Sub Execute() Implements InterfaceX.Execute
'code goes here
End Sub
End Class
Dim myVar As InterfaceX
myVar = New ClassX
myVar.Command = "Select * from Whatever"
myVar.Execute()
Dim myVar as New ClassX
myVar.Command = "Select * from Whatever"
myVar.Execute()
Dim myVar1 as Class1
Dim myVar2 as Class2
'etc ...
Dim myVar10 as Class10
For n = 0 to CollectionX.Length - 1
If Typeof CollectionX(n) is Class1 then
myVar1 = CType(CollectionX(n), Class1)
myVar1.Execute()
ElseIf Typeof CollectionX(n) is Class2 then
myVar2 = CType(CollectionX(n), Class2)
myVar2.ExecuteProc()
'etc ...
ElseIf Typeof CollectionX(n) is Class10 then
myVar10 = CType(CollectionX(n), Class10)
myVar10.ExecuteStatement()
End If
Next
Dim myVar as InterfaceX
For Each myVar in CollectionX
myVar.Execute()
Next
Public Class CryptoFacade
Public Property Key() As String
Public Property Source() As String
Public Property Target() As String
Public Property Type() As String
Public Sub Encrypt()
Public Sub Decrypt()
End Class
Friend Class EncryptFile
'code to encrypt a file
End Class
Friend Class DecryptFile
'code to decrypt a file
End Class
Public Class SingletonMP3Player
'1) the private variable
Private Shared _instance As SingletonMP3Player
'2) the static method to return the singleton
Public Shared Function GetInstance() As SingletonMP3Player
'if it has not already been instantiated then create it
If _instance Is Nothing Then
_instance = New SingletonMP3Player
End If
Return _instance
End Function
'3) the private constructor
Private Sub New()
'prevents instantiation from outside the class
End Sub
'Other methods required to make the class useful
Public Sub Play(filename As String)
'code to play, stopping self if already playing something else
End Sub
Public Sub StopPlaying()
'code here
End Sub
Public Sub OtherUserfulMethod()
'etc ...
End Sub
End Class
Public Class Singing
Sub SingTheBlues()
Dim myPlayer As SingletonMP3Player
myPlayer = SingletonMP3Player.GetInstance()
myPlayer.Play("c:\music\blues\Born Under A Bad Sign.mp3")
End Sub
Sub GrowlDemonically()
Dim myPlayer As SingletonMP3Player
myPlayer = SingletonMP3Player.GetInstance()
myPlayer.Play("c:\music\deathmetal\Blackwater Park.mp3")
End Sub
'or a show off way to do it
Sub JustListen()
SingletonMP3Player.GetInstance().Play("c:\music\prog\3 Min Warning.mp3")
End Sub
End Class
Public MustInherit Class Material : Implements ICloneable
Public MustOverride Property Description() As String
'Returns a shallow clone
Public Function Clone() As Object Implements ICloneable.Clone
Return CType(Me.MemberwiseClone(), Material)
End Function
Public MustOverride Sub CommonMethod()
End Class
Public Class Tape : Inherits Material
'All sorts of complex objects that take a long time to initialize
Private TypeX As ComplexClassOfSomeKind
'etc ...
Public Overrides Property Description() As String
'code goes here
End Property
Public Overrides Sub CommonMethod()
'code here
End Sub
'other methods ...
End Class
Public Class MaterialBuilder
Private _prototypes As New Hashtable
Public Sub New()
InitializePrototypes()
End Sub
Public Sub InitializePrototypes()
'put objects of the different material sub-classes into the hashtable
_prototypes.Add("TAPE", MaterialFactory("TAPE"))
_prototypes.Add("CD", MaterialFactory("CD"))
_prototypes.Add("DVD", MaterialFactory("DVD"))
'etc ...
End Sub
Public Function BuildMaterial(aType As String) As Material
Dim builder As Material
builder = _prototypes.Item(aType)
Return builder.Clone()
End Function
Private Function MaterialFactory(aType As String) As Material
'factory code goes here
End Function
End Class
Class ClientClass
Public Sub DoIt(aType As String, description As String)
Dim builder As New MaterialBuilder
Dim myMaterial As Material = builder.BuildMaterial(aType)
myMaterial.Description = description
myMaterial.CommonMethod()
'etc ...
End Sub
End Class
Public MustInherit Class AbstractDocumentBuilder
Public Shared Function GetBuilder(format As String) _
As AbstractDocumentBuilder
Select Case format.ToUpper()
Case "WORD"
Return New WordBuilder
Case "PDF"
Return New PDFBuilder
Case "XML"
Return New XMLBuilder
Case Else
Return Nothing
End Select
End Function
Public MustOverride Sub BuildDocHeader(docHeaderText As String)
Public MustOverride Sub BuildPageHeader(pageHeaderText As String)
Public MustOverride Sub BuildPreDataText(preDataText As String)
'more methods for other parts of the document ...
Public MustOverride ReadOnly Property Document() As IDocument
End Class
Public Class WordBuilder : Inherits AbstractDocumentBuilder
Private _document As New WordDocument
Public Overrides ReadOnly Property Document() As IDocument
Get
Return _document
End Get
End Property
Public Overrides Sub BuildDocHeader(docHeaderText As String)
'code here for Word format
End Sub
Public Overrides Sub BuildPageHeader(pageHeaderText As String)
'code here for Word format
End Sub
Public Overrides Sub BuildPreDataText(preDataText As String)
'code here for Word format
End Sub
End Class
Public Interface IDocument
Sub Write(fout As FileStream)
End Interface
Public Class WordDocument : Implements IDocument
Public Sub Write(ByVal fout As FileStream) Implements IDocument.Write
'code to write the document to the stream
End Sub
'other needed methods & properties
End Class
Public Sub WriteDocument(format As String, fout As FileStream)
Dim finalDocument As IDocument
Dim builder As AbstractDocumentBuilder
builder = AbstractDocumentBuilder.GetBuilder(format)
builder.BuildDocHeader(doc.DocumentHeader)
builder.BuildPageHeader(doc.PageHeader)
builder.BuildPreDataText(doc.PreDataText)
'Build other parts of the document ...
finalDocument = builder.Document
finalDocument.Write(fout)
End Sub
Customer
|
-----------------------------------------------
| | | |
DomesticTVCustomer IntTVCustomer PPVCustomer etc
Deal
|
--------------------------------------------
| | | |
DomesticTVDeal IntTVDeal PPVDeal etc
Public MustInherit Class Deal
Public MustOverride Sub MakeLotsOfMoney()
'other members of deal
End Class
Public Class DomesticTVDeal : Inherits Deal
Public Overrides Sub MakeLotsOfMoney()
'Money making code goes here
End Sub
'etc ...
End Class
Public Class PPVDeal : Inherits Deal
Public Overrides Sub MakeLotsOfMoney()
'Money making code goes here
End Sub
'etc ...
End Class
Public Class MediaFactory
Public Function GetCustomer(media As MediaType) as Customer
Select Case media
Case MediaType.DOMESTIC
return New DomesticTVCustomer
Case MediaType.INTERNATIONAL
return New InternationalCustomer
Case MediaType.PPV
return New PPVCustomer
Case ETC
'etc ...
End Select
End Function
Public Functon GetDeal(media As Integer) as Deal
'big select statement
End Function
Public Function GetLicense(media As Integer) as License
'big select statement
End Function
'etc ...
End Class
Public MustInherit Class AbstractMediaFactory
Public Enum MediaType As Integer
DOMESTIC
INTERNATIONAL
PPV
'etc ...
End Enum
Public Shared Function GetFactory(media As MediaType) As AbstractMediaFactory
Select Case media
Case MediaType.DOMESTIC
Return New DomesticTVFactory
Case MediaType.INTERNATIONAL
Return New InternationalTVFactory
Case MediaType.PPV
Return New PPVFactory
Case MediaType.ETC
'etc ...
Case Else
Return Nothing
End Select
End Function
Public MustOverride Function GetDeal() As Deal
Public MustOverride Function GetCustomer() As Customer
Public MustOverride Function GetLicense() As License
'Other GetXXX functions go here
End Class
Public Class DomesticTVFactory : Inherits AbstractMediaFactory
Public Overrides Function GetDeal() As Deal
Return New DomesticTVDeal
End Function
Public Overrides Function GetCustomer() As Customer
Return New DomesticTVCustomer
End Function
Public Overrides Function GetLicense() As License
Return New DomesticTVLicense
End Function
End Class
Public Class PPVFactory : Inherits AbstractMediaFactory
Public Overrides Function GetDeal() As Deal
Return New PPVDeal
End Function
Public Overrides Function GetCustomer() As Customer
Return New PPVCustomer
End Function
Public Overrides Function GetLicense() As License
Return New PPVLicense
End Function
End Class
Dim myFactory as AbstractMediaFactory
myFactory = AbstractMediaFactory.GetFactory(media)
Dim theDeal as Deal
theDeal = myFactory.GetDeal()
theDeal.MakeLotsOfMoney()