【Design Mode】 ---- Introduction to Factory Method Mode

1. Concept

1. Factory method pattern, "upgrade" on the basis of static factory method, the most important difference of factory method pattern is the introduction of abstract factory role, abstract factory can be interface, abstract class or concrete class.
2. The factory method is declared in the abstract factory, but the factory method is not implemented. The creation of the specific product object is the responsibility of its subclass. The client program for the abstract factory can specify the specific factory class at runtime. The specific factory class is implemented. Factory method, different specific factories can create different specific products.
3. In actual use, in addition to creating specific product objects, concrete factory classes can also be responsible for the initialization of product objects and some resource and environment configuration work, such as connecting to databases and creating files. In the client code, you only need to care about the factory class, different specific factories can create different products.
4. In general, the factory class is like an encapsulation of the product, showing an interface to the outside, and then when implementing a specific product, inherit the interface and implement the method. The factory class is also the same, define an abstract factory class, and then in the process of concrete implementation, the concrete factory inherits the methods of the abstract factory class.
Externally, it is just an interface that calls abstract classes.

2.UML mat

Insert picture description here

3. Code display

package p1;
//客户端
public class Client  { 
public static void main(String args[]) {
        LogFactory factory;
        Log logger1;
        factory = new FileLogFactory();
        logger1 = factory.CreateLog();
        logger1.WriteLog();
        Log logger2;
        factory = new SqlSeverDatabaseLogFactory();
        logger2 = factory.CreateLog();
        logger2.WriteLog();
        Log logger3;
        factory = new OracleDatabaseLogFactory();
        logger3 = factory.CreateLog();
        logger3.WriteLog();
    }
}
 //抽象工厂角色--对外接口
interface  LogFactory{                             
    public Log CreateLog();
}
//具体工厂角色--文件
class FileLogFactory implements LogFactory { 
    public Log CreateLog(){
        Log log=new FileLog();
        return log;
    }
}
//具体工厂角色--Sql数据库
class SqlSeverDatabaseLogFactory implements LogFactory{ 
    public Log CreateLog(){
        Log log=new SqlSeverDatabaseLog();
        return log;
    }
}
//具体工厂角色--Oracle数据库
class OracleDatabaseLogFactory implements LogFactory{   
    public Log CreateLog(){
        Log log=new OracleDatabaseLog();
        return log;
    }
}
//抽象产品角色--对外接口
interface Log{  				 
    public void WriteLog();
}
//文件记录:具体产品角色
class FileLog implements Log{  
    public void WriteLog(){
        System.out.println("文件日志记录成功。");
    }
}
//Sql数据库记录:具体产品角色
class SqlSeverDatabaseLog implements Log{    public void WriteLog(){
        System.out.println("Sql数据库记录成功");
    }
}
//Oracle数据库记录:具体产品角色
class OracleDatabaseLog implements Log{ 
    public void WriteLog(){
        System.out.println("Oracle数据库记录成功");
    }
}

4. Results display

Insert picture description here

5. Analysis and summary

1. Advantages
(1). The factory method defines an abstract factory class, and the specific factory is only responsible for creating the corresponding products, which conforms to the principle of single responsibility.
(2). When adding a new product, you only need to inherit the abstract factory class, without modifying the original code, in line with the principle of opening and closing.
2. Disadvantages
(1). When adding new products, in addition to adding new product categories, specific factory categories corresponding to them must also be provided. The number of system categories will increase in pairs, increasing the complexity of the system to a certain extent; At the same time, there are more classes that need to be compiled and run, which will bring some extra overhead to
the system ; (2). Increase the abstraction and difficulty of understanding of the system.
3. Applicable scenarios

(1). A class does not know the class of the object it needs.
(2). A class specifies which object is created by its subclass.
(3). If the customer only knows the parameters passed into the factory class, and does not care about the logic of how to create the object.

Published 84 original articles · won praise 6 · views 4725

Guess you like

Origin blog.csdn.net/qq_41152046/article/details/105478504