Simple Factory mode Explanation:
Simple Factory pattern is the innovative model of the class, also called the Static Factory method pattern (FactoryMethod), which is responsible for creating instances of other classes by specifically defining a class. The instances that are created typically have a common parent class.
UML diagram for simple Factory mode:
The roles contained in the Simple factory model and their corresponding responsibilities are as follows:
Factory Role (Creator): This is the core of the simple factory pattern, which is responsible for creating the internal logic of all classes. Of course the factory class must be able to be called outside to create the desired product object.
Abstract (product) product role: The parent class of all objects created by the simple factory pattern, note that the parent class here can be an interface or an abstract class, which is responsible for describing common interfaces common to all instances.
Specific products (concrete product) roles: The specific instance objects created by a simple factory, which often have a common parent class.
In- depth analysis of the Simple factory model :
The Simple factory model solves the problem of how to instantiate an appropriate object.
The core idea of the simple factory model is that there is a specialized class that is responsible for creating the instance process.
Specifically, the product is looked at as a collection of classes that are derived from an abstract class or interface as an object tree. And the factory class is used to produce a suitable object to meet the customer's requirements.
If there is no common logic between the specific products involved in the simple factory model, then we can use the interface to play the role of abstract products, if the specific products have functional logic or, we must extract these common things, put in an abstract class, and then let the specific product inherit abstract class. For the purpose of better reuse, common things should always be abstracted.
In the actual use, the idle product and the specific product is often a multi-layered product structure, as shown in:
Simple Factory mode usage scenario analysis and code implementation:
Gg asked his girlfriend and many beautiful women to eat, but GG himself will not cook or do the meal is not good, this means that GG does not have to create a variety of food objects, each beauty has their own hobbies, to McDonald's after they like to eat what directly to the point on the line, McDonald's is the production of various food factory, At this time GG do not have to do their own hands, can also invite so many beautiful girls to eat, what to do is to pay O (∩_∩) o haha ~, its UML diagram is as follows:
The implementation code is as follows:
New interface for creating a food:
Package com.diermeng.designPattern.SimpleFactory; /* * Abstract interface of the product */ Public Interface Food { /* * Get the appropriate food */ Public void get (); } |
Next, build a specific product: chicken and Fries
package com.diermeng.designpattern.simplefactory.impl; import com.diermeng.designpattern.simplefactory.food;   /* * the implementation of the abstract product interface */ Public class mcchicken implements food{ /* * get a copy of the chicken. */      public void get () { out . println ("I Want a sweet chicken"); } |
Package Com.diermeng.designPattern.SimpleFactory.impl; import Com.diermeng.designPattern.SimpleFactory.Food; /* * French fries to the abstract product interface implementation */ Public class Chips implements food{ /* * Get a copy of fries */ Public void get () { System. out. println ("I want a piece of french fries"); } } |
Now set up a food processing plant:
Package Com.diermeng.designPattern.SimpleFactory.impl; import Com.diermeng.designPattern.SimpleFactory.Food; Public class foodfactory { Public Static Food Getfood (String type) throws instantiationexception, Illegalaccessexception, classnotfoundexception { if (Type.equalsignorecase ("McChicken")) { return McChicken. class. newinstance (); } else if(Type.equalsignorecase ("chips")) { return Chips. class. newinstance (); } Else { System. out. println ("Oops! The corresponding instantiation class could not be found! "); return null; } } } |
Finally we set up the test client:
Package com.diermeng.designPattern.SimpleFactory.client; import Com.diermeng.designPattern.SimpleFactory.Food; import com.diermeng.designPattern.SimpleFactory.impl.FoodFactory; /* * Test Client */ Public class simplefactorytest { Public static void main (string[] args) throws instantiationexception, Illegalaccessexception, classnotfoundexception { Instantiate a variety of foods Food McChicken = foodfactory. Getfood ("McChicken"); Food chips = foodfactory. Getfood ("Chips"); Food eggs = foodfactory. Getfood ("Eggs"); Get food if (mcchicken!=null) { Mcchicken.get (); } if (chips!=null) { Chips.get (); } if (eggs!=null) { Eggs.get (); } } } |
The results of the output are as follows:
Oh! The corresponding instantiation class could not be found! I'd like a sweet chicken. I'd like to have a piece of french fries |
Advantages and disadvantages of the simple factory model:
Pros: Factory class is the key to the whole model. It contains the necessary judgment logic to determine which specific class of objects should be created based on the information given by the outside world. Users can create the required instances directly from the factory class, without having to know how they are created and how they are organized. facilitates the optimization of the entire software architecture.
Disadvantage: Because the factory class centralizes all instances of the creation logic, this directly leads to a problem in the factory, all the clients will be implicated, and because the simple factory model of the product room based on a common abstract class or interface, so, but the product type increases, When there are different product interfaces or abstract classes, the factory class needs to decide when to create the kind of product, which is confused with the products that create the kind of product, violating a single duty, resulting in loss of flexibility and maintainability of the system. And more importantly, the simple factory model violates the "open closure principle", which violates the "system open to expansion, to modify the" principle, because when I add a new product must modify the factory class, the corresponding factory class will need to recompile again.
To summarize: The simple factory model separates the product creator and the consumer, is advantageous to the software system structure optimization; But since all logic is concentrated in a factory class, it leads to no high cohesion, and also violates the "open closure principle". In addition, simple Factory mode methods are generally static, while static factory methods cannot inherit from subclasses, so simple Factory mode cannot form an inheritance tree structure based on a base class.
Introduction to the practical application of the Simple factory model:
As one of the most basic and simplest design patterns, the simple factory model has a very wide range of applications, as illustrated in the example of the JDBC operations database in Java.
JDBC is a set of database programming interface APIs provided by Sun, which leverages the Java language to provide a simple and consistent way to access a variety of relational databases. The Java program uses JDBC to execute SQL statements, process the acquired data, and store the changed data back into the database, so JDBC is a mechanism for a Java application to talk to various relational data. When using JDBC for database access, use the driver interface provided by the database vendor to interact with the database management system.
When the client wants to use the data, it only needs to interact with the factory, which causes the operation steps to be greatly simplified, the steps are as follows: registering and loading the database driver, generally using class.forname (); Creating a link to the database connection object ; Create SQL Statement Object PreparedStatement (SQL), commit SQL statement, use ExecuteQuery () or executeupdate () according to actual situation, display corresponding result; Close database
Simple Factory mode