mardi 26 mai 2015

Abstract Factory to factory

I am new to design patterns and just found one Abstract factory example in Pro .Net book. How can i change this example to Factory (just to trying learn factory and abstract factory)

Public MustInherit Class Greeter

Public Shared Function GetGreerer() As Greeter
    Dim now As DateTime = DateTime.Now
    Dim hour As Integer = now.Hour
    Dim currentGreeter As Greeter

    Select Case hour
        Case 0 To 6
            currentGreeter = New morningGreeter
        Case 7 To 24
            currentGreeter = New nightGreeter
    End Select

    Return currentGreeter
End Function

Public MustOverride Function Greet() As String
End Class

Public Class nightGreeter
Inherits Greeter

Public Overrides Function Greet() As String
    Return "its night time"
End Function
End Class

Public Class morningGreeter
Inherits Greeter

Public Overrides Function Greet() As String
    Return "its morning time"
End Function
End Class

Sub Main()
 Dim g As Greeter = Greeter.GetGreerer
 MsgBox(g.Greet)
End Sub

Aucun commentaire:

Enregistrer un commentaire