Java啤酒生产系统描述_Java描述设计模式(03):工厂方法模式

一、工厂方法模式

1、生活场景

系统常见的数据导出功能:数据导出PDF、WORD等常见格式。

2、工厂方法模式

是类的创建模式,又叫做虚拟构造子(Virtual Constructor)模式或者多态性工厂(Polymorphic Factory)模式。工厂方法模式的用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类中。

3、核心角色

1)、抽象工厂角色

这个角色的是工厂方法模式的核心,任何在模式中创建对象的工厂类必须实现这个接口。在实际的系统中,这个角色也常常使用抽象类实现。

2)、具体工厂角色

担任这个角色的是实现了抽象工厂接口的具体JAVA类。具体工厂角色含有与业务密切相关的逻辑,并且受到使用者的调用以创建导出类。

3)、抽象导出角色

工厂方法模式所创建的对象的超类,也就是所有导出类的共同父类或共同拥有的接口。在实际的系统中,这个角色也常常使用抽象类实现。

4)、具体导出角色

这个角色实现了抽象导出角色所声明的接口,工厂方法模式所创建的每一个对象都是某个具体导出角色的实例。

4、代码UML关系图

20063662ecb67a58ca7337c6c63aeed6.png

5、源代码实现

// 客户端角色

public class C01_FactoryMethod {

public static void main(String[] args) {

String data = "" ;

ExportFactory factory = new ExportWordFactory () ;

ExportFile exportWord = factory.factory("user-word") ;

exportWord.export(data) ;

factory = new ExportPdfFactory() ;

ExportFile exportPdf =factory.factory("log-pdf") ;

exportPdf.export(data) ;

}

}

// 抽象工厂角色

interface ExportFactory {

ExportFile factory (String type) ;

}

// 具体工厂角色

class ExportWordFactory implements ExportFactory {

@Override

public ExportFile factory(String type) {

if ("user-word".equals(type)){

return new ExportUserWordFile() ;

} else if ("log-word".equals(type)){

return new ExportLogWordFile() ;

} else {

throw new RuntimeException("没有找到对象") ;

}

}

}

class ExportPdfFactory implements ExportFactory {

@Override

public ExportFile factory(String type) {

if ("user-pdf".equals(type)){

return new ExportUserPdfFile() ;

} else if ("log-pdf".equals(type)){

return new ExportLogPdfFile() ;

} else {

throw new RuntimeException("没有找到对象") ;

}

}

}

// 抽象导出角色

interface ExportFile {

boolean export (String data) ;

}

// 具体导出角色

class ExportUserWordFile implements ExportFile {

@Override

public boolean export(String data) {

System.out.println("导出用户Word文件");

return true;

}

}

class ExportLogWordFile implements ExportFile {

@Override

public boolean export(String data) {

System.out.println("导出日志Word文件");

return true;

}

}

class ExportUserPdfFile implements ExportFile {

@Override

public boolean export(String data) {

System.out.println("导出用户Pdf文件");

return true;

}

}

class ExportLogPdfFile implements ExportFile {

@Override

public boolean export(String data) {

System.out.println("导出日志Pdf文件");

return true;

}

}

二、Spring框架中应用

1、场景描述

基于spring框架的配置实现如下流程:汽车工厂根据不同的国家,生产不同类型的汽车。

2、核心工厂类

public class ProductCar implements CarFactory {

private Map carMap = null;

public ProductCar() {

carMap = new HashMap<>();

carMap.put("china", new CarEntity("中国", "黑色","红旗"));

carMap.put("america", new CarEntity("美国", "白色","雪佛兰"));

}

@Override

public CarEntity getCar(String type) {

return carMap.get(type);

}

}

3、核心Xml配置文件

4、测试类

1)、代码块

public class SpringTest {

@Test

public void test01 (){

ApplicationContext context01 = new ClassPathXmlApplicationContext("/spring/spring-factorymethod.xml");

CarEntity car1 = (CarEntity)context01.getBean("car1") ;

CarEntity car2 = (CarEntity)context01.getBean("car2") ;

System.out.println(car1);

System.out.println(car2);

}

}

2)、输出结果

CarEntity{country='中国', color='黑色', name='红旗'}

CarEntity{country='美国', color='白色', name='雪佛兰'}

三、工厂方法小结

工厂方法中,把创建类的动作延迟,就是通过对应的工厂来生成类的对象,这种设计方式符合“开闭”原则。缺点就是当产品的种类过多的时候,需要定义很多产品对应的工厂类。

四、源代码地址

GitHub·地址

https://github.com/cicadasmile/model-arithmetic-parent

GitEE·地址

https://gitee.com/cicadasmile/model-arithmetic-parent

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值