SoFunction
Updated on 2025-03-08

Detailed explanation of the factory model of Java design pattern

1. Simple factory model

A factory produces both Huawei and Xiaomi mobile phones.

  • Mobile phone interface class
package ;

public interface Phone {

    void showInfo();
}
  • Huawei mobile phone implementation class
package ;

public class HuaWeiPhone implements Phone{
    @Override
    public void showInfo() {
        ("Huawei Mobile");
    }
}
  • Xiaomi mobile phone implementation category
package ;

public class XiaoMiPhone implements Phone{
    @Override
    public void showInfo() {
        ("Xiaomi Mobile");
    }
}
  • Manufacturing mobile phone factory
package ;

import ;

public class SimpleFactory {

    public static Phone getPhone(String className){
        Phone phone = null;
        try {
            Class<Phone> aClass = (Class<Phone>) (className);
            phone = ();
        } catch (ClassNotFoundException e) {
            ();
        } catch (InstantiationException e) {
            ();
        } catch (IllegalAccessException e) {
            ();
        }
        return phone;
    }
}
  • Specific usage classes
package ;

import ;

public class SimpleFactoryMain {

    public static void main(String[] args) {
        Phone phone = ("");
        ();
    }
}

2. Factory method model

  • A factory interface class for producing mobile phones
package ;

import ;

public interface PhoneFactory {

    Phone getPhone();
}
  • The factory that produces Huawei mobile phones
package ;

import ;
import ;

public class HuaWeiPhoneFactory implements PhoneFactory {
    @Override
    public Phone getPhone() {
        return new HuaWeiPhone();
    }
}
  • The specific factory for producing Xiaomi mobile phones
package ;

import ;
import ;

public class XiaoMiPhoneFactory implements PhoneFactory {
    @Override
    public Phone getPhone() {
        return new XiaoMiPhone();
    }
}
  • Specific usage classes
package ;

import ;

public class FactoryMethodMain {

    public static void main(String[] args) {
        PhoneFactory phoneFactory = new XiaoMiPhoneFactory();
        Phone phone = ();
        ();
    }
}

3. Abstract factory pattern

Now, whether it is Huawei or Xiaomi, they are no longer satisfied with only producing mobile phones. They have also started to build cars. Now there is a concept of "group", or family.

  • Car interface class
package ;

public interface Car {

    void showInfo();
}
  • Huawei Auto
package ;

public class HuaWeiCar implements Car {
    @Override
    public void showInfo() {
        ("Huawei Car");
    }
}
  • Xiaomi Auto
package ;

public class XiaoMiCar implements Car {
    @Override
    public void showInfo() {
        ("Xiaomi Auto");
    }
}
  • Abstract factory, which defines the ability to produce both mobile phones and cars
package ;

import ;
import ;

public interface AbstractFactory {

    Phone getPhone();

    Car getCar();
}
  • Huawei Factory
package ;

import ;
import ;
import ;
import ;

public class HuaWeiFactory implements AbstractFactory{
    @Override
    public Phone getPhone() {
        return new HuaWeiPhone();
    }

    @Override
    public Car getCar() {
        return new HuaWeiCar();
    }
}
  • Xiaomi Factory
package ;

import .*;

public class XiaoMiFactory implements AbstractFactory{
    @Override
    public Phone getPhone() {
        return new XiaoMiPhone();
    }

    @Override
    public Car getCar() {
        return new XiaoMiCar();
    }
}
  • Specific use
package ;

import ;
import ;

public class AbstractFactoryMain {

    public static void main(String[] args) {
        AbstractFactory abstractFactory = new XiaoMiFactory();
        Car car = ();
        Phone phone = ();
        ();
        ();
    }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.