Simple Factory mode:
Interface fruit{public abstract void Eat (),} class Apple implements fruit{public void Eat () { System.out.println ("Apple");} } Class Orange implements fruit{public void Eat () { System.out.println ("Orange");} }//Construction factory class// In other words, if we just need to modify the factory class when we add another instance, class factory{public static Fruit getinstance (String fruitname) { fruit f= null; if ("Apple". Equals (Fruitname)) { f=new apple (); } if ("Orange". Equals (Fruitname)) { f=new orange (); } return f; }} Class hello{public static void Main (string[] a) { fruit f=factory.getinstance ("Orange"); F.eat (); } }
However, we consider that when we add a subclass, we need to modify the factory class. If we want to add a lot of sub-classes, there will be a lot of changes.
So, we use the reflection mechanism of Java:
Package Reflect; Interface fruit{public abstract void Eat (),} class Apple implements fruit{public void Eat () {System.out.pri Ntln ("Apple"); }} class Orange implements fruit{public void Eat () {System.out.println ("orange"); }}
class factory{public static Fruit getinstance (String ClassName) {fruit f=null; try{f= (Fruit) class.forname (ClassName). newinstance (); Java Reflection mechanism}catch (Exception e) {e.printstacktrace (); } return F; }}
Class Hello{PublicStatic vOID Main (string[] a) {Fruit f=factory.getinstance ("reflect.apple"); if (f!=null) {f.eat (); } }}
now, consider one more question: Although the above code can get an instance of an interface through reflection, it needs to pass in the full package and class name.
And users cannot know how many subclasses an interface can use,
So we configure the required subclasses in the form of a property file.
First create a fruit.properties resource file with the following content:
Apple=reflect.appleorange=reflect.orange
interface fruit{public abstract void Eat (),} class Apple implements fruit{public void Eat () {System.ou T.println ("Apple"); }} class Orange implements fruit{public void Eat () {System.out.println ("orange"); }} //Operation Properties File Class
class init{public static Properties Getpro () throws FileNotFoundException, ioexception{properties Pro=new Properties (); File F=new file ("Fruit.properties"); if (f.exists ()) {Pro.load (new FileInputStream (f)); }else{pro.setproperty ("Apple", "reflect.apple"); Pro.setproperty ("Orange", "Reflect.orange"); Pro.store (new FileOutputStream (f), "FRUIT CLASS"); } return pro; }}
//Factory method is still the same class factory{public static Fruit getinstance (String ClassName) {fruit f=null; try{f= (Fruit) class.forname (ClassName). newinstance (); }catch (Exception e) {e.printstacktrace (); } return F; }}class hello{public static void Main (string[] a) throws FileNotFoundException, ioexception{Properties Pro=ini T.getpro (); Fruit f=factory.getinstance (Pro.getproperty ("Apple")); Main change if (f!=null) {f.eat (); } }}
My summary: The factory method can also be designed using the reflection mechanism, and can also be designed by configuring the properties file, nice!!
Turn!! Factory Method--reflection mechanism