Factory mode
Factory pattern: one of the most common design patterns in Java. This type of design pattern is a creation pattern, which provides the best way to create objects.
In factory mode, we do not expose the creation logic to the client when we create the object, and we point to the newly created object by using a common interface.
Meaning: define an interface to create an object, let its subclass decide which factory class to instantiate, and factory mode delays its creation process to subclass.
advantage:
1. If a caller wants to create an object, just know its name.
2. High extensibility. If you want to add a product, just extend a factory class.
3. To shield the specific implementation of the product, the caller only cares about the interface of the product
Disadvantages: each time you add a product, you need to add a specific class and object implementation factory, which will multiply the number of classes in the system, increase the complexity of the system to a certain extent, and increase the dependency of the specific class of the system.
Usage scenario:
1. Loggers: users can choose where to log, such as local hard disk, system events, remote server, etc.
2. Database access, when the user does not know which type of database the final system uses, and when the database may change. 3. To design a framework for connecting servers, three protocols are needed, "POP3", "IMAP" and "HTTP". These three protocols can be used as product classes to jointly implement an interface. Access via factory mode
Code presentation:
1. Create an interface
public interface Moveable{ void go(); }
2. Create entity classes that implement interfaces
public class Car implements Moveable{ @Override public void go() { System.out.println("Car with di di di di..."); } }
public class Train implements Moveable{ @Override public void go() { System.out.println("Train with wu wu wu wu..."); } }
public class Plane implements Moveable{ @Override public void go() { System.out.println("Plane with biu biu biu biu..."); } }
3. Create factory
Method 1:
public class MoveableFactory { public Car getCar() { return new Car(); } public Train getTrain() { return new Train(); } public Plane getPlane() { return new Plane(); } }
Method 2:
public class MoveableFactory { public Moveable getMoveable(String moveType){ if(moveType == null){ return null; } if(moveType.equalsIgnoreCase("car")){ return new Car(); } else if(moveType.equalsIgnoreCase("train")){ return new Train(); } else if(moveType.equalsIgnoreCase("plane")){ return new Plane(); } return null; } }
Test verification:
public class MoveableFactoryTest { public static void main(String[] args) { // Mode 1 MoveableFactory moveableFactory = new MoveableFactory (); Car car = moveableFactory.getCar(); car.go(); Train train = moveableFactory.getTrain(); train.go(); Plane plane = moveableFactory.getPlane(); plane.go(); // Mode 2 MoveableFactory moveableFactory = new MoveableFactory (); Moveable car = moveableFactory.getShape("car"); car.go(); Moveable train = moveableFactory.getShape("train"); train.go(); Moveable plane = moveableFactory.getShape("plane"); plane.go(); } }
Extension: we can write our own logic code before the factory obtains specific cases, such as permission verification, authentication login, etc;