# Assignment3_31911077 **Repository Path**: hellolwp/assignment3_31911077 ## Basic Information - **Project Name**: Assignment3_31911077 - **Description**: assignment3_31911077 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-04-10 - **Last Updated**: 2023-04-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Assignment 3 by *your name* (*your ID number*) --- > Assignment3_31911077. Hello, teacher. Please review my homework, thank you ! > > The following secondary title represents Question number. > ## Question 1 - ### Question 1: Describe your strategy of creating the classes/interface. Which should be created first? Why? Which classes should be created next up to the last? Why? ​ In the command design pattern, the first class that should be created is the command interface. This interface defines the execute method that all the concrete command classes must implement. By creating the interface first, it allows the developer to focus on defining the contract of the commands, rather than on the details of the receiver and the invoker classes. ​ Next, the receiver class should be created. The receiver is the class that is responsible for performing the actual actions that are delegated by the commands. Typically, the receiver class represents a complex system that can be broken down into simple, well-defined actions. In this case, we have the `Television` class as the receiver. ​ After creating the receiver class, the concrete command classes can be created. These classes implement the command interface and encapsulate specific actions to be performed on the receiver object. In this case, we have `TurnOnCommand`, `TurnOffCommand`, and `SwitchChannelCommand` classes. ​ Finally, the invoker class should be created. This class is responsible for initiating the commands and executing them on the receiver object. In this case, we have the `Switch` class as the invoker, which receives command objects and executes the `execute()` method on them. ​ The reason for creating the classes in this order is that it follows the principles of the Single Responsibility Principle and Separation of Concerns. The command interface defines the behavior of the commands, the receiver class is responsible for executing the actions associated with the commands, the concrete command classes wrap these actions, and the invoker class initiates and executes the commands. By separating these concerns, the design becomes more modular, extensible, and easier to maintain. ## Question 2 - ### Question 2: What is a Command in Command Pattern? Explain Figure 1 in relation to your code. Use some snippets to support your claim. ​ In the Command Pattern, a Command is an object that encapsulates a request, command or operation, and is executed by an Invoker object. Figure 1 is a UML diagram that represents the Command Pattern. It presents four types of entities: the Invoker, the Command interface, ConcreteCommand classes, and the Receiver. In the context of the code provided, the `Command` interface represents the `Command` object in the Command Pattern. Here's a snippet of its code: ````java // Command interface public interface Command { public void execute(); } ```` ​ The `TurnOnCommand`, `TurnOffCommand`, and `SwitchChannelCommand` classes are the `ConcreteCommand` classes that implement the `Command` interface. These classes encapsulate the specific actions that need to be executed on the `Television` object. Here are snippets of the `TurnOnCommand` class as an example: ````java // Turn On Command public class TurnOnCommand implements Command { private Television television; public TurnOnCommand(Television television) { this.television = television; } @Override public void execute() { television.turnOn(); } } ```` ​ The `Switch` class serves as the Invoker in the pattern. It stores the references to the `Command` objects (passed as parameters to its constructor) and has methods to execute the commands on the receiver. Here's a snippet of the `turnOn()` method as an example: ````java public void turnOn() { turnOnCommand.execute(); } ```` ​ Finally, the `Television` class serves as the Receiver which performs the actions of the commands. Here's a snippet of the `turnOn()` method as an example: ````java public void turnOn() { System.out.println("Television is turned on"); } ```` ​ In summary, the Command Pattern follows the principles of encapsulation and loose coupling among objects. The Command object encapsulates a request to perform an action on a Receiver object, the Invoker object decouples the object requesting an action from the object that performs it, and the Receiver object performs the actual action. ## Question 3 - ### Question 3:What is a Request/ Document/ Receiver in Command Pattern? Explain Figure 2 in relation to your code. Use some snippets to support your claim. ​ In the Command Pattern, a Request or Document is the command to be performed, which is implemented through a `Command` interface. The Receiver is the object that performs the requested command. ​ Figure 2 is a sequence diagram that represents the flow of messages between the `Client`, `Invoker`, `Command`, and `Receiver` objects in the Command Pattern. In the context of the code provided, the `Television` class is the Receiver as it contains methods that perform the actions requested by the `TurnOnCommand`, `TurnOffCommand`, and `SwitchChannelCommand`. Here's a snippet of the `Command` interface representing the Request/Document in the Command Pattern: ````java // Command interface for request/document public interface Command { public void execute(); } ```` ​ Here's a snippet of the `Television` class serving as the Receiver: ````java // Receiver class public class Television { public void turnOn() { System.out.println("Television is turned on"); } public void turnOff() { System.out.println("Television is turned off"); } public void switchChannel() { System.out.println("Television is switching channel"); } } ```` ​ The `Client` class creates instances of the `Command` objects and the `Television` object as the Receiver. Here's a snippet of the `CommandPatternDemo` class serving as the Client: ````java // Client class public class CommandPatternDemo { public static void main(String[] args) { // Creating the receiver object Television television = new Television(); // Creating the command objects with the receiver object as parameter TurnOnCommand turnOnCommand = new TurnOnCommand(television); TurnOffCommand turnOffCommand = new TurnOffCommand(television); SwitchChannelCommand switchChannelCommand = new SwitchChannelCommand(television); // Creating the invoker object with the command objects as parameter Switch switchObj = new Switch(turnOnCommand, turnOffCommand, switchChannelCommand); // Calling methods on the invoker object switchObj.turnOn(); // Request/pass the turnOn Request/Document to the Invoker switchObj.switchChannel();// Request/pass the switchChannel Request/Document to the Invoker switchObj.turnOff(); // Request/pass the turnOff Request/Document to the Invoker } } ```` ​ The `Invoker` is represented by the `Switch` class, which has a reference to the `Command` object and executes the `execute()` method of the `Command` object. Here's a snippet of the `Switch` class serving as the Invoker: ````java // Invoker class public class Switch { private Command turnOnCommand; private Command turnOffCommand; private Command switchChannel; public Switch(Command turnOnCommand, Command turnOffCommand, Command switchChannel) { this.turnOnCommand = turnOnCommand; this.turnOffCommand = turnOffCommand; this.switchChannel = switchChannel; } public void turnOn() { turnOnCommand.execute(); // Execute the Command (turnOnCommand) } public void turnOff() { turnOffCommand.execute(); // Execute the Command (turnOffCommand) } public void switchChannel() { switchChannel.execute(); // Execute the Command (switchChannelCommand) } } ```` ​ In summary, the `Command` object represents the Request/Document that encapsulates the action to be performed, the `Television` object is the Receiver that performs the actual action of the Request/Document, the `Client` creates the `Command` and `Receiver` objects, and the `Switch` class is the Invoker that receives the `Command` object and subsequently calls the `execute()` method on the `Command` object to execute the Request/Document on the Receiver object. ## Question 4 - ### Question 4:What is a Client in Command Pattern? Explain Figure 3 in relation to your code. Use some snippets to support your claim. ​ In the Command Pattern, the Client is the object that creates the ConcreteCommand objects and sets their Receiver. The Client is responsible for creating and setting up the Invoker object and the ConcreteCommand objects. Figure 3 is a class diagram that represents the Command Pattern. In the context of the code provided, the `CommandPatternDemo` class serves as the Client. Here's a snippet of the `CommandPatternDemo` class serving as the Client: ````java // Client class public class CommandPatternDemo { public static void main(String[] args) { // Creating the receiver object Television television = new Television(); // Creating the command objects with the receiver object as parameter TurnOnCommand turnOnCommand = new TurnOnCommand(television); TurnOffCommand turnOffCommand = new TurnOffCommand(television); SwitchChannelCommand switchChannelCommand = new SwitchChannelCommand(television); // Creating the invoker object with the command objects as parameter Switch switchObj = new Switch(turnOnCommand, turnOffCommand, switchChannelCommand); // Calling methods on the invoker object switchObj.turnOn(); // Request/pass the turnOn Request/Document to the Invoker switchObj.switchChannel();// Request/pass the switchChannel Request/Document to the Invoker switchObj.turnOff(); // Request/pass the turnOff Request/Document to the Invoker } } ```` ​ In this implementation, the `CommandPatternDemo` class is the Client, creating the `Television` object as the Receiver, the `TurnOnCommand`, `TurnOffCommand`, and `SwitchChannelCommand` objects as ConcreteCommand objects, and the `Switch` object as the Invoker. ​ Here's a description of each of the Client's actions based on the code: - `Television television = new Television()`: The Client creates an instance of the `Television` class serving as the Receiver. - `TurnOnCommand turnOnCommand = new TurnOnCommand(television)`: The Client creates an instance of the `TurnOnCommand` class serving as a ConcreteCommand object and sets its Receiver object to the `Television` object. - `TurnOffCommand turnOffCommand = new TurnOffCommand(television)`: The Client creates an instance of the `TurnOffCommand` class serving as a ConcreteCommand object and sets its Receiver object to the `Television` object. - `SwitchChannelCommand switchChannelCommand = new SwitchChannelCommand(television)`: The Client creates an instance of the `SwitchChannelCommand` class serving as a ConcreteCommand object and sets its Receiver object to the `Television` object. - `Switch switchObj = new Switch(turnOnCommand, turnOffCommand, switchChannelCommand)`: The Client creates an instance of the `Switch` class serving as the Invoker object and sets its ConcreteCommand objects as parameters. - `switchObj.turnOn()`, `switchObj.switchChannel()`, and `switchObj.turnOff()`: The Client calls methods on the `Switch` object, which in turn calls the `execute()` method on the corresponding ConcreteCommand objects to perform the requested actions on the Receiver (the `Television` object). ## Question 5 - ### Question 5:What is an Invoker in Command Pattern? Explain Figure 4 in relation to your code. Use some snippets to support your claim. ​ In the Command Pattern, the Invoker is the object that invokes a Request/Document by calling the `execute()` method on the ConcreteCommand object. Figure 4 is a UML diagram that represents the Command Pattern. In the context of the code provided, the `Switch` class serves as the Invoker. Here's a snippet of the `Switch` class serving as the Invoker: ````java // Invoker class public class Switch { private Command turnOnCommand; private Command turnOffCommand; private Command switchChannel; public Switch(Command turnOnCommand, Command turnOffCommand, Command switchChannel) { this.turnOnCommand = turnOnCommand; this.turnOffCommand = turnOffCommand; this.switchChannel = switchChannel; } public void turnOn() { turnOnCommand.execute(); // Execute the Command (turnOnCommand) } public void turnOff() { turnOffCommand.execute(); // Execute the Command (turnOffCommand) } public void switchChannel() { switchChannel.execute(); // Execute the Command (switchChannelCommand) } } ```` ​ In this implementation, the `Switch` class is the Invoker that invokes the ConcreteCommand objects to perform the Request/Document on the Receiver (the `Television` object). ​ Here's a description of the `Switch` class's actions based on the code: - `Command turnOnCommand`, `Command turnOffCommand`, `Command switchChannel`: These are references to the `Command` interface which are passed as parameters in the `Switch` constructor. - `public void turnOn()`, `public void turnOff()`, `public void switchChannel()`: These methods call the `execute()` method on the corresponding ConcreteCommand objects (`turnOnCommand`, `turnOffCommand`, `switchChannelCommand`) to perform the requested action on the Receiver (the `Television` object). - `turnOnCommand.execute()`, `turnOffCommand.execute()`, `switchChannel.execute()`: These lines execute the `execute()` method on the corresponding ConcreteCommand objects, which in turn perform the actions on the Receiver object. ​ In summary, the `Switch` class serving as the Invoker in the Command Pattern decouples the object that requests an action from the object that performs it, by storing ConcreteCommand objects and executing the `execute()` method on the `ConcreteCommand` object. By doing so, it can perform the desired action on the Receiver object without knowing the details of that object's implementation. ## Question 6 - ### Question 6: What method did you add in Television class? Did you add a new Class/Interface? Why? What classes were updated because of the new method you added? Describe the changes. Use some snippets to support your claim. ​ I added a new method `adjustVolume()` to the `Television` receiver class. Here's the code snippet for the updated `Television` class: ````java // Receiver class Television public class Television { // Method to turn on television public void turnOn() { System.out.println("Television is turned on"); } // Method to turn off television public void turnOff() { System.out.println("Television is turned off"); } // Method to switch channel on television public void switchChannel() { System.out.println("Television is switching channel"); } // Method to adjust volume on television public void adjustVolume() { System.out.println("Television volume adjusted"); } } ```` ​ I also added a new class `AdjustVolumeCommand` that implements the `Command` interface to encapsulate the `adjustVolume()` method of the `Television` object. Here's the code snippet for the updated `AdjustVolumeCommand` class: ````java // Concrete AdjustVolumeCommand class implementing Command interface public class AdjustVolumeCommand implements Command { private Television television; // Constructor to set Television object for AdjustVolumeCommand public AdjustVolumeCommand(Television television) { this.television = television; } // Method to execute AdjustVolumeCommand @Override public void execute() { television.adjustVolume(); } } ```` ​ Along with the `AdjustVolumeCommand` class, the `Switch` invoker class was also updated to include the `adjustVolumeCommand` field and its respective `adjustVolume()` method. Here's the code snippet for the updated `Switch` class: ````java // Invoker class Switch public class Switch { private Command turnOnCommand; private Command turnOffCommand; private Command switchChannelCommand; private Command adjustVolumeCommand; // Constructor to set commands public Switch(Command turnOnCommand, Command turnOffCommand, Command switchChannelCommand, Command adjustVolumeCommand) { this.turnOnCommand = turnOnCommand; this.turnOffCommand = turnOffCommand; this.switchChannelCommand = switchChannelCommand; this.adjustVolumeCommand = adjustVolumeCommand; } // Method to turn on Television public void turnOn() { turnOnCommand.execute(); } // Method to turn off Television public void turnOff() { turnOffCommand.execute(); } // Method to switch channel on Television public void switchChannel() { switchChannelCommand.execute(); } // Method to adjust volume on Television public void adjustVolume() { adjustVolumeCommand.execute(); } } ````