Tôi đã trải qua cuốn sách Java hiệu quả và tạo ghi chú cho tài liệu tham khảo trong tương lai của tôi, tôi đã xem qua Mẫu tạo hình.Mẫu Trình tạo: biến thể nào được ưa thích hơn?
Tôi hiểu nó là gì và cách giả sử nó được sử dụng như thế nào.Trong quá trình tôi tạo ra hai biến thể mẫu của mẫu trình xây dựng.
Tôi cần trợ giúp liệt kê những khác biệt và lợi thế của từng loại? Tôi chắc chắn nhận thấy rằng, Example 1
cho thấy ít phương pháp hơn, có ít hạn chế hơn và chung chung hơn, bằng cách cho phép nó được sử dụng linh hoạt hơn.
Hãy chỉ ra những thứ khác mà tôi đã bỏ lỡ?
Ví dụ 1
package item2;
/**
* @author Sudhakar Duraiswamy
*
*/
public class Vehicle {
private String type;
private int wheels;
interface Builder<T>{
public T build();
}
public static class CarBuilder implements Builder<Vehicle>{
private String type;
private int wheels;
CarBuilder createVehicle(){
this.type= "Car";
return this;
}
CarBuilder addWheels(int wheels){
this.wheels = wheels;
return this;
}
public Vehicle build(){
Vehicle v = new Vehicle();
v.type = type;
v.wheels = wheels;
return v;
}
}
public static class TruckBuilder implements Builder<Vehicle>{
private String type;
private int wheels;
TruckBuilder createVehicle(){
this.type= "Truck";
return this;
}
TruckBuilder addWheels(int wheels){
this.wheels = wheels;
return this;
}
public Vehicle build(){
Vehicle v = new Vehicle();
v.type = type;
v.wheels = wheels;
return v;
}
}
public Vehicle(){
}
public static void main(String[] args) {
//This builds a car with 4 wheels
Vehicle car = new Vehicle.CarBuilder().createVehicle().addWheels(4).build();
//THis builds a Truck with 10 wheels
Vehicle truck = new Vehicle.TruckBuilder().createVehicle().addWheels(10).build();
}
}
Ví dụ 2
package item2;
/**
* @author Sudhakar Duraiswamy
*
*/
public class Vehicle2 {
private String type;
private int wheels;
interface Builder<T>{
public T build();
public String getType();
public int getWheels() ;
}
public static class CarBuilder implements Builder<Vehicle2>{
private String type;
private int wheels;
public String getType() {
return type;
}
public int getWheels() {
return wheels;
}
CarBuilder createVehicle(){
this.type= "Car";
return this;
}
CarBuilder addWheels(int wheels){
this.wheels = wheels;
return this;
}
public Vehicle2 build(){
return new Vehicle2(this);
}
}
public static class TruckBuilder implements Builder<Vehicle2>{
private String type;
private int wheels;
public String getType() {
return type;
}
public int getWheels() {
return wheels;
}
TruckBuilder createVehicle(){
this.type= "Truck";
return this;
}
TruckBuilder addWheels(int wheels){
this.wheels = wheels;
return this;
}
public Vehicle2 build(){
return new Vehicle2(this);
}
}
public Vehicle2(Builder<? extends Vehicle2> builder){
Vehicle2 v = new Vehicle2();
v.type = builder.getType();
v.wheels = builder.getWheels();
}
public Vehicle2(){
}
public static void main(String[] args) {
//This builds a car with 4 wheels
Vehicle2 car = new Vehicle2.CarBuilder().createVehicle().addWheels(4).build();
//THis builds a Truck with 10 wheels
Vehicle2 truck = new Vehicle2.TruckBuilder().createVehicle().addWheels(10).build();
}
}
Nghiêm túc xuống bỏ phiếu mà không bình luận để lại là què – Sudhakar
Guys, nếu bạn có thể để lại ý kiến là tại sao câu hỏi này là không thích hợp, nó sẽ thực sự giúp tôi và những người khác – Sudhakar
Bạn nên đi qua trang web này http: // en .wikipedia.org/wiki/Builder_pattern # Java để xem ví dụ thích hợp cho việc triển khai 'Trình tạo mẫu'. –