Turn!! Factory Method--reflection mechanism

Source: Internet
Author: User

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

Related Article

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.