Design mode - factory mode

Posted by Infinitus 8 on Mon, 20 Dec 2021 00:56:55 +0100

Factory mode

explain

Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern belongs to creation pattern, which provides the best way to create objects.

effect

  1. Decoupling; For example, when we buy a computer, we don't need the factory model. We need a new computer, which is equivalent to building a computer ourselves. With the factory model, we just need to tell the factory what brand of computer we want, regardless of how the computer is created; There is nothing that can not be solved by adding a layer;
  2. High expansibility; If you want to add a product, just extend a factory class.
  3. Easy to maintain and reduce the amount of code; Mask the specific implementation of the product, and the caller only cares about the product interface.

classification

1. Simple factory mode (not belonging to 23 design modes, most of which are actually used)

  1. Create an interface
public interface Computer {
     void produce();
}
  1. Interface implementation class
    DellComputer class
public class DellComputer implements Computer {
    public void produce() {
        System.out.println("Buy Dell Computers");

    }
}

LenovoComputer class

public class LenovoComputer implements Computer {
   public void produce() {
       System.out.println("Buy Lenovo computer");
   }
}

  1. Create a factory and create objects according to the passed parameters
 public class ComputerFactory {
    public Computer buyComputer(String name){
       if(name.equals("dell")){
           return new DellComputer();
       }else if(name.equals("lenovo")){
           return  new LenovoComputer();

       }else{
           return null;
       }


    }
}
  1. Use the factory to get the corresponding object
import org.junit.Test;

public class TestComputer {


    @Test
    public  void getComputer(){
        ComputerFactory computerFactory=new ComputerFactory();
        Computer computer=computerFactory.buyComputer("dell");
        computer.produce();

    }
}
  1. results of enforcement

shortcoming

If you want to buy ASUS now, you need to change the logic in the factory class, which is not in line with the opening and closing principle (open to expansion and close to modification) in the seven principles of oop, so the factory mode is introduced;

2. Factory mode


2. New product acer

AcerComputer class

public class AcerComputer implements Computer {
   @Override
   public void produce() {
       System.out.println("Buy Acer computer");
   }
}
  1. Declare factory interface
public interface Factory {
  public Computer createComputer();
}

  1. Factory class producing acer
public class AcerComputerFactor implements  Factory {
   @Override
   public Computer createComputer() {

       return new AcerComputer();
   }
}

  1. Factory class producing lenovo
public class LenovoComputerFactor implements  Factory {
  @Override
  public Computer createComputer() {

      return new LenovoComputer();
  }
}


  1. Factory class producing dell
public class DellComputerFactor implements  Factory {
  @Override
  public Computer createComputer() {
      return new DellComputer();
  }
}


  1. Use the factory to get the corresponding object
import org.junit.Test;

public class TestComputer {


    @Test
    public  void getComputer(){
       Factory factory;
       factory=new DellComputerFactor();
        factory.createComputer().produce();
    }
}
  1. Operation results

shortcoming

No matter how many classes are added, we do not need to modify the code in the original class, but by adding factory classes. However, if there are too many classes, we have to create multiple factories, which is troublesome;

summary

Simple factory: a factory class and a product abstract class.
Factory method: multiple factory classes, one product abstract class.

Topics: Design Pattern