summary
Design pattern is an art. If you really understand this art, you will find that the world will become more beautiful.
definition
Define an interface for creating an object, let its subclass decide which class to instantiate
Use scenarios
Anywhere you use complex objects, you can use factory mode
UML
1. Abstract factory
Abstract factory: We all know that factories generally have only one function, that is, production, for example, Geely Automobile Factory, that is, Geely Automobile, iPhone mobile phone manufacturer, etc. So it can be summarized in a simple way, that is, create();
2. Specific automobile manufacturing plants
Specific automobile manufacturing plant: realizes an abstract factory, with actual processes and methods for manufacturing cars, etc.
3. Abstract car
Abstract cars: We generally know that cars can run, play music, navigate, and turn are common features of cars.
4. Specific cars
Abstract car: Concrete cars realize abstract car and have all its functions. Of course, different cars may have different functions.
The above brief introduction is basically that specific automobile factories manufacture corresponding cars, such as Audi factories, Volkswagen factories, Volkswagen cars, etc.
Code Demo
We have clearly understood the relationship between them above, so we will use code to demonstrate our ideas and logic above in code:
(1) Abstract car factory
package com.zengtao.demo.factory;/** * Abstract factory*/public abstract class CarFactory { public abstract <T extends Car> T createCar(Class<T> cla);}(2) Specific automobile factory
package com.zengtao.demo.factory;/** * Audi factory*/public class AudiFactory extends CarFactory { @SuppressWarnings("unchecked") @Override public <T extends Car> T createCar(Class<T> cla) { Car car = null; try { car = (Car) Class.forName(cla.getName()).newInstance(); } catch (Exception e) { e.printStackTrace(); } return (T) car; }}(3) Defining abstract car
package com.zengtao.demo.factory;public abstract class Car { public abstract void drive(); public abstract void selfNagive(); public abstract void playMusic();}(4) Specific car
(Audi q3)
package com.zengtao.demo.factory;public class AudiQ3Car extends AudiCar { @Override public void drive() { System.out.println("AudiQ3Car has been successfully manufactured"); System.out.println("AudiQ3Car drive"); } @Override public void selfNagive() { System.out.println("AudiQ3Car selfNagive"); } @Override public void playMusic() { System.out.println("AudiQ3Car selfNagive"); } @Override public void playMusic() { System.out.println("AudiQ3Car playMusic"); System.out.println(""); }}(Audi q5)
package com.zengtao.demo.factory;public class AudiQ5Car extends AudiCar { @Override public void drive() { System.out.println("AudiQ5Car has been successfully manufactured"); System.out.println("AudiQ5Car drive"); } @Override public void selfNagive() { System.out.println("AudiQ5Car selfNagive"); } @Override public void playMusic() { System.out.println("AudiQ5Car selfNagive"); } @Override public void playMusic() { System.out.println("AudiQ5Car playMusic"); System.out.println(""); }}(5) Call
package com.zengtao.demo;import com.zengtao.demo.factory.AudiFactory;import com.zengtao.demo.factory.AudiQ3Car;import com.zengtao.demo.factory.AudiQ5Car;public class Main { public static void main(String[] str) { // Create a factory AudiFactory audioFactory = new AudiFactory(); // Use the factory to make q3 car AudiQ3Car audiQ3Car = audioFactory.createCar(AudiQ3Car.class); // Use the factory to make q5 car AudiQ5Car audiQ5Car = audiFactory.createCar(AudiQ5Car.class); // Start driving and play music audiQ3Car.drive(); audiQ3Car.playMusic(); audiQ5Car.drive(); audiQ5Car.playMusic(); }}(6) Results
As for the above, the factory method is used to realize the simple process of manufacturing a car.
First point:
In the abstract factory, we see that the createCar(Class cla) method, why do we define it like this? Use reflection to create a specific car. In this way, our different types of cars only need corresponding conditions, can they be produced? So it is beneficial to write this way, just like many, Audi has q3, q5, a4, a6, a7, a8 and other series.
Second point:
If we have other cars, such as BMW, which also has BMW x1, 320, x5, etc., we can also implement it very simply, just implement the abstract factory, and then implement the abstract car in the specific car, and the expansion is also very convenient.
Summarize
It’s almost time to get out of the abstract factory here. The factory method model is a relatively simple and good design model.
But there are also disadvantages, such as: if there are new and other types of cars, you only need to implement abstract factories and abstract cars, and then create two new classes
But if it is not a car, but a mobile phone, then you have to write it all, which will cause a lot of classes to appear, whether to adopt the factory method model or decide based on needs.
Note: Don't apply design patterns for the sake of design. Of course, the factory method patterns are also commonly used, such as: Activity's onCreate method, we also load and generate corresponding interfaces based on the XML layout we define, etc.
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.