辅导案例-SOFT2201

  • May 15, 2020

SOFT2201 Tutorial 6Behavioural Design Patterns 1Factory Method ExerciseBefore we move on to this week’s patterns you will be doing an exercise using your pre-made UMLdiagrams. You will have some time before this begins to bring up anything from the previous tutorialfor clarification (and being open-book open-class open-tutor you can continue doing so during theexercise itself). You are allowed to modify or update your UML diagrams at any point up until yousubmit your exercise.Behavioural Design Patterns 1Behavioural 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 tochange the object’s behaviour.Strategy PatternOne thing to note is that behavioural patterns encapsulate information required to act in differentways. For strategy pattern, selecting an algorithm (or method) based on run-time instructions, thisinformation is commonly encoded within the invoking method.1SOFT2201 Behavioural Design Patterns 1Question 1: Ranking SelectionWe have been given a set of lap times from each race car in the race. We want to present differentinformation 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 (thecurrent code successfully compiles and runs). Similar to your tasks last week, consider this code andhow its current version provides extensibility (identify the SOLID principle that most applies hereand 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;}}Software Construction Page 2 of 7SOFT2201 Behavioural Design Patterns 1Question 2: Tic Tac ToeYou are to refactor the design of a tic tac toe game (the current version of the code is available onCanvas). We are currently in the process of implementing single player. You will need to redesignthe application to incorporate an AI player and allow for different difficulty settings (or strategies).Before diving straight into the code, draw out your redesign using UML. Assume the followingstrategies are available:• Easy• NormalQuestion 3: Implement the AIGiven the existing code base of the tic tac toe game, implement the strategy pattern as well as eachstrategy.Each strategy is selectable by the player for the AI to employ.An Easy strategy is where the AI randomly places their symbol on an available board piece.A Normal strategy will attempt to place their symbol randomly, unless the computer can win in thenext turn. If the AI knows that they can win in the next turn, it will attempt to do so.(If you feel up to a challenge, implement a Hard strategy that leverages one or two more difficultstrategies)State PatternIn regards to Strategy pattern, the selection or retrieval of the behavioural objects is known by asingle object maintaining links to them. However, State pattern encodes state transition on each stateobject which will affect the behaviour of the object maintaining that state.Question 4: Switchboard DesignYour company Telstro is redesigning their old switchboard to operate on a completely softwarebased system. The switchboard accepts events from phone ports which will then require a connectionbetween the ports to be constructed. Assuming the current design matches the code in the nextquestion, draw out your redesign in UML (again, before diving straight into the code!)Software Construction Page 3 of 7SOFT2201 Behavioural Design Patterns 1Question 5: Switchboard OperatorAfter 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();}Software Construction Page 4 of 7SOFT2201 Behavioural Design Patterns 1public 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;}}Software Construction Page 5 of 7SOFT2201 Behavioural Design Patterns 1public 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; }}Software Construction Page 6 of 7SOFT2201 Behavioural Design Patterns 1Exercise preparationIn order to complete the exercise next week, you will need to do a little refactoring work before thetutorial (just like you did this week). In this week’s module you will notice somebody has extendedyour Module 4 exercise to handle a few bouncing balls - you need to refactor this to use the Strategypattern. There is more thinking to do here than at first glance, so don’t leave this to the last minute -with the upside that you may find a use for what you have learned in a sticky situation.The strategies you need to implement will need to be settable on each Ball by BallPit in themanner of your choosing (you are told which colour ball to apply what strategy to, but you must nothard code this in the Balls themselves). The strategies each involve the Balls changing their ownmovement - however they are still bound by the BallPit rules - they cannot go past the edge, andif another ball hits them they will bounce using the energy-conserving algorithm in BallPit (yourstrategies 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 per second per axis(that is, they can add or subtract anywhere from 0 to 0.017 each from their own xVel and yVel percall to think()). (Note: that negative velocities are normal.)The strategies you must implement are as follows:• Black ball: Accelerates as fast as it can either NW, NE, SW, or SE depending on which cornerit is closest to• Blue ball: Tries to stop when it bounces off another ball, keeps going in all other cases (includ-ing bouncing off a wall)• Red ball: Does nothing (note this doesn’t mean stop, it means keep moving the same speed anddirection it is moving)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 marksperspective you should start by implementing the Strategy pattern with the Red ball strategy applied toall balls, then work on adding the Black and Blue strategies. Ensure you have running code available.Software Construction Page 7 of 7

LATEST POSTS
MOST POPULAR

ezAce多年来为广大留学生提供定制写作、留学文书定制、语法润色以及网课代修等服务,超过200位指导老师为您提供24小时不间断地服务。