Simple factory, simple factory Model
Simple factory mode explanation:
Simple Factory Pattern is an innovative class mode, also known as Static FactoryMethod Pattern. It is used to create instances of other classes by defining a specific class, the created instance usually has a common parent class.
The roles in the simple factory model and their responsibilities are as follows:
Creator: this is the core of the simple factory model. It is responsible for creating the internal logic of all classes. Of course, the factory class must be called by the outside world to create the required product objects.
Abstract Product role: the parent class of all objects created in simple factory mode. Note that the parent class can be an interface or an abstract class, it describes the common public interfaces of all instances.
Concrete Product role: a specific instance object created by a simple factory. These products often share a common parent class.
In-depth analysis of simple factory models:
The simple factory mode solves the problem of how to instantiate a suitable object.
The core idea of the simple factory model is: there is a dedicated class responsible For the instance creation process.
Specifically, products are a collection of classes, which are an object tree derived from an abstract class or interface. The factory class is used to generate a suitable object to meet the customer's requirements.
If the simple factory model involves no common logic between specific products, we can use interfaces to assume the role of abstract products. If there is a functional logic between specific products or, we must extract these common things and put them in an abstract class so that the specific product can inherit the abstract class. To achieve better reuse, common things should always be abstracted.
The factory mode provides interfaces for object creation. Factory models are classified into three types based on the concepts in Java and patterns:
1. Simple Factory)
2. Factory Method)
3. Abstract Factory)
These three models are gradually abstracted from top to bottom and more general. Another kind of classification is to look at the simple factory model as a special case of the factory method model. The two are classified as one type. Both are acceptable. This is a classification method that uses Java and patterns.
Under what circumstances should we remember to use the factory model? There are generally two points:
1. You cannot predict the type of instance to be created during encoding.
2. The system should not rely on the details of how product instances are created, combined, and expressed.
What benefits can the factory model bring to our OOD and OOP ??
Simple factory Mode
This mode is simple and easy to use when the business is relatively simple. It is generally used in scenarios where small projects or specific products are rarely scaled (so that the factory class does not need to be changed frequently ).
It consists of three roles:
Factory role: this is the core of this model. It contains some commercial logic and judgment logic to generate specific factory products based on different logic. For example, the Driver class.
Abstract Product role: it is generally the parent class or implemented interface inherited by a specific product. Implemented by interfaces or abstract classes. In this example, the Car interface.
Specific product role: the object created by the factory class is the instance of this role. It is implemented by a specific class in java, such as Benz and Bmw classes in the example.
// Abstract Product
Abstract class Car {private String name; public abstract void drive (); public String getName () {return name;} public void setName (String name) {this. name = name ;}// product class Benz extends Car {public void drive () {System. out. println (this. getName () + "---- go ---------------------") ;}} class Bmw extends Car {public void drive () {System. out. println (this. getName () + "---- go ---------------------") ;}// simple factory class Driver {public static Car createCar (String car) {Car c = null; if ("Benz ". equalsIgnoreCase (car) c = new Benz (); else if ("Bmw ". required signorecase (car) c = new Bmw (); return c ;}// the boss public class BossSimplyFactory {public static void main (String [] args) throws IOException {// The Boss told the Driver that I was sitting on a Mercedes-Benz Car = Driver today. createCar ("benz"); car. setName ("benz"); // The driver drove the Mercedes-benz departure car. drive () ;}< span style = "font-family: courier new, courier;" >}</span> // abstract class Car {private String name; public abstract void drive (); public String getName () {return name;} public void setName (String name) {this. name = name ;}// product class Benz extends Car {public void drive () {System. out. println (this. getName () + "---- go ---------------------") ;}} class Bmw extends Car {public void drive () {System. out. println (this. getName () + "---- go ---------------------") ;}// abstract Factory abstract class Driver {public abstract Car createCar (String car) throws Exception ;} // specific factory (each factory is responsible for a specific product) class BenzDriver extends Driver {public Car createCar (String car) throws Exception {return new Benz ();}} class BmwDriver extends Driver {public Car createCar (String car) throws Exception {return new Bmw ();}}
// Boss public class Boss {public static void main (String [] args) throws Exception {Driver d = new BenzDriver (); Car c = d. createCar ("benz"); c. setName ("benz"); c. drive ();}}