计算机代考程序代写 Java algorithm SOFT2201 Tutorial 6 Behavioural Design Patterns 1

SOFT2201 Tutorial 6 Behavioural Design Patterns 1
Behavioural Design Patterns 1
Behavioural patterns allows you to encode behaviour within objects to be executed at run time. Be- havioural patterns like State and Strategy pattern allow the programmer to utilise input at runtime to change the object’s behaviour.
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.
1

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 can make reasonable assumptions regarding the Racecar class and DisplayMethod enum (the current code successfully compiles and runs). Similar to your tasks last week, consider this code and how its current version provides extensibility (identify the SOLID principle that most applies here and why). What would the Strategy pattern look like if we applied it here?
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; }
}
SOFT2201 Behavioural Design Patterns 1
Software Construction
Page 2 of 7

Question 2: Strategy Balls
Last week you made a ball accelerate with a gravity-like force and bounce off the walls. Somebody has extended your code to handle multiple bouncing balls. You can find this code on canvas, title StrategyBalls.zip. You need to refactor this code to use the Strategy pattern.
The strategies you need to implement will need to be settable on each Ball by BallPit in the manner of your choosing (you are told which colour ball to apply what strategy to, but you must not hard code this in the Balls themselves). The strategies each involve the Balls changing their own movement – however they are still bound by the BallPit rules – they cannot go past the edge, they must be accelerated by a gravity-like force of 1.0 units per second in x and y directions, and if another ball hits them they will bounce using the energy-conserving algorithm in BallPit (your strategies don’t need to check this, the BallPit code should enforce it).
The Balls each have the ability in their think() method to accelerate by 1.0 unit per second per axis (that is, they can add or subtract anywhere from 0 to 1/frameRate each call from their own xVel and yVel per call to think()). (Note: that negative velocities are normal.)
The strategies you must implement are as follows:
• Black ball: Adds no acceleration of its own. Note that this doesn’t mean that it doesn’t move.
• Red ball: Accelerates as fast as it can to the NW, NE, SW, or SE depending on which corner it is closest to. Not that this isn’t the same as accelerating directly toward the corner, and it’s easier.
• Blue ball: Has an initial acceleration of 0.0. When it bounces off another ball it starts to slow, and continues to slow until it stops. Once velocity is roughly equal to 0, acceleration returns to 0.
If you want to be fancy, there are two optional feature. You do not need to implement the optional features:
• Green ball: Accelerates when ’w’, ’a’, ’s’, or ’d’ keys are pressed. The Ballboy code will give you some pointers for how to detect and handle keyboard input.
• Yellow ball: When the user clicks on another ball, the yellow ball will accelerate towards it.
One of the key questions you need to answer is ’how will the Balls learn about their environment?’. There are a few ways to do this, some representing good design, and some representing bad design – choose wisely!
You will need your code and a final UML diagram for the exercise next week. From a partial marks perspective you should start by implementing the Strategy pattern with the Black ball strategy applied to all balls, then work on adding the Red and Blue strategies. Check that your code works when ’gradle run’ is used, and that it does not require versions of Java other than 11.
SOFT2201 Behavioural Design Patterns 1
Software Construction Page 3 of 7

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 current design matches the code in the next question, draw out your redesign in UML (again, before diving straight into the code!)
SOFT2201 Behavioural Design Patterns 1
Software Construction Page 4 of 7

Question 4: Switchboard Operator
After submitting a new design, Telstro has contracted you to overhaul their current software sys- tem.
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(); } SOFT2201 Behavioural Design Patterns 1 Software Construction Page 5 of 7 } } 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; } } SOFT2201 Behavioural Design Patterns 1 Software Construction Page 6 of 7 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; } } SOFT2201 Behavioural Design Patterns 1 Software Construction Page 7 of 7