Java schema--A simple use case for Factory mode

Source: Internet
Author: User
Tags add interface return string

The Factory mode is a simpler one in the core model of Java EE, I heard that the Jive Forum has a wide range of application of the logarithmic design pattern, the factory model also includes, I started from this mode, I hope one day to be able to use a variety of design patterns freely.

Now give a simple use case, simulate a ham (Ham) production plant, the factory can produce several types of Ham, then after creating a factory instance, just tell it what kind of Ham you produce, it will be produced for you:
Package test;

Interface Ham {//define Interface for auxiliary implementation of Factory mode
void Show ()//The various ham produced by the Ham factory will have the ability of showing ()
}
public class Fatorymodule {//factory class, for products of various kinds of ham
Public Ham Getham (String hamtype) throws exception{//factory class object production Ham action
if (Hamtype.equals ("HamA")) {
return new HamA ();
}
else if (hamtype.equals ("Hamb")) {
return new Hamb ();
}
else if (hamtype.equals ("HamC")) {
return new HamC ();
}
Else
throw new Exception ()//If the factory receives instructions to produce some kind of ham but does not support it, throw an exception

}

public static void Main (string[] args) {//test code
Fatorymodule fatorymodule = new Fatorymodule ();
try {
Ham Myham = Fatorymodule.getham ("Hamb");//You can easily create various types of Ham with minimal changes to the program structure and code
Myham.show ();
catch (Exception ex) {
Ex.printstacktrace ()///The exception should be handled further
}
}
}

Class HamA implements Ham {//a product produced in the factory HamA
public void Show () {
System.out.println ("You Got a HamA.");
}
}

Class Hamb implements Ham {//Another product produced by the factory Hamb
public void Show () {
System.out.println ("You Got a Hamb.");
}
}

Class HamC implements Ham {//Factory production of the third product HamC
public void Show () {
System.out.println ("You Got a HamC.");
}
}
Through the demo of the program I got the following conclusion:

Since the factory model was applied, I only had to tell the factory object what kind of ham (i.e. the parameters in the Getham () method) could be used to get the ham, without having to write Hamx hamx=new hamx (); If there are dozens of different ham objects that need to be created in different parts of the program, then the use of factory-mode code will be simple and uniform, and each place that requires a different ham is only a different parameter for Getham (). Otherwise, once the program needs to be extended and maintained, the new statement at dozens of will cause headaches. And if you need to add a new ham type, just add it to the factory class.
I also understand that if the creation of each Ham object is a production line, then we use a factory to encapsulate a number of production lines, so that the user is like only dealing with people in the factory front, tell him to ask your factory to produce a product, Instead of dealing with every line of production-it's certainly good.
Please comment:



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.