I've been known for the first time today to touch the Java Factory design model.
See below:
Java code
- Interface Fruit
- {
- public void Eat ();
- }
- Class Apple implements Fruit
- {
- public void Eat ()
- {
- System.out.println ("eat apples");
- }
- }
- Class Orange implements Fruit
- {
- public void Eat ()
- {
- System.out.println ("eat oranges");
- }
- }
- Class Factory
- {
- public static Fruit getins (String classname) {
- Fruit F;
- f = null;
- if ("Apple". Equals (classname))
- {
- f = new Apple ();
- }
- if ("Orange". Equals (classname))
- {
- f = new Orange ();
- }
- return F;
- }
- }
- Public class Demo6
- {
- public static void Main (string[] args)
- {
- Fruit F;
- if (args[0]!=null) {
- F=factory.getins (args[0]);
- F.eat ();
- }
- else {
- System.out.println ("Please input parameters");
- }
- }
- }
The program joins a transition segment between the subclass and the interface, which is called the factory by the transition segment to get an instance of the interface.
So is it possible to understand that the factory is processing the things provided by the main method into standards-compliant products (instanced objects) for subsequent programs to process?
java--Object-oriented design mode--Factory mode