CMPUT 301

Software Engineering

Practice w23am3q2


Template Method Pattern Refactoring (Java Code to UML Class Diagram)

This code represents a training program that includes two different types of workouts (cardio and strength training).

Restructure this code with the template method pattern and provide a UML class diagram depicting the structure and operations one would expect after refactoring this code to use a template method pattern. Your solution must significantly reduce code duplication and enhance reuse.

You must provide the template method Java code in a UML comment (see example below).

Image
Draw the resulting UML class diagram of the refactored code. Provide the basic abstractions, attributes, methods, relationships, multiplicities, and navigabilities as appropriate.

class CardioWorkout {
    public void doCardioWorkout() {
        warmUp();
        run();
        stretch();
    }

    private void warmUp() {
        System.out.println("Warming up.");
    }


    private void run() {
        System.out.println("Running.");
    }


    private void stretch() {
        System.out.println("Stretching.");
    }
}

class StrengthWorkout {  
        public void doStrengthWorkout() {
            warmUp();
            doPushUps();
            doSquats();
            stretch();
        }

        private void warmUp() {
            System.out.println("Warming up.");
        }


        private void doPushUps() {
            System.out.println("Doing push ups.");
        }


        private void doSquats() {
            System.out.println("Doing squats.");
        }


        private void stretch() {
            System.out.println("Stretching.");
        }
}