Simple factory mode example

Source: Internet
Author: User

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 );
}
}

 

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.