Factory Pattern Example in Java Here is the requirements: - Its constructor shou
ID: 3559921 • Letter: F
Question
Factory Pattern Example in Java
Here is the requirements:
- Its constructor should have
1) override the two-parameter constructor of the Edge280
2) should be protected, following the factory pattern
3) should rely on the super constructor to do the Work
- should override construct() method from Edge280, following factory pattern
-should have getter and setter for weight field.
Here is my Code:
public class WeightedEdge280 extends Edge280 {
protected double weight;
/**
* @param v1
* @param v2
*/
protected WeightedEdge280(Vertex280 v1, Vertex280 v2) {
super(v1, v2);
// TODO Auto-generated constructor stub
}
/*
public static Edge280 construct(Vertex280 v1, Vertex280 v2){
return new WeightedEdge280(v1,v2);
}*/
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
UML Diagram:
Explanation / Answer
CarType.java will hold the types of car and will provide car types to all other classes
Car.java is parent class of all car instances and it will also contain the common logic applicable in car making of all types.
LuxuryCar.java is concrete implementation of car type LUXURY
SmallCar.java is concrete implementation of car type SMALL
SedanCar.java is concrete implementation of car type SEDAN
CarFactory.java is our main class implemented using factory pattern. It instantiates a car instance only after determining its type.
In TestFactoryPattern.java, we will test our factory code. Lets run this class.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.