Simple factory mode example
/**
Simple factory model: A factory produces three types of high-end automobile (TopCar), middle automobile (MidCar), and low-end automobile (LowCar)
A method is required for automobile production. An ICar interface class is defined to define the method name.
Three kinds of vehicles to implement these methods.
Define a factory class to create car objects
The above is the prototype of a simple factory model. That is, the program developer can create corresponding objects by calling the factory model without repeatedly creating objects.
Note that the class defined with public should be an independent *. java file. There is no distinction here.
*/
Public interface ICar {
// This class defines the method for creating a car, which is not defined here.
}
Public class SimpleFactory {
Public static final String TOPTYPE = toptype;
Public static final String MIDTYPE = midtype;
Public static final String LOWTYPE = lowtype;
Public static ICar create (String mark ){
ICar obj = null;
If (mark. equals (TOPTYPE )){
Obj = new TopCar ();
System. out. println (TOPTYPE );
}
Else if (mark. equals (MIDTYPE )){
Obj = new TopCar ();
System. out. println (MIDTYPE );
}
Else if (mark. equals (LOWTYPE )){
Obj = new TopCar ();
System. out. println (LOWTYPE );
}
Return obj;
}
}
Public class TopCar implements ICar {
}
Public class MidCar implements ICar {
}
Public class LowCar implements ICar {
}
Public class CarTest {
Public static void main (String args []) {
ICar obj = SimpleFactory. create (toptype );
}
}