Simple Factory Model Example

Source: Internet
Author: User

/**
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.