Well, just started to learn Java, to learn something a little bit of record, easy to consolidate.
The factory model of the Baidu definition is this:
Factory mode definition: Instantiate an object and replace the new operation with a factory method.
Easy to understand, that is, when new objects are used, a method that belongs to a factory class.
Paste the following code:
Interface interface Animal {public void bark (); } Subclass-Puppy Class Dog implements Animal { void bark () { System.out.println ("wow!");} } Subclass-Kitten class Cat implements Animal { void bark () { System.out.println ("meow!");} }
The normal method instantiation produces a new kitten puppy object:
Normal method Instantiation class Test {public static void Main (string[] args) { dog dog = new Dog (); Cat cat = new Cat (); Dog.bark (); Cat.bark (); } }
Factory mode:
Animal Factory class Animalfactory {public Animal creator (String animalname) { if (animalname.equals ("Dog")) { Animal dog = new Dog (); return dog; } else if (Animalname.equals ("Cat")) { Animal cat = new Cat (); return cat;}}} Use Factory mode to create a new object class Test {public static void Main (string[] args) { Animalfactory factory = new Animalfactory ( ); Animal dog = Factory.creator ("dog"); Animal cat = Factory.creator ("cat"); Dog.bark (); Cat.bark (); }}
Let's explain why it's here.
Animal dog = Factory.creator ("dog");
Instead of
Dog dog = Factory.creator ("dog");
First, the animal factory is a factory, the return type of the Creator method is animal, the animal class is an interface, we know that the interface can not be instantiated, here involves the problem of upward transformation.
Add a little bit of the upward transformation of the knowledge, such as the parent class animal has a bark () method, assuming that the subclass of dog inherits the parent class and defines a new method eat (), then animal dog = new Dog (), dog.eat () compile time will be error, because a reference can call which member functions and variables depend on the type of the reference, and animal has only the bark () method, so the compilation error.
Assuming that the subclass cat inherits the parent class and overrides the Bark () method, this time animal cat = new Cat (), call Cat.bark (), what is the result of the output?
is the bark () method that is overridden by the cat class because a reference to which method is called depends on the object that the reference is pointing to, because cat is pointing to the cat () object, although its type is animal, but the calling method is the bark () method inside the cat.
OK, back to the factory model, look at the above code carefully, then someone will ask, why use Factory mode, in the above code, it seems that the Factory mode is more complex, and no direct instantiation so clear.
Small programs are difficult to see the advantages of the factory model. For example, there is a new demand, in a class based on the generation of a subclass, then the previous use of the parent class to the sub-class, if the small program may have dozens of places to change, if the larger may be hundreds of thousands of places to change. If you use Factory mode, you can just modify the factory-generated objects.
But the factory model is not absolutely good, in the online search, there are predecessors said that the factory model is mainly for the polymorphism, is a class will derive a lot of things, this situation will use the Factory mode. Some of the other cases are a liability, depending on the situation.
Above is my personal understanding and opinion, if there are mistakes please correct me! Learn together, progress together!
Basic Factory mode in Java