java factory pattern

Posted by Crashin on Mon, 20 Sep 2021 17:47:32 +0200

Introduction:

Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern is a creation pattern, which provides the best way to create objects.
As the name suggests, a factory is the place where products are produced. The factory mode includes simple factory, static factory, factory method, abstract factory, etc. I will not j introduce the simple factory and static factory one by one. The single example mode can be regarded as a static factory. We will focus on factory method and abstract factory.

1. Factory method

Applicable scenarios:

The consumer does not care about the class (product) he wants to create, or the consumer knows what product he wants to create, but does not care about the creation process

Mode instance:
1.1. Moveable: the parent class of the vehicle
package com.mashibing.factory.factoryMethod;
public interface Moveable {
    void go();
}
1.2 Car, vehicle
package com.mashibing.factory.factoryMethod;
public class Car implements Moveable{
    public void go(){
        System.out.println("Car go wuwuwuwuwuwuwu......");
 }
}
1.3. CarFactory: the factory that generates the Car
package com.mashibing.factory.factoryMethod;
public class CarFactory {
    public Moveable create(){
        System.out.println("a car created!");
 return new Car();
 }
}
1.4 Plane: aircraft
package com.mashibing.factory.factoryMethod;
public class Plane implements Moveable{
    public void go() {
        System.out.println("plane flying shuashua......");
 }
}
1.5 PlaneFactory: the factory that generates aircraft
package com.mashibing.factory.factoryMethod;
public class PlaneFactory {
    public Moveable create(){
        System.out.println("a plane created!");
 return new Plane();
 }
}
1.6 testing
package com.mashibing.factory.factoryMethod;
public class Main {
    public static void main(String[] args) {
//        Moveable m = new CarFactory().create();
 Moveable m = new PlaneFactory().create();
 m.go();
 }
}

Conclusion: I don't care about how to produce the product. I need a car. Then I find a car factory and you generate a car for me. I need an airplane. Find the factory that makes the airplane and you build an airplane for me.
The factory method mode is more convenient in expanding products. If I need a ship, I can add a ship product category and a shipbuilding factory

2. Abstract factory pattern

Applicable scenarios:

The factory method mode is more convenient for expanding products, but if what I want is not a product, but a series of products, for example, write a small tank war game: the objects inside include tanks, bullets, explosions, etc. an ordinary tank hits an ordinary bullet to produce an ordinary explosion (this is a product family). An upgraded tank fires a nuclear bomb to produce a more powerful explosion (this is another product family). If the factory method mode is still used, although it can be implemented, it seems not well organized, let's try the abstract factory mode.

Mode instance:

Tank is a small game. There are many codes that are difficult to paste. Let's give a simple example. A modern man drives a car, eats bread and holds an AK47 gun; a man in the magic world rides a broom, eats poisonous mushrooms and holds a magic wand.
This picture: I draw myself a little confused. After reading the following code, look at this picture a little better:

2.1 Food: Food abstract class
package com.mashibing.factory.abstractFactory;
public abstract class Food {
    abstract void printName();
}
2.2. Bread: inherited from Food
package com.mashibing.factory.abstractFactory;
public class Bread extends Food{
    public void printName(){
        System.out.println("bread");
 }
}
2.3. Bread: inherited from Food
package com.mashibing.factory.abstractFactory;
public class MushRoom extends Food{
    public void printName(){
        System.out.println("Poisonous mushroom");
 }
}
2.4. Weapon: weapon abstract class
package com.mashibing.factory.abstractFactory;
public abstract class Weapon {
    abstract void shoot();
}
2.5 Ak47: inherited from Weapon
package com.mashibing.factory.abstractFactory;
public class Ak47 extends Weapon{
    public void shoot(){
        System.out.println("tututututu......");
 }
}
2.6 MagicStick: inherited from Weapon
package com.mashibing.factory.abstractFactory;
public class MagicStick extends Weapon{
    public void shoot(){
        System.out.println("diandian......");
 }
}
2.7 vehicle: Vehicle abstract class
package com.mashibing.factory.abstractFactory;
public abstract class Vehicle {
    abstract void go();
}
2.8 Car: inherited from Vehicle
package com.mashibing.factory.abstractFactory;
public class Car extends Vehicle{
    public void go(){
        System.out.println("Car go wuwuwuwuwuwuwu......");
 }
}
2.9. Broom: inherited from Vehicle
package com.mashibing.factory.abstractFactory;
public class Broom extends Vehicle{
    public void go(){
        System.out.println("broom ........");
 }
}
2.10 abstract: abstract Factory
package com.mashibing.factory.abstractFactory;
/**
 * Abstract factory
 */
public abstract class AbstractFactory {
    abstract Food createFood();
 abstract Vehicle createVehicle();
 abstract Weapon createWeapon();
}
2.11. ModernFactory: a specific factory serving modern people
package com.mashibing.factory.abstractFactory;
/**
 * Specific factory
 */
public class ModernFactory extends AbstractFactory{
    @Override
    Food createFood() {
        return new Bread();
 }
    @Override
    Vehicle createVehicle() {
        return new Car();
 }
    @Override
    Weapon createWeapon() {
        return new Ak47();
 }
}
2.12 MagicFactory: a specific factory serving people in the magical world

package com.mashibing.factory.abstractFactory;
/**

  • Specific factory
    */
    public class MagicFactory extends AbstractFactory{
    @Override
    Food createFood() {
    return new MushRoom();
    }
    @Override
    Vehicle createVehicle() {
    return new Broom();
    }
    @Override
    Weapon createWeapon() {
    return new MagicStick();
    }
    }
2.13 testing
package com.mashibing.factory.abstractFactory;
public class Main {
    public static void main(String[] args) {
        //Magic World Series
//        AbstractFactory f = new MagicFactory();
//        Vehicle c = f.createVehicle();
//        c.go();
//        Weapon w = f.createWeapon();
//        w.shoot();
//        Food b = f.createFood();
//        b.printName();

 //Modern man series
 AbstractFactory f = new ModernFactory();
 Vehicle c = f.createVehicle();
 c.go();
 Weapon w = f.createWeapon();
 w.shoot();
 Food b = f.createFood();
 b.printName();
 }
}

Summary: it's convenient to abstract the factory mode when expanding a family of products, but it's not so convenient when expanding a single product. There are good and bad. Referring to spring's ioc, you can expand both a single product and a product family. I haven't studied it yet. I'll write notes after the research. Thank you

Topics: Java Design Pattern