The source code is JDK1.8 as reference
1. Definition:
Defines an interface for creating objects, letting subclasses decide which class to instantiate. The factory method defers the instantiation of a class to a subclass.
2. Analysis:
May at first glance the definition of factory design pattern a bit dizzy, but think about the literal meaning of factory, in fact, is also very good understanding, the so-called factory, is according to the specified demand to produce some products, factory design mode is no exception, is in accordance with the needs of factory to provide a description, The factory then produces the specified product as required and is used by the calling application scenario.
General class Diagram:
class Diagram parsing:
2.1.Product (Abstract product Class)
Define common characteristics of the product and make general constraints on the product implementation class.
2.2.ConcreteProduct (Product realization Class)
Product specific definition, can have a number of product classes exist, all need to implement product abstract products class.
2.3.Creator (Abstract create Class)
The abstract factory, which defines the upper bound for the implementation of the factory.
2.4.ConcreteCreator (implement Factory)
Complete the specific creation of the product class.
Factory design pattern implementation is to focus on some of the highly abstract class of the instantiation process, of course, the instantiation process also includes the initialization of the internal data of the instance, even if this is not factory or not very understanding, then now look at factory General class diagram of the concrete implementation.
3. Specific application:
3.1.Product (Abstract product Class)
publicabstractclass Product { // .. 产品类的公共方法 publicvoidmethod1(){ // .. 业务逻辑处理 } // .. 抽象方法 publicabstractvoidmethod2();}
3.2.ConcreteProduct (Product realization Class)
publicclass ConcreteProduct1 extends Product { publicvoidmethod2() { // .. 业务逻辑处理 }}publicclass ConcreteProduct2 extends Product { publicvoidmethod2() { // .. 业务逻辑处理 }}
3.3.Creator (Abstract Create Class)
publicabstractclass Creator { /* * 创建一个产品对象,其输入参数类型可以自行设置 * 通常为String、Enum、Class等,当然也可以为空 */ publicabstractcreateProduct(Class<T> c);}
3.4.ConcreteCreator (Implement Factory)
public class concretecreator extends creator { public <t extends product> T createproduct (class<t> c) {Product Product=null ; try {Product = (product) class.forname (C.getname ()). Newinstance (); } catch (Exception e) {//: Exception Handling } return (T) Product; }}
This is not very clear, when you need to ConcreteProduct1 or ConcreteProduct2 in which class instance, only need to tell the concretecreator to produce which class instance is possible, Concretecreator can produce instances of this class according to specific requirements. The factory approach is to centralize the instantiation of the same class of classes with a common abstraction, whereby the factory is specifically instantiated. This facilitates the management of class instantiation, while shielding the implementation details of specific classes.
Note: I am referring to the "design mode of Zen" and "design mode" two books learned, which added their own understanding of iterator design patterns, as well as the JDK in the source code understanding. The next article will continue to talk about factory's extended mode.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java design mode (v) Factory mode