1

In template method pattern, there is a method marked final, but I cannot understand the meaning of having this method. Please help me out. In another word, in class PackageA, one can implement the algorithm for each abstract method as needed. And the client can call method in PackageA with their own order. What is the function/ meaning to having this public final void performTrip()? Thank you very much.

public abstract class Trip {
    public final void performTrip() {
        doComingTransport();
        doDayA();
        doDayB();
        doDayC();
        doReturningTransport();
    }

    public abstract void doComingTransport();

    public abstract void doDayA();

    public abstract void doDayB();

    public abstract void doDayC();

    public abstract void doReturningTransport();
}

public class PackageA extends Trip {
    @Override
    public void doComingTransport() {
        System.out.println("PKG A" + "coming");
    }

    @Override
    public void doDayA() {
        System.out.println("PKG A" + "A");
    }

    @Override
    public void doDayB() {
        System.out.println("PKG A" + "B");
    }

    @Override
    public void doDayC() {
        System.out.println("PKG A" + "C");
    }

    @Override
    public void doReturningTransport() {
        System.out.println("PKG A" + "Return");
    }

}
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
mybebr
  • 19
  • 1
  • There is no "method called final". The method is called `performTrip`. It is *marked* final by the `final` keyword. I have edited your question accordingly. – slim Aug 14 '17 at 09:42
  • This is to show you which method to call. – ACV Aug 14 '17 at 09:43
  • Related: https://stackoverflow.com/questions/22015933/understanding-template-method-pattern?rq=1 – Mark Rotteveel Aug 14 '17 at 09:45

6 Answers6

2

The final keywords means that a sub-class of Trip cannot override the performTrip() method.

In the current context this means sub-classes are allowed to override the individual parts of a trip, e.g. what happens on day A or B, but not the order in which they happen during the trip (day A always happens before day B).

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
1

It just means that no other class extending the abstract class Trip may override the performTrip() method.

If an inheriting class want to use it they must use it as written in the Trip class.

So PackageA cannot contain a method with the same signature (performTrip()), and any call to performTrip() in PackageA will refer to the method as recorded in the Trip class.

Assafs
  • 3,257
  • 4
  • 26
  • 39
1

In Template design pattern, one method is defining the sequence or in other words the order of calling the other methods in the class. In your example

doComingTransport();
doDayA();
doDayB();
doDayC();
doReturningTransport();

sequence of calling these methods cannot be changed in this Template pattern. So any class extending this class should not have the chance to override "performTrip" method because this defines the order of the calling methods. But the individual methods can be overridden and change the functionality. That's why method "performTrip" is declared as final so subclasses cannot override

Juliyanage Silva
  • 2,529
  • 1
  • 21
  • 33
1

The final key word prevents other programmers from overriding this method in a technical way.

This seams to be necessary to distinguish is from the other methods in this class which must be overridden (to be implemented) in subclasses.

However,
The problem I see here is that all of the abstract methods should have protectedvisibility since they are meant to provide type specific behavior which should not be part of a classes public API.

Furthermore the final key word also prevents the creation of mock objects for this class (and its descendants) which makes it hard to build UnitTest for classes that have a dependency to this class.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
1

The Template pattern implies that client code will use PackageA.performTrip(). It could be like

 Collection<Trip> worldTour;
// add PackageA, PackageB and climbEverest to a worldTour

foreach(Trip currentTrip: worldTour) {
   currentTrip.performTrip();
}

Perforimng dayB() outside the tour often is restricted, for example it could be protected

With final you protect realisation of performTrip: nobody could go for dayA() after DayB()

ADS
  • 708
  • 3
  • 14
1

Making template method final makes sure that our implementation supports intent of the Template Design pattern "Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure." as given in GOF book, by Eric Gamma). Book also explains the reasoning in it's consequences part - "It's important for template methods to specify which operations are hooks (may be overridden) and which are abstract operations (must be overridden)... "

But I do not think making micro operations protected is necessary condition of design pattern. class can use those in public scope as long as it does not harm the pattern.

Practically design patterns never appear in isolation but they appear hand in hand situation. so making 100% justice to one pattern should not make injustice to others helping application. After all patterns are at abstract design level and not at implementation level.

Kedar Tokekar
  • 418
  • 3
  • 10