Featured image of post Java设计模式-工厂方法模式FactoryMethod

Java设计模式-工厂方法模式FactoryMethod

相关文章

【合集】Java设计模式

工厂方法

总结

用不同的工厂,生产不同的产品

应用

{@link Calendar#getInstance}

咖啡接口

1
2
3
public abstract static class Coffee {
    public abstract String getName();
}

具体咖啡

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public static class LatteCoffee extends Coffee {
    public String getName() {
        return "拿铁咖啡";
    }
}
public static class AmericanoCoffee extends Coffee {
    public String getName() {
        return "美式咖啡";
    }
}

咖啡工厂接口

1
2
3
public interface CoffeeFactory {
    Coffee createCoffee();
}

具体咖啡工厂

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public static class LatteCoffeeFactory implements CoffeeFactory {
    public Coffee createCoffee() {
        return new LatteCoffee();
    }
}
public static class AmericanoCoffeeFactory implements CoffeeFactory {
    public Coffee createCoffee() {
        return new AmericanoCoffee();
    }
}

咖啡店

组合了一个咖啡工厂

1
2
3
4
5
6
7
@Builder
public static class CoffeeStore {
    private CoffeeFactory coffeeFactory;
    public Coffee orderCoffee() {
        return coffeeFactory.createCoffee();
    }
}

客户端调用

1
2
3
4
Coffee coffee = CoffeeStore.builder()
          .coffeeFactory(new LatteCoffeeFactory())
          .build()
          .orderCoffee();

简单工厂(非23种设计模式)

总结

同一个工厂通过传参控制创建不同产品

咖啡接口

1
2
3
public abstract static class Coffee {
    public abstract String getName();
}

具体咖啡

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public static class LatteCoffee extends Coffee {
    public String getName() {
        return "拿铁咖啡";
    }
}
public static class AmericanoCoffee extends Coffee {
    public String getName() {
        return "美式咖啡";
    }
}

简单咖啡工厂类

通过入参type控制要生产的咖啡品类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public static class SimpleCoffeeFactory {
    public static Coffee createCoffee(String type) {
        if ("americano".equals(type)) {
            return new AmericanoCoffee();
        }
        if ("latte".equals(type)) {
            return new LatteCoffee();
        }
        throw new RuntimeException("对不起,您输入的咖啡类型不存在。");
    }
}

咖啡店

通过入参type控制要生产的咖啡品类

1
2
3
4
5
6
public static class CoffeeStore {
    public Coffee orderCoffee(String type) {
        Coffee coffee = SimpleCoffeeFactory.createCoffee(type);
        return coffee;
    }
}

客户端调用

1
2
CoffeeStore store = new CoffeeStore();
Coffee latte = store.orderCoffee("latte");

简单工厂:使用配置文件解除耦合

总结:注入到map中,通过map获取对象。

bean.properties文件

1
2
american=com.zx.pattern.factory.config_factory.AmericanCoffee
latte=com.zx.pattern.factory.config_factory.LatteCoffee

img_33.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Slf4j
@Service
@RequiredArgsConstructor
public class DemoServiceImpl implements DemoService {

    private static final Map<String, Coffee> COFFEE_MAP = new HashMap<>();

    // 读取配置文件以及创建对象写在静态代码块中,目的就是只需要执行一次。
    static {
        Properties properties = new Properties();
        try (InputStream is = DemoServiceImpl.class.getClassLoader().getResourceAsStream("bean.properties");) {
            properties.load(is);
            for (Object key : properties.keySet()) {
                String className = properties.getProperty((String) key);
                Coffee coffee = (Coffee) Class.forName(className).getDeclaredConstructor().newInstance();
                COFFEE_MAP.put((String) key, coffee);
            }
        } catch (Exception e) {
            log.error("配置失败", e);
            throw new RuntimeException("配置失败", e);
        }
    }

     public static Coffee createCoffee(String name) {
         return COFFEE_MAP.get(name);
     }
   
}

简单工厂:使用Spring Boot自动注入解除耦合

流程达到接口+实现类

1
2
3
4
5
6
7
8
public interface ProcessApproveListener {
    /**
     * 流程到达事件
     *
     * @param processApproveDTO 流程到达对象
     */
    void processApproveEvent(ProcessApproveDTO processApproveDTO);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/**
 * 租金账单流程 达到
 */
@Slf4j
@RequiredArgsConstructor
@Service(value = RENT_BILL_APPLY + "_" + PRM_PROCESS_APPROVE)
public class RentBillApplyProcessApproveListener implements ProcessApproveListener {

    @Override
    public void processApproveEvent(ProcessApproveDTO processApproveDTO) {

    }

}

流程结束接口+实现类

1
2
3
4
5
6
7
8
public interface ProcessEndListener {
    /**
     * 流程结束事件
     *
     * @param processEndDTO 流程结束对象
     */
    void processEndEvent(ProcessEndDTO processEndDTO);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/**
 * 租金账单流程 结束
 */
@Slf4j
@RequiredArgsConstructor
@Service(value = RENT_BILL_APPLY + "_" + PRM_PROCESS_END)
public class RentBillApplyProcessEndListener implements ProcessEndListener {

    @Override
    public void processEndEvent(ProcessEndDTO processEndDTO) {

    }

}

常量类

1
2
3
4
5
6
7
@UtilityClass
public class ProcessKeyConstants {
    /**
     * 租金账单流程
     */
    public static final String RENT_BILL_APPLY = "rent_bill_apply";
}

客户端调用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Slf4j
@Component
@RequiredArgsConstructor
public class MqConsumer {
    private final Map<String, ProcessApproveListener> processApproveServiceMap;
    private final Map<String, ProcessEndListener> processEndServiceMap;

    public void processApproveListener(String message) throws Exception {
        ProcessApproveListener processApproveListener = 
            processApproveServiceMap.get(processApproveVo.getProcessDefinitionKey() + "_" + PRM_PROCESS_APPROVE);

        if (processApproveListener != null) {
            ProcessApproveDTO processApproveDTO = new ProcessApproveDTO();
            processApproveListener.processApproveEvent(processApproveDTO);
        }
    }

    public void processEndListener(String message) throws Exception {
        ProcessEndListener processEndListener = 
            processEndServiceMap.get(processEndVo.getProcessDefinitionKey() + "_" + PRM_PROCESS_END);

        if (processEndListener != null) {
            ProcessEndDTO processEndDTO = new ProcessEndDTO();
            processEndListener.processEndEvent(processEndDTO);
        }
    }
}

可以使用Rabbit MQ监听

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Component
@RequiredArgsConstructor
@Slf4j
public class MqConsumer {
    /**
     * 流程到达
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QUEUE_BOL_APPROVE_PROCESS, durable = "true"),
            exchange = @Exchange(value = EXCHANGE_PRM_PROCESS, type = ExchangeTypes.TOPIC),
            key = PRM_PROCESS_APPROVE + "_" + SYSTEM_KEY))
    public void processApproveListener(String message) throws Exception {
        
    }

    /**
     * 流程结束
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QUEUE_BOL_END_PROCESS, durable = "true"),
            exchange = @Exchange(value = EXCHANGE_PRM_PROCESS, type = ExchangeTypes.TOPIC),
            key = PRM_PROCESS_END + "_" + SYSTEM_KEY))
    public void processEndListener(String message) throws Exception {
        
    }

}
皖ICP备2024056275号-1
发表了78篇文章 · 总计149.56k字
本站已稳定运行