I'm having trouble understanding the whole process of cloning in Java. I've read everything I can find and I still don't get it. I need to build a "Defensive" class using cloning. I'm not exactly sure how to do this I get the general idea that I need to make the variables immutable so that once they are edited once the user will not be able to mess with them. Here is my code if you need any other information let me know. Thanks in advance.
that is one of four classes I can post the others if needed here is my Main...Code:/*
* Memory.java
*/
package defensive;
public final class Memory implements Cloneable{
//Attributes
private String partNumber;
private String model;
private int capacity;
// Constructors
public Memory() {
this("00000000", "Dummy", 0);
}
public Memory(String partNumber, String model, int capacity) {
this.partNumber = partNumber;
this.model = model;
this.capacity = capacity;
}
//Accessors
public String getPartNumber() {
return partNumber;
}
public String getModel() {
return model;
}
public int getCapacity() {
return capacity;
}
//Mutators
public final void setPartNumber(String partNumber) {
this.partNumber = partNumber;
}
public final void setModel(String model) {
this.model = model;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
@Override
public Object clone() throws CloneNotSupportedException {
Memory s2 = (Memory)super.clone();
//s2.capacity = (Capacity)this.capacity.clone();
return s2;
}
// Behaviors
public String toString() {
return "Model: " + model + " (" + capacity +
" GB)" + " - P/N: " + partNumber;
}
}
If any other info is needed let me know thanks.Code:/*
* Main.java
*/
package defensive;
import java.lang.String;
public class Main {
public static void main(String[] args) {
boolean TOGGLE = false; //true = Demo, false = Test
if (TOGGLE) {
//Brittle Demo
System.out.println("===== Begin Brittle Demo =====\n");
//Build a brittle computer object
Cpu brittleCpu = new Cpu("11111", "22222", 3.3);
Memory brittleMemory = new Memory("44444", "55555", 6);
MotherBoard brittleMotherBoard =
new MotherBoard("77777", brittleCpu, brittleMemory);
Computer brittleComputer =
new Computer("88888", brittleMotherBoard);
//Show original configuration
System.out.println("Original Configuration\n" + brittleComputer + "\n");
//Break it
brittleMemory.setCapacity(100000000);
//Show broken configuration
System.out.println("Broken Configuration\n" + brittleComputer + "\n");
//Create a replacement CPU and install in brittleMotherBoard
Cpu replacementCpu = new Cpu("AAAAA", "BBBBB", 999.0);
brittleComputer.getMotherBoard().setCpu(replacementCpu);
//Show new configuration
System.out.println("New Configuration\n" + brittleComputer + "\n");
//Modify replacementCpu
replacementCpu.setModel("ZZZZZ");
//Show new broken configuration
System.out.println("New Broken Configuration\n" + brittleComputer + "\n");
//Create String representing memory model data * use to modify memory
String memoryModelChange = "WWWWW";
brittleComputer.getMotherBoard().getMemory().setModel(memoryModelChange);
//Show current configuration
System.out.println("Current Configuration\n" + brittleComputer + "\n");
//Modify memoryModelChange
memoryModelChange += "*****";
//Show current configuration
System.out.println("Unchanged Configuration\n" + brittleComputer + "\n");
}
else {
//Begin Test
System.out.println("===== Begin Bullet-Proof Testing =====\n");
//Create test Cpu object and print state
Cpu testCpu = new Cpu("GX9764CPU", "Intel 3200G", 3.2);
System.out.println(testCpu.toString() + "\n");
//Create test Memory object and print state
Memory testMemory = new Memory("MD400SD", "Micron SDRAM", 4);
System.out.println(testMemory.toString() + "\n");
//Create test MotherBoard object and print state
MotherBoard testMotherBoard = new MotherBoard("Intel XP3300", testCpu,
testMemory);
System.out.println(testMotherBoard.toString() + "\n");
//Create test Computer object and print state
Computer testComputer = new Computer("Dell PowerEdge 2100",
testMotherBoard);
System.out.println(testComputer.toString() + "\n");
//Test #1
System.out.println("*** Test #1 ***\n");
//Change state of testCpu and testMemory
testCpu.setModel("Broken");
testMemory.setCapacity(0);
//Show changes have no impact on testComputer object
System.out.println(testCpu + "\n");
System.out.println(testMemory + "\n");
System.out.println(testComputer + "\n");
//Test #2
System.out.println("*** Test #2 ***\n");
//Change state of testMotherBoard
Cpu newCpu = new Cpu("Broken", "Broken", 0.0);
testMotherBoard.setCpu(newCpu);
//Show changes have no impact on testComputer object
System.out.println(testMotherBoard + "\n");
System.out.println(testComputer + "\n");
//Test #3
System.out.println("*** Test #3 ***\n");
//Get aggregate object of testComputer and modify it
MotherBoard newMotherBoard = testComputer.getMotherBoard();
//Show current state of testComputer's motherboard'
System.out.println(newMotherBoard + "\n");
//Change the CPU in the acquired motherboard object
newMotherBoard.setCpu(newCpu);
//Show current state of acquired motherboard object
System.out.println(newMotherBoard + "\n");
//Show changes have no impact on testComputer object
System.out.println(testComputer + "\n");
//Test #4
System.out.println("*** Test #4 ***\n");
//Swap out the CPU in the computer
Cpu swapCpu = new Cpu("AZ4500CPU", "Intel 5000G", 5.0);
MotherBoard tempMotherBoard = testComputer.getMotherBoard();
tempMotherBoard.setCpu(swapCpu);
testComputer.setMotherBoard(tempMotherBoard);
//Show CPU swapped out
System.out.println(testComputer + "\n");
//Modify swapCpu and show no impact on testComputer
swapCpu.setPartNumber("None");
System.out.println(testComputer + "\n");
}
}
}

