Spring-基于IOC/DI实现的打印机系统
1.深入理解IOC/DI
打印机的案例可以充分的表示IOC控制反转的概念
IOC/DI的深入理解
案例:打印机的案例
- 可以模拟彩色墨盒
- 可以模拟纸张的类型
- 打印机依赖于墨盒和不同的纸,采用的就是依赖注入的方式
Ink的实现类
- ColorInk
- GreyInk
Paper的实现类
- TextPaper
Printer
- Ink
- Paper
2.4.1Ink接口
1.ink接口代码
//墨盒
public interface Ink {
//获取颜色的值,返回的值是一个#xxxxx的16进制形式的值,需要在实现类中进行处理
public String getColor(int r,int g, int b);
}
实现类
2.4.2Paper纸张
1.接口代码
//纸张
public interface Paper {
//定义回车换行
public static final String newline="\r\n";
// 输出一个字符到纸上(需要根据文字的多少实现换行)
public void putChar(String c);
// 得到纸上输出的内容
public String getContent();
}
2.4.3使用ink和paper接口开发Printer程序
- 目前采用的IOC的方式进行打印数据
- 采用的是面向接口的方法
//打印机
public class Printer {
//墨盒
private Ink ink;
//纸张
private Paper paper;
//设置传入的墨盒参数
public void setInk(Ink ink) {
this.ink = ink;
}
//设置传入的纸张的数据
public void setPaper(Paper paper) {
this.paper = paper;
}
//打印数据
public void print(String str){
//输出颜色标记
System.out.println("使用"+ink.getColor(255,255,0)+"颜色打印:"+"\n");
//逐字符进行打印
for (int i = 0; i < str.length(); i++) {
paper.putChar(str.charAt(i));
}
//将纸张的内容输出
System.out.println(paper.getContent());
}
public Ink getInk() {
return ink;
}
public Paper getPaper() {
return paper;
}
}
2.4.4 ink和paper的接口实现
1.ink的实现类
//黑色墨盒
//打印采用的是灰色的打印的方式
public class BlackInk implements Ink {
@Override
public String getColor(int r, int g, int b) {
int x=(r+g+b)/3;
Color color=new Color(x,x,x);
String c="#"+Integer.toHexString(color.getRGB()).substring(2);
return c;
}
}
//彩色墨盒
public class ColorInk implements Ink {
@Override
public String getColor(int r, int g, int b) {
Color color=new Color(r,g,b);
String c="#"+Integer.toHexString(color.getRGB()).substring(2);
return c;
}
}
2.paper的实现类
//纸张
public class TextPaper implements Paper {
//每行的字符数
private int charPerLine=16;
//每页行数
private int linePerPage=5;
//纸张的内容
private String content="";
//当前的横向位置,从0到charPerLine-1
private int posX=0;
//当前的页数
private int posY=1;
private int posP=1;
//输出字符
@Override
public void putChar(char c) {
content+=c;
++posX;
//判断是否换行
if (posX==charPerLine){
content+=Paper.newline;
posX=0;
++posY;
}
//判断是否翻页
if (posY==linePerPage){
content+="==第"+posY+"页==";
content+=Paper.newline+Paper.newline;
posY=0;
++posP;
}
}
//获取输出的内容
@Override
public String getContent() {
String ret=this.content;
//补齐本页空行,并显示页码
if (!(posX==0&&posY==0)){
int count=linePerPage-posY;
for (int i=0; i<count; ++i){
ret+=Paper.newline;
}
ret+="==第"+posP+"页==";
}
return ret;
}
public void setCharPerLine(int charPerLine) {
this.charPerLine = charPerLine;
}
public void setLinePerPage(int linePerPage) {
this.linePerPage = linePerPage;
}
}
2.4.5组装打印机
- 设置配置文件,设置对应的依赖的传递的关系就可以实现
<!-- 定义彩色墨盒-->
<bean id="colorInk" class="cn.lxz.domain.impl.ColorInk"/>
<!-- 定义灰色墨盒 -->
<bean id="blackInk" class="cn.lxz.domain.impl.BlackInk"/>
<!-- 定义A4纸张大小-->
<bean id="a4Paper" class="cn.lxz.domain.impl.TextPaper">
<property name="linePerPage" value="80"/>
<property name="charPerLine" value="10"/>
</bean>
<!-- 定义B5纸张大小-->
<bean id="b5Paper" class="cn.lxz.domain.impl.TextPaper">
<property name="charPerLine" value="6"/>
<property name="linePerPage" value="5"/>
</bean>
<!-- 组装打印机-->
<bean id="printer" class="cn.lxz.domain.Printer">
<property name="ink" ref="colorInk"/>
<property name="paper" ref="b5Paper"/>
</bean>
2.4.6测试打印机的效果
图1 打印的效果图
2.4.7完整的代码图
图2 完整的代码图