Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Defining Algorithm Skeletons with the Template Method Pattern

Tech May 7 5

The Template Method pattern establishes the skeleton of an algorithm in a base class, deferring specific step implementations to subclasses. This ensures the algorithm's structure remains unchanged while allowing customized behavior for individual steps.

Consider an automated vehicle system where the operational sequence must remain strictly consistent: engine ignition, safety alert, route navigation, and engine shutdown. Different vehicle types will implement these steps uniquely, but the overarching execution flow must not be altered.

Abstract Base Class

public abstract class AutomatedVehicle {

    public abstract void igniteEngine();

    public abstract void soundAlert();

    public abstract void plotRoute();

    public abstract void haltEngine();

    public final void executeDrivingSequence() {
        igniteEngine();
        soundAlert();
        plotRoute();
        haltEngine();
    }
}

Concrete Implementation: Sport Coupe

public class SportCoupe extends AutomatedVehicle {

    @Override
    public void igniteEngine() {
        System.out.println("Sport Coupe: Engine roaring to life");
    }

    @Override
    public void soundAlert() {
        System.out.println("Sport Coupe: Honking horn");
    }

    @Override
    public void plotRoute() {
        System.out.println("Sport Coupe: Calculating fastest route");
    }

    @Override
    public void haltEngine() {
        System.out.println("Sport Coupe: Engine turned off");
    }
}

Concrete Implementation: Heavy Truck

public class HeavyTruck extends AutomatedVehicle {

    @Override
    public void igniteEngine() {
        System.out.println("Heavy Truck: Diesel engine starting");
    }

    @Override
    public void soundAlert() {
        System.out.println("Heavy Truck: Blowing air horn");
    }

    @Override
    public void plotRoute() {
        System.out.println("Heavy Truck: Routing via truck-friendly roads");
    }

    @Override
    public void haltEngine() {
        System.out.println("Heavy Truck: Engine braking and stopping");
    }
}

Execution Simulation

public class Simulator {

    public static void main(String[] args) {
        AutomatedVehicle coupe = new SportCoupe();
        coupe.executeDrivingSequence();

        AutomatedVehicle truck = new HeavyTruck();
        truck.executeDrivingSequence();
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.