Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

SOFT2201/COMP9201

Week 6 Tutorial

Behavioural Design Patterns

Overview

Behavioural patterns allow you to encode behaviour within objects to be executed at run time. Be- havioural patterns like State and Strategy patterns allow the programmer to utilise input at runtime to change the object’s behaviours.

Strategy Pattern

One thing to note is that behavioural patterns encapsulate information required to act in different ways. For strategy pattern, selecting an algorithm (or method) based on run-time instructions, this information is commonly encoded within the invoking method.

Question 1: Ranking Selection

We have been given a set of lap times from each race car in the race. We want to present different information based on different requests. We want to display three kinds of data: Fastest Lap Time, Fastest Race Time and Slowest Race Time,

You are now given the Race class as follows 1. You can make any reasonable assumptions regarding the Racecar class and the DisplayMethod enum as long as they together could make the current given code successfully compiles and runs.

• Write down the Racecar class and the DisplayMethod enum to make the code work.

• Consider the whole codebase and think about how this current version provides extensibility

• Identify the SOLID principle that most applies here and explain the reason why you think so

public class Race {

private List  cars  = new ArrayList<>();

public List  displayData (DisplayMethod  method)  { List  times  = new ArrayList<>();

if (method  ==  DisplayMethod .FastestLapTime)  {

for (Racecar  car   :  cars)  {

times .add (car .fastestLapTime ());

}

Collections .sort (times);

} else if (method  ==  DisplayMethod .FastestRaceTime )  { for (Racecar  car   :  cars)  {

times .add (car .raceTime ());

}

Collections .sort (times);

} else if (method  ==  DisplayMethod .SlowestRaceTime )  { for (Racecar  car   :  cars)  {

times .add (car .raceTime ());

}

Collections .sort (times);

Collections .reverse (times);

}

return times ;

}

}

Question 2: Refactor on Ranking Selection

Based on your knowledge of strategy pattern, refactor the design in the above codebase you have written in Question 1 by utilising strategy design pattern.

State Pattern

In regards to Strategy pattern, the selection or retrieval of the behavioural objects is known by a single object maintaining links to them. However, State pattern encodes state transition on each state object which will affect the behaviour of the object maintaining that state.

Question 3: Switchboard Design

Your company Telstro is redesigning their old switchboard to operate on a completely software based system. The switchboard accepts events from phone ports which will then require a connection between the ports to be constructed. Assuming the old design matches the code given in the Question 4, draw out your redesign in UML before diving straight into the code!

Question 4: Switchboard Operator

After submitting the design, Telstro has contracted you to overhaul their current software system. Please implement your redesign idea based on the following code.

import java .util .Map;

import java .util .HashMap;

import java .util .Scanner ;

public class Switchboard {

private Map  ports ;

public Switchboard (int n)  {

this .ports  = new HashMap<>();

for (int i  =  0 ;  i  < n; i++) {

this .ports .put ( ""  +  i , new PhonePort ( ""  +  i)); }

}

public void connect (String  from ,  String  to)  {

PhonePort  source  =  ports .get (from);

PhonePort  destination  =  ports .get (to);

PhoneLine  line  = new PhoneLine (source ,  destination); source .connect (line);

destination .connect (line);

}

public void disconnect (String  address)  {

PhonePort  source  =  ports .get (address);

source .disconnect ();

}

public void hold (String  address)  {

PhonePort  source  =  ports .get (address);

source .hold ();

}

public void resume (String  address)  {

PhonePort  source  =  ports .get (address);

source .resume ();

}

public static void main (String []  args)  {                      Switchboard  switchboard  = new Switchboard ( 10 ); Scanner  input  = new Scanner (System .in); while (input .hasNextLine ())  {

String []  words  =  input .nextLine () .split ( "  " ); if (words .length  ==  3 )  {

if (words [ 0 ] .equals ( "CONNECT" ))  {                           switchboard .connect (words [ 1 ],  words [2 ]);

} else if (words [ 0 ] .equals ( "DISCONNECT" ))  { switchboard .disconnect (words [ 1 ]);

}

} else if (words .length  ==  2 )  {

if (words [ 0 ] .equals ( "HOLD" ))  {

switchboard .hold (words [ 1 ]);                } else if (words [ 0 ] .equals ( "RESUME" ))  {

switchboard .resume (words [ 1 ]);

}

}

}

}

}

public class PhonePort {

private String  address ;

private PhoneLine  line ;

private PortState  state ;

private static enum PortState  {

Waiting ,  Busy ,  Holding

}

public PhonePort (String  address)  {

this .address  =  address ;

line  = null;

state  =  PortState .Waiting;

}

public void connect (PhoneLine  line )  {

if (state  ==  PortState .Waiting)  {

state  =  PortState .Busy;

this .line  =  line ;

}

}

public void disconnect ()  {

if (state  ==  PortState .Busy)  {

state  =  PortState .Waiting;

line  = null;

}

}

public void hold ()  {

if (state  ==  PortState .Busy)  {

state  =  PortState .Holding;

}

}

public void resume ()  {

if (state  ==  PortState .Holding)  {

state  =  PortState .Busy;

}

}

}

public class PhoneLine {

private PhonePort  from ;

private PhonePort  to ;

public PhoneLine (PhonePort  from ,  PhonePort  to )  {

this .from  =  from;

this .to  =  to;

}

public PhonePort  source ()  { return from;  }

public PhonePort  destination ()  { return to;  }

}

JavaFX Application

Question 5: Bouncy Ball Revisited

JavaFX (JavaFX 17 API document) provides users an easy way to handle events within its applica- tions. Specifically, users can create and register event handlers to respond to KeyEvent, MouseEvent, Action Event, Drag and Drop Events and many more. Various Event Handler properties can be found in Node class.

You have done the implementation of a bouncy ball in the Question 4 of week 4 tutorial. It creates a program that bounces a ball from the bottom of the screen to the top and back again. In this exercise, you are required to add a label on the screen, by clicking on which to active the bouncing of the ball.