1, Factory mode understanding
How to understand the factory model? Simply put, it is to transfer the creation process of the class to the factory class.
If the initialization of our classes is complex, or there are implementations of different classes, we can consider using factory mode.
Factory, as the name suggests, is a place for production. It is more convenient to expand and manage the production of this kind of factory.
2, Classification
Generally, the factory mode is divided into three categories:
- Simple factory (static factory)
- Factory method
- Abstract factory
Let's distinguish these three differences from the latitude of AbstractFactory
1. Simple factory
The simple factory does not have AbstractFactory and does not abstract the factory. It directly generates different entity classes according to different names. Although it does not meet the opening and closing principle, I think it is simple and cool to use, and the development is enough.
2. Factory method
The factory method will abstract AbstractFactory. Each type of product will implement a factory and inherit AbstractFactory, which is aimed at a single product. For example, for vehicles, we can have bicycle factory, automobile factory, aircraft factory, etc.
3. Abstract Factory
Abstract factory is the advanced method of factory. In fact, there is little difference, but abstract factory aims at a wider range, and its production is product family. For example, we also have drivers for vehicles, and we also have fashion for drivers. At this time, there are product families: vehicles, drivers and fashion. Then the unified production of this dozen products is what we call an abstract factory.
3, For example
The example sub link comes again. Similarly, since it is divided into three categories, we will give three examples. In fact, it is an upgrading process.
Take transportation as an example:
Simple factory implementation:
First, prepare a series of entity classes of vehicles:
/** * @description: vehicle **/ public abstract class Vehicle { public abstract void go(); }
public class Car extends Vehicle{ @Override public void go() { System.out.println("This is a car running"); } }
public class Plane extends Vehicle{ @Override public void go() { System.out.println("This is the plane flying"); } }
public class Bicycle extends Vehicle{ @Override public void go() { System.out.println("Cycling Bicycle"); } }
Realize simple factory
public class SimpleFactory { public static Vehicle createVehicle(String name) { switch (name) { case "car": return new Car(); case "plane": return new Plane(); case "bicycle": return new Bicycle(); default: return null; } } }
use:
public class Main { public static void main(String[] args) { Vehicle vehicle=SimpleFactory.createVehicle("car"); vehicle.go(); } }
It's very convenient for us to produce vehicles according to different names. Just pass different names to different vehicles
Based on the simple factory to factory method
Factory method implementation
Abstract factory class
public abstract class AbstractVehicleFactory { public abstract Vehicle createVehicle(); }
public class BicycleFactory extends AbstractVehicleFactory{ @Override public Vehicle createVehicle() { System.out.println("I did a lot of things when I created my bike"); return new Bicycle(); } }
public class CarFactory extends AbstractVehicleFactory{ @Override public Vehicle createVehicle() { System.out.println("When I built the car, I did a series of precise operations"); return new Car(); } }
public class PlanFactory extends AbstractVehicleFactory{ @Override public Vehicle createVehicle() { System.out.println("When I built the plane, I did a series of precise operations"); return new Plane(); } }
use
public class Main { public static void main(String[] args) { AbstractVehicleFactory factory = new PlanFactory(); Vehicle vehicle = factory.createVehicle(); vehicle.go(); } }
Of course, when creating a factory, we can also read the configuration file configuration for different creation, which is similar to the case method of a simple factory.
Abstract factory method
As we said before, the difference between abstract factory and factory method is that the former produces a series of products and the latter is a single category. In fact, it's easy to understand. It just expands the scope of creation control
To do this, we need to expand our product family:
New clothes:
public abstract class Clothes { public abstract void shining(); }
public class Jacket extends Clothes{ @Override public void shining() { System.out.println("The jacket glowed brown"); } }
public class ShortSleeve extends Clothes{ @Override public void shining() { System.out.println("Short sleeves give off a white light"); } }
public class Windbreaker extends Clothes{ @Override public void shining() { System.out.println("The windbreaker glowed black"); } }
New role:
public abstract class Character { public abstract void whoIsThat(); }
public class CommonHuman extends Character{ @Override public void whoIsThat() { System.out.println("I'm an ordinary person"); } }
public class LongAT extends Character{ @Override public void whoIsThat() { System.out.println("I'm long Aotian"); } }
public class ZhangSan extends Character { @Override public void whoIsThat() { System.out.println("I'm Zhang San"); } }
Abstract the factory that produces a family:
public abstract class AbstractFactory { public abstract Character createCharacter(); public abstract Clothes createClothes(); public abstract Vehicle createVehicle(); }
public class CommonHumanFactory extends AbstractFactory{ @Override public Character createCharacter() { return new CommonHuman(); } @Override public Clothes createClothes() { return new Jacket(); } @Override public Vehicle createVehicle() { return new Car(); } }
public class LongATFactory extends AbstractFactory{ @Override public Character createCharacter() { return new LongAT(); } @Override public Clothes createClothes() { return new Windbreaker(); } @Override public Vehicle createVehicle() { return new Plane(); } }
public class ZhangSanFactory extends AbstractFactory{ @Override public Character createCharacter() { return new ZhangSan(); } @Override public Clothes createClothes() { return new ShortSleeve(); } @Override public Vehicle createVehicle() { return new Bicycle(); } }
use:
public class Main { public static void main(String[] args) { AbstractFactory abstractFactory=new LongATFactory(); Character character=abstractFactory.createCharacter(); Vehicle vehicle=abstractFactory.createVehicle(); Clothes clothes=abstractFactory.createClothes(); character.whoIsThat(); vehicle.go(); clothes.shining(); } }
4, Summary
Factory mode is traditionally divided into three types:
Simple factory pattern, factory method pattern, abstract factory pattern.
The difference is simple:
- A simple factory has only one factory
- The factory method abstracts the factory and produces a single class
- Abstract factory abstracts the factory of a product family and produces a family of class bound products
The advantage of using the factory mode is that it shields the complex creation process at the bottom of the class, and can carry out production uniformly. The process is easier to manage. It is open to expansion and closed to modification, which is in line with the opening and closing principle.
In daily development, if you encounter a more complex customization process of a series of classes, you can consider using factory mode to optimize your code.
Many people should be curious about the above three distinctions. Which is good and which is bad?
In fact, there is no difference between good and bad in real development. Being suitable and easy to use is the best. The same types of direct can also be used in combination, and flexible use of methods is the fundamental way. I'm dying stranded. I hope this article will help you.
At the same time, welcome to add personal wechat dyinggq to exchange and learn together~~
I am dying stranded , I always look forward to meeting you. Whether you expect it or not, the tide rises and falls, I'm just here
I'll see you next time~