The factory mode is highly efficient in project development, which means to define an interface for creating objects and let the subclass decide which class to instantiate. This is a factory
Next, let's take a simple example:
As shown in, we first define our product abstract class interface, which can also be an abstract class:
Package factory; public abstract class PlatForm {public void search () {System. out. println ("your search result is ...... ");} Public abstract void music ();}
Below are some implementation classes
package factory;public class BaiduProduct extends PlatForm{@Overridepublic void music() {System.out.println("baidu music");}}
package factory;public class Qihu360Product extends PlatForm{@Overridepublic void music() {System.out.println("360 music");}}
package factory;public class SogouProduct extends PlatForm{@Overridepublic void music() {System.out.println("sogou music");}}
The three implementation classes have been completed. Next we will use a factory abstract class.
Package factory; public abstract class Creator {/*** creates a product object, where T can be a String Enum Class parameter, you can set * @ param c * @ return */public abstract <T extends PlatForm> T createProduct (Class <T> c );}
The following are implementation classes
Package factory; public class ConcrentCreator extends Creator {@ Overridepublic <T extends PlatForm> T createProduct (Class <T> c) {PlatForm plat = null; try {/*** creates an interface for creating an object, allowing the subclass to determine which object to instantiate. Factory method to delay the instantiation of a Class to subclass */plat = (PlatForm) Class. forName (c. getName ()). newInstance ();} catch (InstantiationException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (ClassNotFoundException e) {e. printStackTrace () ;}return (T) plat ;}}
Package factory; public class Client {public static void main (String [] args) {Creator creator = new ConcrentCreator (); // here, you can pass in the class you want to instantiate and then call the method you want. PlatForm plat = creator. createProduct (BaiduProduct. class); plat. search (); plat. music ();}}
You can output the desired result by imitating a client.
Abstract product classes in factory mode methods define product commonalities to achieve the most abstract definition of physical objects. creator creates classes for product promotions, that is to say, the abstract factory has a specific implementation creator factory class for how to create a product class. There are many variants of the factory class. below, I will summarize a common source code of the class.
1: Abstract Product
Package factory. total; public abstract class Product {public void method1 () {// all Product commonalities} // The abstract method has its own class to implement public abstract void method2 ();}
2: product implementation
package factory.total;public class ConcretProduct1 extends Product{@Overridepublic void method2() {//TODO}}package factory.total;public class ConcretProduct2 extends Product{@Overridepublic void method2() {//TODO}}
3: Abstract Factory
package factory.total;public abstract class Creator {public abstract <T extends Product> T createMethod(Class<T> c);}
4: specific factory types:
package factory.total;public class ConcreteCreator extends Creator{@Overridepublic <T extends Product> T createMethod(Class<T> c) {Product pro = null;try {pro = (Product) Class.forName(c.getName()).newInstance();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}return (T) pro;}}
Scenario Simulation
package factory.total;public class Client {public static void main(String[] args) {Creator creator = new ConcreteCreator();Product pro = creator.createMethod(ConcretProduct1.class);pro.method1();pro.method2();}}
The factory model is divided into the simple factory model and the multi-level factory model. However, the two cases are similar to the previous one. The simple factory model simply removes the abstract factory class, then, change the factory implementation class method to the static method. Multi-level factory mode refers to: for example, when we see a complicated project, all product classes are put in a factory class for initialization, and the structure is unclear, now we have a factory class for each product. Below I will show the application of multi-level factory classes for instances.
I have some basic classes or used them before, but added a factory class for each product.
1: abstract factory class:
package factory.more;public abstract class AbstractPlatFactory {public abstract PlatForm createPlat();}
2: specific factory type:
package factory.more;public class BaiduFactory extends AbstractPlatFactory{@Overridepublic PlatForm createPlat() {// TODO Auto-generated method stubreturn new BaiduProduct();}}
package factory.more;public class Qihu360Factory extends AbstractPlatFactory{@Overridepublic PlatForm createPlat() {return new Qihu360Product();}}
package factory.more;public class SogouFactory extends AbstractPlatFactory{@Overridepublic PlatForm createPlat() {return new SogouProduct();}}
Scenario:
package factory.more;public class Client {public static void main(String[] args) { PlatForm plat = new BaiduFactory().createPlat(); plat.search(); plat.music();}}
This is the use of multi-level factory,
In fact, there are also applications of abstract factory classes, which provide a set of interfaces for related objects without specifying their specific classes. (Today is too late. I will talk about it later)