定义一个创建对象的接口,让其子类决定实例化哪个工厂类,工厂模式使其创建过程延迟到子类进行
工厂模式
使用场景
- 数据库访问,当用户不知道系统采用哪种数据库时,以及数据库可能发生变化时
- 需要根据不同条件创建不同的对象时
优点
- 一个调用者想创建一个对象,只要知道其名称就可以了。
- 扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。
- 屏蔽产品的具体实现,调用者只关心产品的接口。
缺点
- 每次增加实现都需要增加具体实现类,耦合性和复杂性会升高。
- 简单对象可以通过new实现,无须使用工厂模式,引入工厂会增加复杂度
实现
对象接口
| 12
 3
 4
 5
 6
 7
 8
 
 | public interface Office {
 
 
 
 void bootstrap();
 
 }
 
 | 
实现类
| 12
 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
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 
 | public class Word implements Office {@Override
 public void bootstrap() {
 System.out.println("Word 启动 <<<<<<<<<<<<");
 try{
 Thread.sleep(1000);
 }catch (Exception e){
 System.out.println("Word 启动失败!!\n\n");
 e.printStackTrace();
 }
 }
 }
 
 
 public class Excel implements Office {
 @Override
 public void bootstrap() {
 System.out.println("Excel 启动 <<<<<<<<<<<<");
 try{
 System.out.println("加载系统配置...");
 Thread.sleep(500);
 System.out.println("加载用户变量...");
 Thread.sleep(500);
 }catch (Exception e){
 System.out.println("Excel 启动失败!!\n\n");
 e.printStackTrace();
 }
 
 System.out.println("Excel 启动完成 <<<<<<<<<<<<");
 }
 }
 
 
 public class PowerPoint implements Office {
 @Override
 public void bootstrap() {
 System.out.println("PowerPoint 启动 <<<<<<<<<<<<");
 try{
 System.out.println("图形化界面初始化...");
 Thread.sleep(300);
 System.out.println("渲染进行中...");
 Thread.sleep(700);
 }catch (Exception e){
 System.out.println("PowerPoint 启动失败!!\n\n");
 e.printStackTrace();
 }
 System.out.println("PowerPoint 启动成功 <<<<<<<<<<<<");
 }
 }
 
 | 
增加枚举类型
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | package cn.leithda.factory;
 
 
 
 
 
 
 
 public enum OfficeType {
 WORD,
 EXCEL,
 PPT
 }
 
 | 
增加工厂类
| 12
 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
 29
 30
 31
 32
 
 | package cn.leithda.factory;
 
 
 
 
 
 
 
 public class OfficeFactory {
 
 
 
 
 
 
 public static Office getOffice(OfficeType type) {
 if (type == null) {
 return null;
 }
 switch (type) {
 case WORD:
 return new Word();
 case EXCEL:
 return new Excel();
 case PPT:
 return new PowerPoint();
 default:
 throw new RuntimeException("未知参数,无法创建对应实例");
 }
 }
 }
 
 | 
测试
| 12
 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
 29
 30
 31
 32
 33
 34
 
 | package cn.leithda.factory;
 import org.junit.Test;
 
 
 
 
 
 
 
 
 public class OfficeFactoryTest {
 
 @Test
 public void test(){
 Office word = OfficeFactory.getOffice(OfficeType.WORD);
 Office excel = OfficeFactory.getOffice(OfficeType.EXCEL);
 Office powerPoint = OfficeFactory.getOffice(OfficeType.PPT);
 word.bootstrap();
 excel.bootstrap();
 powerPoint.bootstrap();
 }
 }
 
 
 
 
 
 
 
 
 
 
 
 
 | 
Usage