Explanation of factory pattern of java design pattern


Overview

  1. The factory pattern ( simple factory pattern , factory method pattern, abstract factory pattern) is the most commonly used one of the 23 design patterns and belongs to the creational pattern in design patterns .
  2. The factory pattern is a manifestation of java interface-oriented programming. When creating objects, we will not expose the creation logic to the client, but create objects through abstract interfaces, separate use and creation, reduce coupling and facilitate expansion
  3. There are many application scenarios for the factory model, such as jdbc, spring ioc, spring aop, parsing xml, Executors, etc., there are countless, in actual development, such as exporting different formats of reports, docking some interfaces of different hardware manufacturers, etc.

Simple factory pattern

Insert picture description here

/**
 * 手机(抽象产品角色)
 */
public interface Phone {
    
    

    //获取手机
    void getPhone();
}

/**
 * @date 2020/5/9 19:55
 * @Author yz
 * 华为手机实现类(具体产品角色)
 */
public class HuaweiPhone implements Phone{
    
    
    @Override
    public void getPhone() {
    
    
        System.out.println("华为手机");
    }
}

/**
 * @date 2020/5/9 19:54
 * @Author yz
 * 小米手机实现类(具体产品角色)
 */
public class XiaomiPhone implements Phone{
    
    
    @Override
    public void getPhone() {
    
    
        System.out.println("小米手机");
    }
}

/**
 * @date 2020/5/9 19:56
 * @Author yz
 * 手机工厂类(工厂角色)
 */
public class PhoneFactory {
    
    

    /**
     * 生产手机
     */
    public Phone producePhone(String name){
    
    
        Phone phone=null;
        if("xiaomi".equals(name))
            phone=new XiaomiPhone();
        if("huawei".equals(name))
            phone=new HuaweiPhone();
        return phone;
    }
}
public class Customer {
    
    

    public static void main(String[] args) {
    
    
        PhoneFactory phoneFactory=new PhoneFactory();
        Phone xiaomi = phoneFactory.producePhone("xiaomi");
        Phone huawei = phoneFactory.producePhone("huawei");
        xiaomi.getPhone();
        huawei.getPhone();
    }
}

composition

  1. Factory role (PhoneFactory)
  2. Abstract product role (Phone)
  3. Multiple specific product roles (HuaweiPhone, XiaomiPhone)

application

  1. The business is relatively simple, with fewer implementation classes
  2. Fixed business, basically no need to modify
  3. The caller does not need to care about the implementation logic

advantage

Shielding the implementation, the caller does not need to care about the specific implementation, good encapsulation, decoupling, and convenient for business expansion. When a new implementation is needed, only need to add the implementation class and modify the factory class.

Disadvantage

Every time you add an implementation class, you need to modify the factory class, which violates the principle of opening and closing, and when there are too many implementation classes, it is not easy to maintain and the factory class will become bloated.

Factory method pattern

Insert picture description here

/**
 * 手机(抽象产品角色)
 */
public interface Phone {
    
    

    //得到手机
    void getPhone();
}

/**
 * @date 2020/5/9 19:54
 * @Author yz
 * 小米手机实现类(具体产品角色)
 */
public class XiaomiPhone implements Phone {
    
    
    @Override
    public void getPhone() {
    
    
        System.out.println("小米手机");
    }
}
/**
 * @date 2020/5/9 19:55
 * @Author yz
 * 华为手机实现类(具体产品角色)
 */
public class HuaweiPhone implements Phone {
    
    
    @Override
    public void getPhone() {
    
    
        System.out.println("华为手机");
    }
}
/**
 * @date 2020/5/9 19:56
 * @Author yz
 * 手机工厂类(抽象工厂)
 */
public interface PhoneFactory {
    
    
    /**
     * 生产手机
     */
    public Phone producePhone();
}

/**
 * @date 2020/5/9 19:54
 * @Author yz
 * 小米手机工厂类(实例工厂)
 */
public class XiaomiPhoneFactory implements PhoneFactory {
    
    

    @Override
    public Phone producePhone() {
    
    
        return new XiaomiPhone();
    }
}

/**
 * @date 2020/5/16 11:27
 * @Author yz
 * 华为手机工厂类(实例工厂)
 */
public class HuaweiPhoneFactory implements PhoneFactory {
    
    

    @Override
    public Phone producePhone() {
    
    
        return new HuaweiPhone();
    }
}

/**
 * @date 2020/5/9 19:59
 * @Author yz
 * 客户类
 */
public class Customer {
    
    

    public static void main(String[] args) {
    
    
        PhoneFactory xiaomiPhoneFactory=new XiaomiPhoneFactory();
        PhoneFactory huaweiPhoneFactory=new HuaweiPhoneFactory();
        xiaomiPhoneFactory.producePhone().getPhone();
        huaweiPhoneFactory.producePhone().getPhone();
    }
}

composition

  1. Abstract factory role (PhoneFactory)
  2. Abstract product role (Phone)
  3. Multiple specific product roles (HuaweiPhone, XiaomiPhone)
  4. Multiple specific factory roles (HuaweiPhoneFactory, XiaomiPhoneFactory)

advantage

It conforms to a single responsibility. When a new implementation is added, there is no need to modify the code, just add new products and factories, which solves the shortcomings of simple factories that violate the principle of opening and closing.

Disadvantage

Every time a product (instance class) is added, a factory class needs to be added, which can easily increase the number of factory classes and increase the management difficulty

Abstract factory method

Insert picture description here

/**
 * 手机(抽象产品角色)
 */
public interface Phone {
    
    

    //生产
    void getPhone();
}
/**
 * @date 2020/5/9 19:54
 * @Author yz
 * 小米手机实现类(具体产品角色)
 */
public class XiaomiPhone implements Phone {
    
    
    @Override
    public void getPhone() {
    
    
        System.out.println("小米手机");
    }
}
/**
 * @date 2020/5/9 19:55
 * @Author yz
 * 华为手机实现类(具体产品角色)
 */
public class HuaweiPhone implements Phone {
    
    
    @Override
    public void getPhone() {
    
    
        System.out.println("华为手机");
    }
}
/**
 * 充电器(抽象产品角色)
 */
public interface Charger {
    
    

    //得到充电器
    void getCharger();
}
/**
 * @date 2020/5/31 16:17
 * @Author yz
 * 小米充电器实现类(具体产品角色)
 */
public class XiaomiCharger implements Charger{
    
    
    @Override
    public void getCharger() {
    
    
        System.out.println("小米充电器");
    }
}
/**
 * @date 2020/5/31 16:17
 * @Author yz
 * 华为充电器实现类(具体产品角色)
 */
public class HuaweiCharger implements Charger{
    
    
    @Override
    public void getCharger() {
    
    
        System.out.println("华为充电器");
    }
}

/**
 * @date 2020/5/9 19:56
 * @Author yz
 * 电子产品工厂类(抽象工厂)
 */
public interface ElectronicFactory {
    
    
    /**
     * 得到手机
     */
    public Phone producePhone();

    /**
     * 得到充电器
     */
    public Charger produceCharger();
}
/**
 * @date 2020/5/9 19:54
 * @Author yz
 * 小米工厂类(实例工厂)
 */
public class XiaomiFactory implements ElectronicFactory {
    
    

    @Override
    public Phone producePhone() {
    
    
        return new XiaomiPhone();
    }

    @Override
    public Charger produceCharger() {
    
    
        return new XiaomiCharger();
    }
}
/**
 * @date 2020/5/16 11:27
 * @Author yz
 * 华为工厂类(实例工厂)
 */
public class HuaweiFactory implements ElectronicFactory {
    
    

    @Override
    public Phone producePhone() {
    
    
        return new HuaweiPhone();
    }

    @Override
    public Charger produceCharger() {
    
    
         return new HuaweiCharger();
    }
}
public class Customer {
    
    

    public static void main(String[] args) {
    
    
        ElectronicFactory xiaomiPhoneFactory=new XiaomiFactory();
        ElectronicFactory huaweiPhoneFactory=new HuaweiFactory();
        xiaomiPhoneFactory.producePhone().getPhone();
        huaweiPhoneFactory.producePhone().getPhone();
        xiaomiPhoneFactory.produceCharger().getCharger();
        huaweiPhoneFactory.produceCharger().getCharger();
    }
}

composition

  1. Abstract factory role (ElectronicFactory)
  2. Multiple abstract product roles (Phone, Charger)
  3. Multiple specific product roles (HuaweiPhone, XiaomiPhone, HuaweiCharger, XiaomiCharger)
  4. Multiple specific factory roles (HuaweiFactory, XiaomiFactory)

advantage

It conforms to the principle of opening and closing, and it is easy to increase the product family. It only needs to increase the corresponding products and factories, with high scalability.

Disadvantage

When you need to add products, you need to modify all the methods of the factory class (abstract factory, instance factory), which violates the principle of opening and closing

The difference between abstract factory method and factory method

Factory method pattern: An abstract product class can derive multiple concrete product classes. An abstract factory class can derive multiple concrete factory classes. Each concrete factory class can only create one instance of a concrete product class.

Abstract factory pattern: multiple abstract product categories, each abstract product category can derive multiple specific product categories. An abstract factory class can derive multiple concrete factory classes. Each concrete factory class can create multiple instances of concrete product classes.

Difference: The factory method pattern has only one abstract product class, while the abstract factory pattern has multiple. The concrete factory class of the factory method pattern can only create one instance of the concrete product class, while the abstract factory pattern can create multiple instances.

to sum up

Three methods have advantages and disadvantages, from the complex structure and expanded view of the abstract factory> factory method> simple plant , the actual development, it is used in most simple factory, factory method and use less abstract factory, factory Although methods and abstract factories increase the scalability of the system, they are more troublesome to modify than simple factories. The real masters have no tricks to win. The martial arts masters can't follow the tricks 100%. Use it in combination with the business. The best is the best.

Guess you like

Origin blog.csdn.net/qq_38306425/article/details/106004298