Τρίτη 14 Ιουλίου 2020

Template Method Pattern (OOP)

The Trip class is the template. The method performTrip is final, we can't change it. But we can make new classes redefining the parts of the trip.


class Trip {
      module final performTrip {
            .doComingTransport
            .doDayA
            .doDayB
            .doDayC
            .doReturningTransport
      }
      module doComingTransport {
            error "abstract"
      }
      module doDayA {
            error "abstract"
      }
      module doDayB() {
            error "abstract"
      }
      module doDayC() {
            error "abstract"
      }
      module doReturningTransport {
            error "abstract"
      }
}
class PackageA as Trip {
      module doComingTransport {
            Print "The tourists are coming by air ..."
      }
      module doDayA {
            Print "The tourists are visiting the aquarium ..."
      }
      module doDayB {
            Print "The tourists are going to the beach ..."
      }
      module doDayC {
            Print "The tourists are going to mountains ..."
      }
      module doReturningTransport {
            Print "The tourists are going home by air ..."
      }
}
class PackageB as Trip {
      module doComingTransport {
            Print "The tourists are comming by train ..."
      }
      module doDayA {
            Print "The tourists are visiting the mountain ..."
      }
      module doDayB {
            Print "The tourists are going to the beach ..."
      }
      module doDayC {
            Print "The tourists are going to zoo ..."
      }
      module doReturningTransport {
            Print "The tourists are going home by train ..."
      }
}
Print "First Trip"
M=PackageB()
M.performTrip
Print "Next Trip"
M=PackageA()
M.performTrip

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.