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