The simple factory model is one of the factory models. There are three factory models: the simple factory model, the factory method model, and the abstract factory model, that is, all object creation tasks are handed over to the factory. From the simple factory mode to the abstract factory mode, the abstract level gradually increases.
The factory model consists of three parts: the factory role, the abstract Product role and the specific product role.
The factory role is the core of the factory model. It determines which product role to create. Because each product role implements the same interface, the question of which role to create can be solved in the factory, you do not need to solve the problem on the client.
The following is a detailed example: the example that is commonly used on the Internet: rich people, drivers, and three cars (Mercedes-Benz, BMW, and Audi ). Before using the design model, the rich man got on the Mercedes-Benz and said, "drive the Mercedes-Benz", the car will only drive (I think this is a strange example, so I can understand it for the time being ), then let's take a look at what will happen with the simple factory model.
First, determine the role:
1. Abstract Product role (a product factory must be available before production)
1 public interface Car {2 public void drive();3 }
2. Specific product roles
1 public class Audi implements Car {2 3 @Override4 public void drive() {5 System.out.println("Drive Audi");6 }7 8 }
1 public class Benz implements Car {2 3 @Override4 public void drive() {5 System.out.println("Drive Benz");6 }7 8 }
1 public class BMW implements Car {2 3 @Override4 public void drive() {5 System.out.println("Drive BMW");6 }7 8 }
3. Factory role
1 public class driver {2 public static car drivecar (string car) {3 if (car. equalsignorecase ("Benz") 4 return New Benz (); 5 If (car. equalsignorecase ("BMW") 6 RETURN new BMW (); 7 if (car. equalsignorecase ("Audi") 8 return New Audi (); 9 else {10 system. out. println ("this car is not available in the garage"); 11 return NULL; 12} 13} 14}
Then write a client to test it.
1 public class TestDriver {2 @Test3 public void Magnate() throws Exception{4 Car car = Driver.driveCar("Benz");5 car.drive();6 }7 }
There is a small bug in the code here, that is, inputting a nonexistent car name on the client will cause an exception, but this does not prevent us from understanding the simple factory mode, so we don't need to worry about it.
As you can see, the factory role is the core of the entire code. The advantage of the simple factory mode is that you do not need to expose the specific class to the client directly. In addition, the logic judgment in the code is in the factory role, so the client does not need to be responsible for too many functions. The disadvantage is that it violates the open and closed principles. We can see that if a rich man adds a car and needs to modify the specific product role and factory role, such a small example may seem insignificant, but if it is in a very large project, it is very likely to take a long time.