/**
Simple Factory Model: One factory produces three types of automotive high-grade cars (Topcar), medium-sized cars (midcar), low-grade cars (Lowcar)
The production vehicle requires a method that defines a ICAR interface class that defines the method name.
Three kinds of cars to implement these methods.
Define a factory class responsible for creating car objects
These are the prototypes of a simple factory model. That is, program developers do not have to create objects over and over again, and only call the factory model to create the appropriate objects.
Note the class that is defined with public should be a separate *.java file. No distinction is made here.
*/
public interface icar{
This class is defined as a method for creating a car, and 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");
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Simple Factory Model Example