If the development of the work, the factory design pattern probably has been deeply rooted, the more common example is to implement the database Operation class in code, considering that there may be a database type transformation or migration, usually a database operation class abstracted out an interface, and then use the factory to get the actual database operation class instance.
Here's an example of the simplest factory model:
InterfaceIAnimal {voidTalk ();}classCatImplementsIAnimal {@Override Public voidTalk () {System.out.println ("I ' m cat"); }}classDogImplementsIAnimal {@Override Public voidTalk () {System.out.println ("I ' m Dog"); }}classAnimalfactory {Privateanimalfactory () {} Public Staticianimal getinstance (String animal) {if("Cat". Equals (animal)) { return NewCat (); } Else if("Dog". Equals (animal)) { return NewDog (); } Else { return NULL; } }} Public classMain { Public Static voidMain (string[] args)throwsparseexception{ianimal Animal= Animalfactory.getinstance ("Cat"); Animal.talk (); Animal= Animalfactory.getinstance ("Dog"); Animal.talk (); }}View Code
In the simplest case, the client first wanted to generate an instance of cat, passed into the "cat", and, after a while, wanted to generate a dog instance, passed directly to "dog" to the factory and continued to use it.
Observe the factory class, which is based on the client passed the string of a manual new instance and return, that if the factory just to accommodate hundreds of instances of the return, and may be added to the new instance type, not every time to modify the factory class, there is no way I write a factory class, The factory class can then automatically generate and return the instances required by the client? At this point, we can use the mechanism of reflection, according to the client to pass in the string, using the reflection principle to find the entity class we want. First of all, the interface and entity classes are to be written back to him, and then modify the factory class as follows:
class animalfactory { private animalfactory () {} publicstatic IAnimal getinstance (String animal) { try { = class.forname (animal); return (IAnimal) cls.newinstance (); Catch (Exception e) { returnnull; }} }
View Code
This class is first found by the static method of class type forname (String className) and returns the class type object of this type, which throws classnotfoundexception when it is not found, and returns null if it throws an exception.
The Newinstance () method of the class object is then used to produce an instance of an instance of the entity class found by the Forname function. Equivalent to generating an instance with the default constructor, so an exception is thrown here, such as when the default constructor is a private type.
Then, if the instances I produce need to be instantiated according to the parameters passed in, then we can use reflection to find the corresponding parameters of the constructor, to regenerate into an instance of the good.
InterfaceIAnimal {voidTalk ();}classCatImplementsIAnimal {PrivateString name; PublicCat (String name) { This. Name =name; } @Override Public voidTalk () {System.out.println ("I ' m Cat:" + This. Name); }}classDogImplementsIAnimal {PrivateString name; PublicDog (String name) { This. Name =name; } @Override Public voidTalk () {System.out.println ("I ' m Dog:" + This. Name); }}classAnimalfactory {Privateanimalfactory () {} Public Staticianimal getinstance (string animal, string name) {Try{Class CLS=class.forname (animal); Constructor<?> con = cls.getconstructor (String.class); return(ianimal) con.newinstance (name); } Catch(Exception e) {return NULL; } }} Public classMain { Public Static voidMain (string[] args)throwsparseexception{ianimal Animal= Animalfactory.getinstance ("Cat", "Miao"); Animal.talk (); Animal= Animalfactory.getinstance ("Dog", "Wang"); Animal.talk (); }}View Code
In the factory class, if we find the class type object, we use it to find the corresponding constructor object, the GetConstructor method takes an infinite number of arguments, and optionally specifies what type of constructor parameter we are looking for. In the example, this type is string, so we use String.class to specify that the type is a string type, and then use the constructor object, newinstance (string para) to instantiate and return.
This is done by using reflection to improve the traditional Factory mode step. If there is something wrong with writing, the Welcome Place treatise.
Respect intellectual property rights, reprint quoted please inform the author and indicate the source!
"Java reflection mechanism" improves simple Factory mode design with Reflection