Hello, I am Xiao Huang, a Java development engineer of Unicorn Enterprise. The school recruited dozens of offers, the annual salary is 20W~40W. Thank you for meeting us in the vast sea of people. As the saying goes: When your talent and ability are not enough to support your dream, please calm down and study. I hope you can study with me and work hard together to realize your own dream. Welcome to pay attention to my public number: “love to knock code yellow”.
Abstract Factory pattern
1. Introduction
Intent: Provide an interface to create a set of dependencies or dependencies without specifying their specific classes
When to use: The system has more than one product family, and the system only consumes the products of one of these family, as shown in the following example
There is currently a dress-change game where you need to choose different game suits to enter different game scenes. For example, the normal city suit contains the following items:
- Food: bread
- Weapons: AK47
- Vehicle: BMW
The image of a man eating bread, carrying an AK47 and driving a BMW was on the page, but when we entered the police station, if our guns were seen by the police, the game would be over.
Therefore, we need to carry out one-click costume change, and transform our character image into one eating lollipop, holding computer and riding skateboard, so that we can enter the police station and complete our character. Next, let’s see how this can be achieved.
2. Implement
Our overall structure is shown as follows:
We package weapons, food and vehicles into their own abstract classes to implement different methods, such as AK47 and Computer
Create an AbstractFactory AbstractFactory that produces a family of products, such as weapons, food, vehicles, etc
We need to customize different factory classes for different scenarios
Normal people’s dress-changing factories, we return BMWS, bread, AK47s
The police department’s costume factory, we sent back skateboards, lollipops, computers
You can accomplish this requirement by specifying different reload factories in Main
3. Code
-
Weapon: public abstract class Weapon {abstract void shoot(); }
- AK47:
public class AK47 extends Weapon {... }
- Computer:
public class Computer extends Weapon {... }
- AK47:
-
Vehicle: public abstract class Vehicle {abstract void go(); }
- BMW:
public class BMW extends Vehicle {... }
- SkateBoard:
public class SkateBoard extends Vehicle {... }
- BMW:
-
Food: public abstract class Food {abstract void printName(); }
- Bread:
public class Bread extends Food {... }
- StickCandy:
public class StickCandy extends Food {... }
- Bread:
-
AbstractFactory
/** * abstract factory */ public abstract class AbstractFactory { abstract Food createFood(a); / / vehicle abstract Vehicle createVehicle(a); / / weapons abstract Weapon createWeapon(a); } Copy the code
-
CleverFactory
public class CleverFactory extends AbstractFactory{ @Override Food createFood(a) { return new StickCandy(); } @Override Vehicle createVehicle(a) { return new SkateBoard(); } @Override Weapon createWeapon(a) { return newComputer(); }}Copy the code
-
ModerenFactory
public class ModerenFactory extends AbstractFactory { @Override Food createFood(a) { return new Bread(); } @Override Vehicle createVehicle(a) { return new BMW(); } @Override Weapon createWeapon(a) { return newAK47(); }}Copy the code
-
The test class
public class Main { public static void main(String[] args) { // Normal people walk for a period of time AbstractFactory abstractFactory = new ModerenFactory(); // Perform the assignment Vehicle vehicle = abstractFactory.createVehicle(); Weapon weapon = abstractFactory.createWeapon(); Food food = abstractFactory.createFood(); // When you walk to the police station, you need to change your clothes abstractFactory = new CleverFactory(); // Perform the assignmentvehicle = abstractFactory.createVehicle(); weapon = abstractFactory.createWeapon(); food = abstractFactory.createFood(); }}Copy the code
4. To summarize
We can see that through the above way, we can complete the transformation of clothing in an instant
It’s also convenient when we’re doing extensions
If I currently need to add a Food as a hamburger, I just need to inherit our Food
AbstractFactory also has some disadvantages. When I need to add a product as a hobby, I need to create that abstract class and specify it in AbstractFactory, this extension is not easy
Of course, everything has advantages and disadvantages. When we create an abstract factory, we can plan the products of this batch of product family in advance to prevent subsequent addition and maintenance
Well, that’s the end of our abstract Factory
I am a Java development engineer of a unicorn enterprise. If you have any questions, please leave a message or send a private message to me on wechat. I will see you next time