Design Pattern Learning -- Factory Method
What
Factory Method: defines an interface for object creation, so that sub-classes can decide which class to instantiate. Factory Method delays the instantiation of a class to its subclass.
Why
Factory Method is a basic creation mode. It mainly depends on the subclass to decide which class to instantiate. It is mainly used in framework code or toolkit.
Applicable to the following scenarios:
1. When a class does not know the class of the object it must create
2. When a class wants to be specified by a subclass
3. When the class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies
How
In the following scenario, to write a syntax analyzer, you must support syntax analysis of C and java code, and complete code syntax check and parsing. Take this as an example to implement the factory model.
Syntax analyzer Interface
public interface Parsing { void checkGrammar(); void parseCode();}
C code syntax analyzer implementation
public class CParsing implements Parsing { @Override public void checkGrammar() { System.out.println(check c grammar); } @Override public void parseCode() { System.out.println(parse c code); }}
Java code syntax analyzer implementation
public class JavaParsing implements Parsing { @Override public void checkGrammar() { System.out.println(check java grammar); } @Override public void parseCode() { System.out.println(parse java code); }}
Parser factory Interface
public interface ParsingFactory { Parsing createParsing();}
C code parser factory implementation
public class CParsingFactory implements ParsingFactory { @Override public Parsing createParsing() { return new CParsing(); }}
Java code parser factory implementation
public class JavaParsingFactory implements ParsingFactory { @Override public Parsing createParsing() { return new JavaParsing(); }}
Client call
public class App { public static void main( String[] args ){ ParsingFactory factory=new CParsingFactory(); Parsing parsing=factory.createParsing(); parsing.checkGrammar(); parsing.parseCode(); ParsingFactory factory1=new JavaParsingFactory(); Parsing parsing1=factory1.createParsing(); parsing1.checkGrammar(); parsing1.parseCode(); }}
The preceding example class diagram is as follows:
Discuss
Abstarct Factory VS Factory Method
After learning Factory Method, we are very confused about the Factory Method mode and Abstarct Factory. After reading the two chapters, we have basically come up with a connection between the two. Abstarct Factory is a special Factory Method, but what is the difference between the two?
After completing the Factory Method example, we found that the UML sample in Abstract Factory and the UML sample in Factory Method have the same structure, so we thought about why?
The intention is as follows:
Factory Method: DefinitionAn interface for creating objects,Let the subclass decide which class to instantiate. Factory MethodInstantiation delay to its subclass.
Abstarct Factory: CreatesA series of related or interdependent InterfacesWithout specifying their specific classes.
The above bold section shows the difference.
1. abstarct Factory emphasizes the creation of a series of mutually dependent interfaces, that is, the Abstarct Factory can produce a series of objects, which is a one-to-many relationship. Then I thought about it again, the example of Abstarct Factory is complete (for details, see ChangeLog of Abstarct Factory, design pattern learning ). Its scalability focuses on horizontal scaling. For example, in the example of Abstarct Factory learning, we have another RoleDao. we add the createRoleDao method in DaoFactory.
2. The Factory Method emphasizes creating an object interface and letting the subclass decide which class to instantiate, that is, its Factory and product are in a one-to-one relationship. Its scalability focuses on vertical expansion. For example, in this example, we need to add a syntax analyzer that supports the SQL language. I need to add a SqlParsingFactory class and a SqlParsing class.