In object-oriented programming programming, our most common operation is new object, but in the process of creating a new object, there will be some problems, such as we need to pay attention to the implementation details of creating a new object, initialize some necessary parameters, etc. This will make us focus more on object creation rather than program logic implementation, which seriously delays our program development efficiency. The emergence of factory patterns and abstract factory patterns perfectly solves this problem, allowing us to no longer care about object creation, but focus more on the implementation of business.
Features:
1. Programmers create objects directly through factory methods and no longer pay attention to the details of creating objects.
2. Hiding the implementation details of the object is also conducive to the security of the program.
3. Reduce the program coupling degree.
Applications in enterprise-level development and common frameworks:
Sessionfactory, etc. in Hibernate
Factory model classification:
Simple factory model, the most commonly used form in program development, the specific code is as follows:
public class Demo { /** * The demo class is our usual operation class. In this class, we don't have to care about the implementation details of creating cars*/ public static void main(String[] args) { Car car = CarFactory.createCar("dz"); car.run(); Car car2 = CarFactory.createCar("at"); car2.run(); }}interface Car{ public void run();}class Dz implements Car{ public void run() { System.out.println("Volkswagen is running"); }}class At implements Car{ public void run() { System.out.println("Alto car is running"); }}class CarFactory{ public static Car createCar(String type){ if("dz".equals(type)){ System.out.println("created a Volkswagen car"); return new Dz(); } if("at".equals(type)){ System.out.println("created an Alto car"); return new At(); } return null; }}The factory method mode is easier to expand than the simple factory mode, and there is no need to modify the previous code.
public class Demo { /** * The demo class is our usual operation class. In this class, we don't have to care about the implementation details of creating cars*/ public static void main(String[] args) { AtFactory atFactory = new AtFactory(); DzFactory dzFactory = new DzFactory(); Car at = atFactory.createCar(); Car dz = dzFactory.createCar(); at.run(); dz.run(); }}interface Car { public void run();}class Dz implements Car { public void run() { System.out.println("Volkswagen is running"); }}class At implements Car { public void run() { System.out.println("Auto Car is running"); }}interface CarFactory { Car createCar();}class DzFactory implements CarFactory { public Car createCar() { return new Dz(); }}class AtFactory implements CarFactory { public Car createCar() { return new At(); }}Abstract factory method pattern:
public class Demo { public static void main(String[] args) { Car carFactory = new GDCarFactory(); FDZ fdz = carFactory.createFdz(); fdz.zhuansu(); }}interface FDZ { void zhuansu();}class GDFDZ implements FDZ { public void zhuansu() { System.out.println("High-end engine speed"); }}class DDFDZ implements FDZ { public void zhuansu() { System.out.println("Low-end engine speed slow"); }}interface ZY { void shushidu();}class GDZY implements ZY { public void shushidu() { System.out.println("High-end seats are comfortable"); }}class DDZY implements ZY { public void shushidu() { System.out.println("Low-end seats are uncomfortable"); }}interface LT { void mosundu();}class GDLT implements LT { public void mosundu() { System.out.println("High-end tires do not wear"); }}class DDLT implements LT { public void mosundu() { System.out.println("Low-end tires wear fast"); }}interface Car { FDZ createFdz(); ZY createZy(); LT createLt();}class GDCarFactory implements Car{ @Override public FDZ createFdz() { return new GDFDZ(); } @Override public ZY createZy() { return new GDZY(); } @Override public LT createLt() { return new GDLT(); } }class DDCarFactory implements Car{ @Override public FDZ createFdz() { return new DDFDZ(); } @Override public ZY createZy() { return new DDZY(); } @Override public LT createLt() { return new DDLT(); } }Comparison of three methods:
1. Simple factory mode: The simple factory mode is simple in design, with small code volume, but poor scalability. When it is necessary to expand, you need to modify the previous code.
2. Factory method mode: strong scalability, but increases code complexity
3. Abstract factory model: The abstract factory model and the factory model are different. The abstract factory model divides products into grades, but the factory model is to classify products. To give an example of a car: the factory model is to produce different types of cars, such as Audi and Volkswagen, while the abstract factory model divides the same car into grades. For example, both Volkswagen, we divide high-end cars and low-end cars. From a methodological perspective, the abstract factory pattern is more like the refinement of the factory pattern. One is aimed at different products, and the other is aimed at the same product family.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.