directory

background

classification

implementation

Simple factory

The factory method

The abstract factory

conclusion


background

In real business we often create objects, and the common approach is new. But in some cases, not controlling new can be a waste of resources, can’t be multiplexed, and can easily reach system bottlenecks. At this point, consider using the factory pattern. Such as database connection resources, Redis connection resources, logging and other scenarios.

classification

  • Simple Factory: The Simple Factory pattern is not strictly one of the 23 design patterns. It violates the open and close principle and breaks the close to change principle. When adding products, add corresponding product categories and specific factories.
  • Factory Method: Compared with the simple Factory mode, the Factory is further abstracted and specific products correspond to specific factories. Is this a factory or does it only produce one specific product?
  • Abstract Factory: Abstract Factory pattern. In contrast to the Factory method pattern, Abstract Factory starts to produce multi-grade products. Multiple products of different grades constitute product families.
  • Product family: a group of products produced by the same plant in a different product hierarchy.
  • Product hierarchy: The abstract superclass and its different product subclasses constitute a product hierarchy.

implementation

Simple factory

/** * computer abstract class */
public interface Computer {

    /** * How to make a computer */
    Computer make(a);
}

/** * notebook */
public class Laptop implements Computer {
    public Laptop(a) {
        System.out.println("make a Laptop Computer!");
    }

    @Override
    public Computer make(a) {
        return newLaptop(); }}/** * PC, desktop */
public class Desktop implements Computer {
    public Desktop(a) {
        System.out.println("make a Desktop Computer!");;
    }

    @Override
    public Desktop make(a) {
        return newDesktop(); }}/** * Simple factory mode */
public class ComputerSimpleFactory {
    Computer makeComputer(String computerType) {
        switch (computerType) {
            case "desktop":
                return new Desktop();
            case "laptop":
                return new Laptop();
        }
        return null;
    }

    public static void main(String[] args) {
        ComputerSimpleFactory computerSimpleFactory = new ComputerSimpleFactory();
        Computer desktop = computerSimpleFactory.makeComputer("desktop");
        Computer laptop = computerSimpleFactory.makeComputer("laptop"); }}Copy the code

The factory method

  • The Computer class uses those defined in a simple factory.
/** * abstract factory */
public interface AbstractFactory {
    Computer makeComputer(a);
}

/** * desktop factory */
public class DesktapFactory implements AbstractFactory{
    @Override
    public Computer makeComputer(a) {
        return newDesktop(); }}/** * notebook factory */
public class LaptopFactory implements AbstractFactory{
    @Override
    public Computer makeComputer(a) {
        return newLaptop(); }}public class MethodFactory {
    public static void main(String[] args) {
        // Build a desktop
        DesktapFactory desktapFactory = new DesktapFactory();
        Computer desktop = desktapFactory.makeComputer();
        // Build a laptop
        LaptopFactory laptopFactory = newLaptopFactory(); Computer laptop = laptopFactory.makeComputer(); }}Copy the code

The abstract factory

/** * abstract factory ** */
public interface AbstractFactory {
    /** * Make computer *@return* /
    public Computer makeComputer(a);

    /** * make power supply *@return* /
    public PowerSupply makePowerSupply(a);
}

/** * Dell factory ** */
public class DellFactory implements AbstractFactory{
    /** * Makes Dell computers **@return* /
    @Override
    public Computer makeComputer(a) {
        return new DellComputer();
    }

    /** * Manufacturing Dell power supply **@return* /
    @Override
    public PowerSupply makePowerSupply(a) {
        return newDellPowerSupply(); }}/**
 * 惠普工厂
 *
 * 
 */
public class HPFactory implements AbstractFactory{
    /** * makes HP computers **@return* /
    @Override
    public Computer makeComputer(a) {
        return new HpComputer();
    }

    /** * manufacturing HP power **@return* /
    @Override
    public PowerSupply makePowerSupply(a) {
        return newHPPowerSupply(); }}public class DellComputer implements Computer{
    public DellComputer(a){
        System.out.println("make a DellComputer!");
    }
    /** * HP computer */
    @Override
    public Computer make(a) {
        return null; }}public class HpComputer implements Computer {
    public HpComputer(a) {
        System.out.println("make a HpComputer!");
    }

    /** * How to make HP computers */
    @Override
    public Computer make(a) {
        return null; }}public interface PowerSupply {
    /** * Production power *@return* /
    public PowerSupply make(a);
}

public class DellPowerSupply implements PowerSupply{
    public DellPowerSupply(a){
        System.out.println("make a DeskPowerSupply");
    }

    /** * Dell Power *@return* /
    @Override
    public PowerSupply make(a) {
        return newDellPowerSupply(); }}public class HPPowerSupply implements PowerSupply{
    public HPPowerSupply(a){
        System.out.println("make a HPPowerSupply");
    }

    /** * HP Power *@return* /
    @Override
    public PowerSupply make(a) {
        return newHPPowerSupply(); }}public class AbstractFactoryTest {
    public static void main(String[] args) {
        // Dell factory
        DellFactory dellFactory = new DellFactory();
        dellFactory.makePowerSupply();
        dellFactory.makeComputer();
        // HP factory
        HPFactory hpFactory = newHPFactory(); hpFactory.makeComputer(); hpFactory.makePowerSupply(); }}Copy the code

conclusion

  • The application of factory mode and abstract factory mode is the main work practice. Usage scenarios: common resource usage, thread pool, object pool, etc
  • The main difference between the two is to produce one product and multiple products, the code form is not very different.
  • The emphasis is on understanding concepts, which can help abstract the design of real business scenarios