株式会社エス・スリー・フォー

C++/C#/VB.NETによる リファクタリング – リファクタリング後(VB.NET)

Movie.vb

Namespace visualbasic_after

  Public Class Movie

    Public Enum enumPriceCode
      CHILDRENS = 2
      REGULAR = 0
      NEW_RELEASE = 1
    End Enum

    Private _title As String
    Private _price As Price

    Public Sub New(ByVal title As String, ByVal code As enumPriceCode)
      _title = title
      PriceCode = code
    End Sub

    Public Property PriceCode() As enumPriceCode
      Get
        PriceCode = _price.PriceCode
      End Get
      Set(ByVal Value As enumPriceCode)
        Select Case Value
        Case Movie.enumPriceCode.REGULAR
          _price = New RegularPrice()
        Case Movie.enumPriceCode.CHILDRENS
          _price = New ChildrensPrice()
        Case Movie.enumPriceCode.NEW_RELEASE
          _price = New NewReleasePrice()
        Case Else
          Throw New System.Exception("不正な料金コード")
        End Select
      End Set
    End Property

    Public ReadOnly Property Title() As String
      Get
        Title = _title
      End Get
    End Property

    Public Function getCharge(ByVal daysRented As Integer) As Double
       getCharge = _price.getCharge(daysRented)
    End Function

    Public Function getFrequentRenterPoints(ByVal daysRented As Integer) As Integer
       getFrequentRenterPoints = _price.getFrequentRenterPoints(daysRented)
    End Function

  End Class

End Namespace

Rental.vb

Namespace visualbasic_after

  Public Class Rental

    Private _movie As Movie
    Private _daysRented As Integer

    Public Sub New(ByVal movie As Movie, ByVal daysRented As Integer)
      _movie = movie
      _daysRented = daysRented
    End Sub

    Public ReadOnly Property DaysRented() As Integer
      Get
        DaysRented = _daysRented
      End Get
    End Property

    Public ReadOnly Property TheMovie() As Movie
      Get
        TheMovie = _movie
      End Get
    End Property

    Public ReadOnly Property Charge() As Double
      Get
        Charge = _movie.getCharge(_daysRented)
      End Get
    End Property

    Public ReadOnly Property FrequentRenterPoints() As Integer
      Get
        FrequentRenterPoints = _movie.getFrequentRenterPoints(_daysRented)
      End Get
    End Property

  End Class

End Namespace

Customer.vb

Namespace visualbasic_after

  Public Class Customer

    Private _name As String
    Private _rentals As System.Collections.ArrayList

    Public Sub New(ByVal name As String)
      _rentals = New System.Collections.ArrayList()
      _name = name
    End Sub

    Public Sub addRental(ByVal arg As Rental)
      _rentals.Add(arg)
    End Sub

    Public ReadOnly Property Name() As String
      Get
        Name = _name
      End Get
    End Property

    Public Function statement() As String
      Dim result As String = "Rental Record for " + Name + Chr(13) + Chr(10)
      Dim item As Rental
      For Each item In _rentals
        ' この貸し出しに対する数値の表示
        result += Chr(9) + item.TheMovie.Title + Chr(9) + item.Charge.ToString() + Chr(13) + Chr(10)
      Next
      ' フッタ部分の追加
      result += "Amount owed is " + TotalCharge.ToString() + Chr(13) + Chr(10)
      result += "You earned " + TotalFrequentRenterPoints.ToString() + " frequent renter points"
      statement = result
    End Function

    Public ReadOnly Property TotalCharge() As Double
      Get
        Dim result As Double = 0
        Dim item As Rental
        For Each item In _rentals
          result += item.Charge
        Next
        TotalCharge = result
      End Get
    End Property

    Public ReadOnly Property TotalFrequentRenterPoints() As Double
      Get
        Dim result As Integer = 0
        Dim item As Rental
        For Each item In _rentals
          result += item.FrequentRenterPoints
        Next
        TotalFrequentRenterPoints = result
      End Get
    End Property
  End Class

End Namespace

Price.vb

Namespace visualbasic_after

  Public MustInherit Class Price

    Public ReadOnly Property PriceCode() As Movie.enumPriceCode
      Get
        PriceCode = getPriceCode()
      End Get
    End Property

    Public MustOverride Function getCharge(ByVal daysRented As Integer) As Double

    Public Overridable Function getFrequentRenterPoints(ByVal daysRented As Integer) As Integer
      getFrequentRenterPoints = 1
    End Function

    Public Overridable Function getPriceCode() As Movie.enumPriceCode
      getPriceCode = Movie.enumPriceCode.REGULAR
    End Function

  End Class

  Class ChildrensPrice
    Inherits Price

    Public Overrides Function getPriceCode() As Movie.enumPriceCode
      getPriceCode = Movie.enumPriceCode.CHILDRENS
    End Function

    Public Overrides Function getCharge(ByVal daysRented As Integer) As Double
      Dim result As Double = 1.5
      If daysRented > 3 Then
        result += (daysRented - 3) * 1.5
      End If
      getCharge = result
    End Function

  End Class

  Public Class NewReleasePrice
    Inherits Price

    Public Overrides Function getPriceCode() As Movie.enumPriceCode
      getPriceCode = Movie.enumPriceCode.NEW_RELEASE
    End Function

    Public Overrides Function getCharge(ByVal daysRented As Integer) As Double
      getCharge = daysRented * 3
    End Function

    Public Overrides Function getFrequentRenterPoints(ByVal daysRented As Integer) As Integer
      If daysRented > 1 Then
        getFrequentRenterPoints = 2
      Else
        getFrequentRenterPoints = 1
      End If
    End Function

  End Class

  Class RegularPrice
    Inherits Price

    Public Overrides Function getPriceCode() As Movie.enumPriceCode
      getPriceCode = Movie.enumPriceCode.REGULAR
    End Function

    Public Overrides Function getCharge(ByVal daysRented As Integer) As Double
      Dim result As Double = 2
      If daysRented > 2 Then
        result += (daysRented - 2) * 1.5
      End If
      getCharge = result
    End Function

  End Class

End Namespace