设计模式一抽象工厂模式

欢迎光临我的博客[http://poetize.cn],前端使用VUE2,聊天室使用VUE3,后台使用Spring Boot




概述

  通过工厂创建的对象,彼此之间是有关联的,是需要相互匹配的,
  而简单工厂并没有解决对象之间相互依赖匹配的问题

  合理解决方案就是使用抽象工厂设计模式:提供一个创建一系列相关或者相互依赖的接口
  
接口内部严格按照相关或相互依赖的对象关系创建,
  切勿堆积其它没有用的方法或者无关联关系的方法

组成

AbstractFactory:抽象工厂,提供统一的接口工厂。

ConcreteFactory:具体工厂,用来创建具体的带有关联关系的实例产品

AbstractProduct:统一的产品接口

ConcreteProduct:具体产品

优点:解耦

缺点:不容易扩展

 

  1 public class Client {
  2     public static void main(String[] args) {
  3         ConcreteFactory1 a = new ConcreteFactory1();
  4         CPUApi cpu = a.createCpu();
  5         MainBoardApi mainBoard = a.createMainBoard();
  6         cpu.calculate();
  7         mainBoard.installCpu();
  8 
  9     }
 10 }
 11 
 12 
 13 //CPU的模拟方法
 14 public interface CPUApi {
 15     public void calculate();
 16 }
 17 
 18 //主板模拟方法
 19 public interface MainBoardApi {
 20     public void installCpu();
 21 }
 22 
 23 //cpu实现
 24 public class IntelCpu implements CPUApi {
 25     private int pins = 0;
 26 
 27     public IntelCpu(int pins) {
 28         // TODO Auto-generated constructor stub
 29         this.pins = pins;
 30     }
 31 
 32     @Override
 33     public void calculate() {
 34         // TODO Auto-generated method stub
 35         System.out.println("INTEL CPU 针脚数:" + pins);
 36     }
 37 }
 38 
 39 public class AMDCpu implements CPUApi {
 40     private int pins = 0;
 41 
 42     public AMDCpu(int pins) {
 43         // TODO Auto-generated constructor stub
 44         this.pins = pins;
 45     }
 46 
 47     @Override
 48     public void calculate() {
 49         // TODO Auto-generated method stub
 50         System.out.println("AMD CPU针脚数:" + pins);
 51     }
 52 }
 53 
 54 //主板实现
 55 public class GAMainBoard implements MainBoardApi {
 56     private int cpuHoles = 0;
 57 
 58     public GAMainBoard(int cpuHoles) {
 59         // TODO Auto-generated constructor stub
 60         this.cpuHoles = cpuHoles;
 61     }
 62 
 63     @Override
 64     public void installCpu() {
 65         // TODO Auto-generated method stub
 66         System.out.println("GA MainBoard 插孔数:" + cpuHoles);
 67     }
 68 
 69 }
 70 
 71 public class MSIMainBoard implements MainBoardApi {
 72     private int cpuHoles = 0;
 73 
 74     public MSIMainBoard(int cpuHoles) {
 75         // TODO Auto-generated constructor stub
 76         this.cpuHoles = cpuHoles;
 77     }
 78 
 79     @Override
 80     public void installCpu() {
 81         // TODO Auto-generated method stub
 82         System.out.println("MSI MainBord 插孔数:" + cpuHoles);
 83     }
 84 
 85 }
 86 
 87 //抽象工厂
 88 public interface AbstractFactory {
 89     public CPUApi createCpu();
 90 
 91     public MainBoardApi createMainBoard();
 92 }
 93 
 94 //抽象工厂具体实现
 95 public class ConcreteFactory1 implements AbstractFactory {
 96 
 97     //两者之间有联系
 98     @Override
 99     public CPUApi createCpu() {
100         // TODO Auto-generated method stub
101         return new AMDCpu(1156);
102     }
103 
104     @Override
105     public MainBoardApi createMainBoard() {
106         // TODO Auto-generated method stub
107         return new MSIMainBoard(1156);
108     }
109 }
110 
111 public class ConcreteFactory2 implements AbstractFactory {
112 
113     //两者之间有联系
114     @Override
115     public CPUApi createCpu() {
116         // TODO Auto-generated method stub
117         return new AMDCpu(939);
118     }
119 
120     @Override
121     public MainBoardApi createMainBoard() {
122         // TODO Auto-generated method stub
123         return new MSIMainBoard(939);
124     }
125 
126 }
posted @ 2019-07-30 22:13  LittleDonkey  阅读(164)  评论(0编辑  收藏  举报