Especificar os tipos de objetos a serem criados usando uma instância-protótipo e criar novos objetos pela cópia desse protótipo.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prototype2;
/**
*
* @author patrick
*/
public abstract class CarPrototype {
private String model;
private String factory;
private String category;
private String year;
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getFactory() {
return factory;
}
public void setFactory(String factory) {
this.factory = factory;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String showInformation() {
return "Model:" + this.getModel()+ " \nFactory:" + this.getFactory() + "\nCategory: " + this.getCategory()+ "\nYear: " + this.getYear();
}
public abstract CarPrototype getClone();
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prototype2;
/**
*
* @author patrick
*/
public class Client {
public static void main(String[] args) {
FiatPrototype protFiat = new FiatPrototype();
CarPrototype car1 = protFiat.getClone();
car1.setModel("Palio Fire");
car1.setCategory("Hatch");
car1.setYear("2014");
CarPrototype car2 = protFiat.getClone();
car2.setModel("Siena");
car2.setCategory("Sedan");
car2.setYear("2014");
System.out.println(car1.showInformation());
System.out.println("=========================");
System.out.println(car2.showInformation());
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prototype2;
/**
*
* @author patrick
*/
public class FiatPrototype extends CarPrototype {
public FiatPrototype() {
this.setModel("Fiat 0001");
this.setFactory("Fiat");
this.setCategory("Hatch");
this.setYear("2000");
}
protected FiatPrototype(FiatPrototype fiat_Prototype) { //constructor by copy
this.setModel(fiat_Prototype.getModel());
this.setFactory(fiat_Prototype.getFactory());
this.setCategory(fiat_Prototype.getCategory());
this.setYear(fiat_Prototype.getYear());
}
public FiatPrototype getClone() {
return new FiatPrototype(this);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prototype2;
/**
*
* @author patrick
*/
public class VolksPrototype extends CarPrototype {
public VolksPrototype() {
this.setModel("Volks 0001");
this.setFactory("volks");
this.setCategory("Hatch");
this.setYear("2000");
}
protected VolksPrototype(VolksPrototype volks_Prototype) { //constructor by copy
this.setModel(volks_Prototype.getModel());
this.setFactory(volks_Prototype.getFactory());
this.setCategory(volks_Prototype.getCategory());
this.setYear(volks_Prototype.getYear());
}
public VolksPrototype getClone() {
return new VolksPrototype(this);
}
}