Command パターン
処理をクラスとして表現するわけですね。共通の情報を元にいくつかの処理がある場合、一つの分岐によって処理クラス自体を入れ替えて使用すると。
これも面白そうなので書いてみました。
[Visual Basic]
Imports System
Public Class Demand
Private _name As String = String.Empty
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
Public Sub New(ByVal name As String)
Me._name = name
End Sub
End Class
Public MustInherit Class AbstractCommand
Protected _demand As Demand
Public Sub SetDemand(ByVal d As Demand)
Me._demand = d
End Sub
Public MustOverride Sub Execute()
End Class
Public Class RalCommand
Inherits AbstractCommand
Public Overrides Sub Execute()
If MyBase._demand IsNot Nothing Then
Console.WriteLine("{0}とは違うのだよ、{0}とは!!", MyBase._demand.Name)
End If
End Sub
End Class
Public Class JaianCommand
Inherits AbstractCommand
Public Overrides Sub Execute()
If MyBase._demand IsNot Nothing Then
Console.WriteLine("{0}のくせになまいきだぞ!", MyBase._demand.Name)
End If
End Sub
End Class
Public Class Program
Public Shared Sub Main(ByVal args As String())
If args.Length < 2 Then
Exit Sub
End If
Dim command As AbstractCommand
If args(0) = "R" Then
command = New RalCommand
ElseIf args(0) = "J" Then
command = New JaianCommand
Else
command = Nothing
End If
If command IsNot Nothing Then
Dim d As New Demand(args(1))command.SetDemand(d)
command.Execute()
End If
End Sub
End Class