Factory method pattern - Design Pattern

Keywords: xml Database encoding Java

The factory method pattern optimizes the simple factory pattern by introducing abstract factory class to make the system more open and close.

Let's use an example to simulate the factory method pattern

Create an abstract factory first

package FactoryMethodPattern;

public interface LoggerFactory  {
    public Logger createLogger();
}

Create a specific factory class

 

package FactoryMethodPattern;

public class DatabaseLoggerFactory  implements LoggerFactory {
    @Override
    public Logger createLogger() {
        Logger logger = new DatabaseLogger();
        return logger;
    }
}

 

package FactoryMethodPattern;

public class FileLoggerFactory implements LoggerFactory {
    @Override
    public Logger createLogger() {
        Logger logger = new FileLogger();
        return logger;
    }
}

Create abstract product class

package FactoryMethodPattern;

public interface Logger {
    public void write();
}

Specific products

package FactoryMethodPattern;

public class DatabaseLogger implements Logger {
    public void write(){
        System.out.println("Database logging...");
    }
}
package FactoryMethodPattern;

public class FileLogger implements Logger {
    @Override
    public void write() {
        System.out.println("Documentation log...");
    }
}

Testing

package FactoryMethodPattern;

import SimpleFactoryPattern.XMLUtil;

public class Window {
    public static void main(String[] args) throws Exception {
        LoggerFactory factory;
           Logger logger;
//           LoggerFactory factory =  (LoggerFactory) XMlUtil.getBean();
//           factory = new FileLoggerFactory();
//           logger = factory.createLogger();
//           logger.write();
        factory = new DatabaseLoggerFactory();
        logger = factory.createLogger();
        logger.write();
    }
}

Getting class objects through reflection and configuration files

<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <className>FactoryMethodPattern.FileLoggerFactory</className>
</config>

XML read

package FactoryMethodPattern;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class XMlUtil {
    public static Object getBean() throws Exception{
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(new File("src//FactoryMethodPattern//config.xml"));
        NodeList node = document.getElementsByTagName("className");
        Node node1=  node.item(0).getFirstChild();
        String className = node1.getNodeValue();
        Class c = Class.forName(className);
        Object o = c.newInstance();
        return o;
    }
}

Testing

package FactoryMethodPattern;

import SimpleFactoryPattern.XMLUtil;

public class Window {
    public static void main(String[] args) throws Exception {
//        LoggerFactory factory;
           Logger logger;
           LoggerFactory factory =  (LoggerFactory) XMlUtil.getBean();
           factory = new FileLoggerFactory();
           logger = factory.createLogger();
           logger.write();
//        factory = new DatabaseLoggerFactory();
//        logger = factory.createLogger();
//        logger.write();
    }
}

Summary of plant method mode:

Advantages:

1. In the factory method, users only need to pay attention to the factory corresponding to the product, not the details of creation, and do not need to know the class name of the product.

2. All concrete product factories have the same abstract parent class, which can implement multiple sets.

3. When adding new products, there is no need to modify the abstract factory, only need to add new specific factories and specific products, which conform to the opening and closing principle, and the system has good scalability.

Disadvantages:

1. When expanding, more classes need to be created to increase the complexity of the system.

Posted by PatelNehal on Tue, 03 Dec 2019 12:13:02 -0800