Part 1 | Part 2
In part 1 you read what is defensive copy and how to implement it. In this part you will see how to implement defensive copy using copy constructors.
Here we have added a copy constructors to Engine class which will be used to implement defensive copy mechanism in Car class.
Engine.java
Car.java
Client.java
Car Engine Location Before =Front
Car Engine Location After =Front
Note that here Car class did not get affected.
Now change your Client class as following to see the effect of Setter Method:
Client.java
Car Engine Location Before =Front
Car Engine Location After =Front
Notice that engine location of engine of car did not get changed upon changing engine location on engine2.
For seeing the effect of Getter Method change Client class as:
Client.java
Car Engine Location Before =Front
Car Engine Location After =Front
As you are seeing in each of the output above, car object is not getting effected now.
Part 1 | Part 2
In part 1 you read what is defensive copy and how to implement it. In this part you will see how to implement defensive copy using copy constructors.
Here we have added a copy constructors to Engine class which will be used to implement defensive copy mechanism in Car class.
Engine.java
class Engine {
private String engineLocation;
/*Simple Constructor*/
Engine(String engineLocation){
this.engineLocation=engineLocation;
}
/*Copy Constructor */
Engine(Engine engine){
this.engineLocation = engine.getEngineLocation();
}
public void setEngineLocation(String engineLocation){
this.engineLocation=engineLocation;
}
public String getEngineLocation(){
return engineLocation;
}
}
private String engineLocation;
/*Simple Constructor*/
Engine(String engineLocation){
this.engineLocation=engineLocation;
}
/*Copy Constructor */
Engine(Engine engine){
this.engineLocation = engine.getEngineLocation();
}
public void setEngineLocation(String engineLocation){
this.engineLocation=engineLocation;
}
public String getEngineLocation(){
return engineLocation;
}
}
Car.java
class Car {
private String make;
private Engine engine;
Car(String make,Engine otherEngine){
this.make=make;
/*Using copy constructor of Engine*/
this.engine = new Engine(otherEngine);
}
public void setMake(String make){
this.make=make;
}
public String getMake(){
return make;
}
public void setEngine(Engine otherEngine){
/*Using copy constructor of Engine*/
this.engine = new Engine(otherEngine);
}
public Engine getEngine(){
/*Using copy constructor of Engine*/
Engine engineTemp = new Engine(engine); /*'engine' is a member of Car class*/
return engineTemp;
}
}
private String make;
private Engine engine;
Car(String make,Engine otherEngine){
this.make=make;
/*Using copy constructor of Engine*/
this.engine = new Engine(otherEngine);
}
public void setMake(String make){
this.make=make;
}
public String getMake(){
return make;
}
public void setEngine(Engine otherEngine){
/*Using copy constructor of Engine*/
this.engine = new Engine(otherEngine);
}
public Engine getEngine(){
/*Using copy constructor of Engine*/
Engine engineTemp = new Engine(engine); /*'engine' is a member of Car class*/
return engineTemp;
}
}
Client.java
public class Client{
public static void main(String args[]){
Engine engine= new Engine("Front");
Car car = new Car("Maruti",engine);
System.out.println("Car Engine Location Before ="+car.getEngine().getEngineLocation());
engine.setEngineLocation("Back");
System.out.println("Car Engine Location After ="+car.getEngine().getEngineLocation());
}
}
public static void main(String args[]){
Engine engine= new Engine("Front");
Car car = new Car("Maruti",engine);
System.out.println("Car Engine Location Before ="+car.getEngine().getEngineLocation());
engine.setEngineLocation("Back");
System.out.println("Car Engine Location After ="+car.getEngine().getEngineLocation());
}
}
Output:
Car Engine Location Before =Front
Car Engine Location After =Front
Note that here Car class did not get affected.
Now change your Client class as following to see the effect of Setter Method:
Client.java
public class Client{
public static void main(String args[]){
Engine engine= new Engine("Front");
Car car = new Car("Maruti",engine);
Engine engine2= new Engine("Front");
car.setEngine(engine2);
System.out.println("Car Engine Location Before ="+car.getEngine().getEngineLocation());
engine2.setEngineLocation("back");
System.out.println("Car Engine Location After ="+car.getEngine().getEngineLocation());
}
}
public static void main(String args[]){
Engine engine= new Engine("Front");
Car car = new Car("Maruti",engine);
Engine engine2= new Engine("Front");
car.setEngine(engine2);
System.out.println("Car Engine Location Before ="+car.getEngine().getEngineLocation());
engine2.setEngineLocation("back");
System.out.println("Car Engine Location After ="+car.getEngine().getEngineLocation());
}
}
Output:
Car Engine Location Before =Front
Car Engine Location After =Front
Notice that engine location of engine of car did not get changed upon changing engine location on engine2.
For seeing the effect of Getter Method change Client class as:
Client.java
public class Client{
public static void main(String args[]){
Engine engine= new Engine("Front");
Car car = new Car("Maruti",engine);
Engine engine2= car.getEngine();
System.out.println("Car Engine Location Before ="+car.getEngine().getEngineLocation());
engine2.setEngineLocation("Back");
System.out.println("Car Engine Location After ="+car.getEngine().getEngineLocation());
}
}
public static void main(String args[]){
Engine engine= new Engine("Front");
Car car = new Car("Maruti",engine);
Engine engine2= car.getEngine();
System.out.println("Car Engine Location Before ="+car.getEngine().getEngineLocation());
engine2.setEngineLocation("Back");
System.out.println("Car Engine Location After ="+car.getEngine().getEngineLocation());
}
}
Output:
Car Engine Location Before =Front
Car Engine Location After =Front
As you are seeing in each of the output above, car object is not getting effected now.
Part 1 | Part 2
I would like to know your comments and if you liked the article then please share it on social networking buttons.
No comments:
Post a Comment