Quickly understand factory mode

Factory mode:

effect:

  1. It realizes the separation of caller and callee
  2. Detailed classification
    • Simple factory mode
    • Factory method model
    • Abstract factory pattern
  • Meet the seven principles of OOP
    • Opening and closing principle
    • Dependency Inversion Principle
    • Dimitt's law

Three modes

Simple factory mode:

  • It is used to produce any product in the same hierarchical structure (for adding new products, the existing code needs to be overwritten)
    • Interface
package factory.simple;
public interface Car {
    void name();
}	
  • factory
package factory.simple;

//Simple factory is also called static factory mode
//It is impossible to add a new product without changing the source code if the opening and closing principle is not satisfied
public class CarFactory {

  public static Car getCarFactory(String car){
      if (car.equals("Wuling")){
          return new Wuling();
      }else if (car .equals("Tesla") ){
          return new Tesla();
      }else{
          return  null;
      }
  }
}
  • Implementation class
package factory.simple;

public class Wuling implements Car{

 @Override
 public void name() {
     System.out.println("Wuling Hongguang");
 }
}
package factory.simple;

public class Tesla implements Car{

    @Override
    public void name() {
        System.out.println("Tesla");
    }
}
  • consumer
package factory.simple;

public class Consumer {
    public static void main(String[] args) {
       Car car=CarFactory.getCarFactory("Wuling");
       car.name();
    }
}

Factory method mode:

  • It is used to produce fixed products in the same hierarchical structure (any product can be added)
    • Interface
package factory.method;

public interface Car {
 void name();
}
package factory.method;

public interface CarFactory {
    Car getCar();
}
  • factory
package factory.method;

public class TeslaFactory implements CarFactory{
  @Override
  public Car getCar() {
      return new Tesla();
  }
}
package factory.method;

public class WulingFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new Wuling();
    }
}
  • Implementation classes of two factories
package factory.method;

public class Tesla implements Car {

 @Override
 public void name() {
     System.out.println("Tesla");
 }
}
package factory.method;

public class Wuling implements Car {

    @Override
    public void name() {
        System.out.println("Wuling Hongguang");
    }
}
  • Consumer
package factory.method;

public class Consumer {
   public static void main(String[] args) {
       Car car1 = new WulingFactory().getCar();
       Car car2 = new TeslaFactory().getCar();
       car1.name();
       car2.name();


       /**
        * contrast:
        * Structure complexity: simple is lower
        * Code complexity: simple is lower
        * Programming complexity: simple is lower
        * Management difficulty: simple is lower
        *
        * According to the design principle: select the factory mode!
        * According to the actual business: select a simple factory!
        *
        */
   }
}

Summary:

  • Simple factory mode (static factory mode)
    • Although it does not conform to the design principles to some extent, it is actually used most
  • Factory method model
    • The extension is realized by adding a new factory class without modifying the existing class

Application scenario:

  • getInstance() method of Calendar in JDK
  • Acquisition of Connection object in JDBC
  • Creating object management bean objects by IOC container in Spring
  • Reflect Class object newInstance() method

Abstract factory method

definition:

  • The abstract factory pattern provides an interface to create a series of related or interdependent objects without specifying their specific classes

Applicable scenario

  • The client (application layer) does not depend on the details of how to create and implement product class instances
  • Emphasize that a series of related product objects (belonging to the same product family) are used together, and creating objects requires a lot of repetitive code
  • Emphasize the library of a product class. All products appear with the same interface, so that the client does not depend on the specific implementation

advantage:

  • The isolation of specific products in the application layer does not need to care about the details of creation
  • Create a series of products together

Disadvantages:

  • All product sets that may be created are specified, and it is difficult to expand new products in the product family
  • It increases the abstraction and incomprehensibility of the system
  • It does not meet the opening and closing principle. It can be used if the product is stable

code:

  • Interface
package factory.abstract1;

public interface IProductFactory {
    //Production of mobile phones
    IphoneProduct iphoneProduct();
    //Production router
    IRouteProduct iRouteProduct();
}

package factory.abstract1;

//Mobile interface
public interface IphoneProduct {
    void start();
    void shutdown();
    void callup();
    void sendSMS();
}
package factory.abstract1;

//Router interface
public interface IRouteProduct {
    void start();
    void shutdown();
    void openwife();
    void setting();
}

  • Super factory
package factory.abstract1;

public class XiaoMiFactory implements IProductFactory {
   @Override
   public IphoneProduct iphoneProduct() {
       return new XiaoMiIphone();
   }

   @Override
   public IRouteProduct iRouteProduct() {
       return new XiaoMiRouter();
   }
}
package factory.abstract1;

public class HuaWeiFactory implements IProductFactory {
    @Override
    public IphoneProduct iphoneProduct() {
        return new HuaWeiphone();
    }

    @Override
    public IRouteProduct iRouteProduct() {
        return new HuaWeiRouter();
    }
}
  • Implementation class of implementation factory
package factory.abstract1;

public class HuaWeiphone implements IphoneProduct{
    @Override
    public void start() {
        System.out.println("Huawei power on");
    }

    @Override
    public void shutdown() {
        System.out.println("Huawei shutdown");
    }

    @Override
    public void callup() {
        System.out.println("Huawei open wifi");
    }

    @Override
    public void sendSMS() {
        System.out.println("Huawei sends SMS");
    }


}
package factory.abstract1;

public class HuaWeiRouter implements IRouteProduct{

    @Override
    public void start() {
        System.out.println("Huawei turns on the router");
    }

    @Override
    public void shutdown() {
        System.out.println("Huawei turns off the router");
    }

    @Override
    public void openwife() {
        System.out.println("Huawei shutdown WiFi");
    }

    @Override
    public void setting() {
        System.out.println("Huawei turns on routing settings");
    }
}
package factory.abstract1;

public class XiaoMiIphone implements IphoneProduct{
    @Override
    public void start() {
        System.out.println("Millet boot");
    }

    @Override
    public void shutdown() {
        System.out.println("Millet shutdown");
    }

    @Override
    public void callup() {
        System.out.println("Millet open wifi");
    }

    @Override
    public void sendSMS() {
        System.out.println("Xiaomi texting");
    }


}
package factory.abstract1;

public class XiaoMiRouter implements IRouteProduct{

    @Override
    public void start() {
        System.out.println("Xiaomi turns on the router");
    }

    @Override
    public void shutdown() {
        System.out.println("Xiaomi turns off the router");
    }

    @Override
    public void openwife() {
        System.out.println("Millet off WiFi");
    }

    @Override
    public void setting() {
        System.out.println("Millet open routing settings");
    }
}

  • client
package factory.abstract1;

import java.text.SimpleDateFormat;
import java.util.Date;
//Self deployment test
public class Client {
    public static void main(String[] args) {
//        System.out.println("Xiaomi family ------------------- >");
//        IProductFactory iProductFactory=new XiaoMiFactory();
//        IphoneProduct iphoneProduct = iProductFactory.iphoneProduct();
//        iphoneProduct.start();
//        iphoneProduct.shutdown();
//        IRouteProduct iRouteProduct = iProductFactory.iRouteProduct();
//        iRouteProduct.openwife();
//        iRouteProduct.setting();
//        System.out.println("Huawei family ------------------- >");
//        iProductFactory=new HuaWeiFactory();
//        iphoneProduct = iProductFactory.iphoneProduct();
//        iphoneProduct.start();
//        iphoneProduct.shutdown();
//        iRouteProduct = iProductFactory.iRouteProduct();
//        iRouteProduct.openwife();
//        iRouteProduct.setting();

        int max=5,min=1;
        int ran2 = (int) (Math.random()*(max-min)+min);
        System.out.println(ran2);


    }
}

Keywords: Java Design Pattern

Added by ReVeR on Sun, 19 Dec 2021 18:19:43 +0200