lundi 14 août 2017

In Java, how to understand the signature with public final void in Template method pattern

In template method pattern, there is a method called 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");
    }

}

Aucun commentaire:

Enregistrer un commentaire