23 days to understand the abstract factory pattern of design pattern

Hello, I'm Xiao Huang, a Java development engineer in Unicorn enterprise
The school recruits dozens of offer s, with an average annual salary of 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 learn,
I hope you can study and work hard with me to realize your dream.
Welcome to my official account: "love knocking yellow".

Abstract factory pattern

1. Introduction

Intent: provide an interface to create a series of related or dependent interfaces without specifying their specific classes

When to use: the system has more than one product family, and the system only consumes products of one family. Examples are as follows

At present, there is a costume change game. To enter different game scenes, you need to choose different game suits, such as a normal urban suit, which contains:

  • Food: Bread
  • Weapon: AK47
  • Vehicle: BMW

At this time, a character with bread, AK47 and BMW will jump on the paper. However, when we enter the police station, if our guns are seen by the police, the game will end directly.

Therefore, we need to change our character image with one click to convert our character image into the image of eating lollipops, holding a computer and riding a skateboard. In this way, we can enter the police station and complete our character. Next, let's see how to realize this.

2. Realization

Our overall structure is as follows:

We encapsulate weapons, food and vehicles into their own abstract classes and implement their different methods, such as AK47, Computer, etc

Create an abstract factory of AbstractFactory to produce a class of product family, weapons, food, vehicles, etc

For different scenarios, we need to customize different factory classes

For the normal people's changing factory, we let them return to BMW, bread and AK47

In the dressing factory of the police station, we let it return to skateboards, lollipops and computers

This requirement can be completed by specifying different replacement plants in main

3. Code

  • Weapon: public abstract class Weapon {abstract void shoot();}

    • AK47: public class AK47 extends Weapon {...}
    • Computer: public class Computer extends Weapon {...}
  • Vehicle: public abstract class Vehicle {abstract void go();}

    • BMW: public class BMW extends Vehicle {...}
    • SkateBoard: public class SkateBoard extends Vehicle {...}
  • Food: public abstract class Food {abstract void printName();}

    • Bread: public class Bread extends Food {...}
    • StickCandy: public class StickCandy extends Food {...}
  • AbstractFactory

    /**
     * Abstract factory
     */
    public abstract class AbstractFactory {
        abstract Food createFood();
        // vehicle
        abstract Vehicle createVehicle();
        // arms
        abstract Weapon createWeapon();
    }
    
  • CleverFactory

    public class CleverFactory  extends AbstractFactory{
        @Override
        Food createFood() {
            return new StickCandy();
        }
    
        @Override
        Vehicle createVehicle() {
            return new SkateBoard();
        }
    
        @Override
        Weapon createWeapon() {
            return new Computer();
        }
    }
    
  • ModerenFactory

    public class ModerenFactory extends AbstractFactory {
        @Override
        Food createFood() {
            return new Bread();
        }
    
        @Override
        Vehicle createVehicle() {
            return new BMW();
        }
    
        @Override
        Weapon createWeapon() {
            return new AK47();
        }
    }
    
  • Test class

    public class Main {
        public static void main(String[] args) {
            // Normal people walk for a period of time
            AbstractFactory abstractFactory = new ModerenFactory();
            // Assign values
            Vehicle vehicle = abstractFactory.createVehicle();
            Weapon weapon = abstractFactory.createWeapon();
            Food food = abstractFactory.createFood();
            // When we get to the police station, we need to change clothes
            abstractFactory = new CleverFactory();
            // Assign values
            vehicle = abstractFactory.createVehicle();
            weapon = abstractFactory.createWeapon();
            food = abstractFactory.createFood();
        }
    }
    

4. Summary

We can see that through the above methods, we can complete the transformation of clothes in an instant

It is also convenient for us to expand

If I need to add a Food hamburger at present, I just need to inherit our Food

Abstract factory also has some disadvantages. When I need to add a product as a hobby, I need to create the abstract class and specify it in AbstractFactory. This extension is not easy

Of course, everything has its advantages and disadvantages. When creating an abstract factory, we can plan the products of this batch of product families in advance to prevent subsequent addition and maintenance

Well, the abstract factory of this issue will end here

I am a Java development engineer of Unicorn enterprise. If you have any questions, you can leave a message or send a private message to me on wechat. See you next time

Keywords: Java Programming Design Pattern

Added by LooieENG on Mon, 03 Jan 2022 13:00:40 +0200