Java reflection (3)

Source: Internet
Author: User
Document directory
  • 1. The simplest Factory Design Model
  • 2. Reflection implementation
  • 3. added flexibility: Configuration File
11. Factory Design Model 1. Simplest Factory Design Model

Scenario: There is a fruit interface. Both the apple and orange classes implement the fruit interface, and the factory is used to produce fruits;

Import Java. io. *; import Java. util. *; interface fruit {public void eat ();} class Apple implements fruit {public void eat () {system. out. println ("eat apple") ;}} class orange implements fruit {public void eat () {system. out. println ("eat oranges") ;}} class factory {public static fruit getinstance (string name) throws exception {fruit F = NULL; If ("apple ". equals (name) {f = new Apple ();} else if ("orange ". equals (name) {f = new orange () ;} If (F! = NULL) {return F;} else return NULL;} public class factorydemo01 {public static void main (string ARGs []) throws exception {fruit F = factory. getinstance ("apple"); F. eat ();}}

2. Reflection implementation

One disadvantage of the above example is that the getinstance code in the factory will increase with the increase in the number of fruits. For example, if a banana class is added, you need to add

If (name. Equals ("banana ")){...}

This is inconvenient, so reflection is a good solution:

Import Java. io. *; import Java. util. *; interface fruit {public void eat ();} class Apple implements fruit {public void eat () {system. out. println ("eat apple") ;}} class orange implements fruit {public void eat () {system. out. println ("eat oranges") ;}} class factory {public static fruit getinstance (string name) throws exception {fruit F = nullfruit F = (fruit) class. forname (name ). newinstance (); If (F! = NULL) {return F;} else return NULL;} public class factorydemo01 {public static void main (string ARGs []) throws exception {fruit F = factory. getinstance ("apple"); F. eat ();}}

3. added flexibility: Configuration File

However, if the apple class has a package name, you must add the package name + class name to access this class. For example, at the top of the apple class: Package org; you must use org. Apple to access the apple class. Therefore, this problem can be solved through the properties file;

Import Java. io. *; import Java. util. *; interface fruit {public void eat ();} class Apple implements fruit {public void eat () {system. out. println ("eat apple") ;}} class orange implements fruit {public void eat () {system. out. println ("eat oranges") ;}} class factory {public static fruit getinstance (string name) throws exception {fruit F = nullfruit F = (fruit) class. forname (name ). newinstance (); If (F! = NULL) {return F;} else return NULL;} private Factory () {}} public class factorydemo01 {public static void main (string ARGs []) throws exception {properties P = new properties (); p. load (New fileinputstream ("1. properties "); string STR = P. getproperty ("apple"); fruit F = factory. getinstance (STR); F. eat ();}}

1. PropertiesCode:

apple=Appleorange=Orange

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.