三、用Spring改造打印机
问题描述:打印机是由墨盒和纸张确定的,墨盒中又有不同的颜色,纸张中又有不同的纸张类型,打印机通过墨盒的颜色来确定要打印的颜色,根据纸张的类型来确定打印纸张的类型。
问题分析:由于墨盒颜色和纸张类型的不同,我们可以定义相关的接口及实现类来实现。
- 墨盒接口:定义一个getInk()方法,并定义其不同颜色的实现类;
- 纸张接口:定义putInChar()方法和getContent()方法,并定义其不同类型纸张的实现类。
代码演示:
接口
package cn.printer.face;
/**
* 墨盒接口
*/
public interface Ink {
/**
* 获取颜色的方法
* @param r
* @param g
* @param b
* @return
*/
public String getInk(int r,int g,int b);
}
package cn.printer.face;
/**
* 纸张接口
*/
public interface Paper {
/**
* 换行常量
*/
public static final String newLine = "\n\t";
/**
* 输出字符到纸张
* @param c
*/
public void putInChar(char c);
/**
* 得到输出纸张上的内容
* @return
*/
public String getContent();
}
接口的实现类
package cn.printer.iface;
import cn.printer.face.Ink;
import java.awt.*;
/**
* 黑白墨盒
*/
public class GreyInk implements Ink {
@Override
public String getInk(int r, int g, int b) {
int rgb = (r + g + b)/3;
Color color = new Color(rgb,rgb,rgb);
return "#" + Integer.toHexString(color.getRGB()).substring(2);
}
}
package cn.printer.iface;
import cn.printer.face.Ink;
import java.awt.*;
/**
* 彩色墨盒
*/
public class ColorInk implements Ink {
@Override
public String getInk(int r, int g, int b) {
Color color = new Color(r,g,b);
return "#" + Integer.toHexString(color.getRGB()).substring(2);
}
}
package cn.printer.iface;
import cn.printer.face.Paper;
/**
* 纸张类型的实现类
*/
public class TextPaper implements Paper {
private int charPerLine = 16;//每行字符数
private int linePerpage = 5;//每页行数
private String content = "";//纸张中的内容
private int posX = 0;//当前横向位置,从0到charPerLine-1
private int posY = 1;//当前行数,从0到linePerPage-1
private int posP = 1;//当前页数
/**
* setter方法,用于属性注入
* @param charPerLine
*/
public void setCharPerLine(int charPerLine) {
this.charPerLine = charPerLine;
}
public void setLinePerpage(int linePerpage) {
this.linePerpage = linePerpage;
}
/**
* 输出字符到纸张
* @param c
*/
@Override
public void putInChar(char c) {
//拼接字符
content += c;
//此行字数加一
posX++;
//判断是否换行
if (posX==charPerLine){
content += Paper.newLine;
posX = 0;
posY++;
}
//判断是否翻页
if (posY==linePerpage){
content += "===第" + posP + "页===";
content +=Paper.newLine + Paper.newLine + Paper.newLine;
posY = 0;
posP++;
}
}
/**
* 得到输出纸张上的内容
* @return
*/
@Override
public String getContent() {
String get_content = this.content;
//补齐本页空行,并显示页码
if (!(posY == 0 && posX == 0)){
int count = linePerpage - posY;
for (int i = 0;i < count;i++){
get_content += Paper.newLine;
}
get_content += "==第" + posP + "页===";
}
return get_content;
}
}
打印机类
package cn.printer.pojo;
import cn.printer.face.Ink;
import cn.printer.face.Paper;
/**
* 打印机类
*/
public class Printer {
//定义接口
public Ink ink = null;
public Paper paper = null;
/**
* 打印的方法
* @param str
*/
public void print(String str){
System.out.println("正在打印"+ink.getInk(255,255,255)+"颜色的纸张");
for (int i = 0;i < str.length();i++){
paper.putInChar(str.charAt(i));
}
//输出纸张内容
System.out.println(paper.getContent());
}
public void setInk(Ink ink) {
this.ink = ink;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
}
applicationContent.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--生产墨盒-->
<bean id="GreyInk" class="cn.printer.iface.GreyInk"/>
<bean id="ColorInk" class="cn.printer.iface.ColorInk"/>
<!--依赖注入-->
<!--生产A4纸张-->
<bean id="A4Paper" class="cn.printer.iface.TextPaper">
<property name="charPerLine" value="10"/>
<property name="linePerpage" value="5"/>
</bean>
<!--生产B5纸张-->
<bean id="B5Paper" class="cn.printer.iface.TextPaper">
<property name="charPerLine" value="6"/>
<property name="linePerpage" value="4"/>
</bean>
<!--开始打印:不同颜色的墨盒和不同类型的纸张-->
<bean id="printer" class="cn.printer.pojo.Printer">
<property name="ink" ref="ColorInk"/>
<property name="paper" ref="A4Paper"/>
</bean>
</beans>
测试类
package cn.printer.test;
import cn.printer.pojo.Printer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("src/cn/printer/applicationContext.xml");
Printer printer = (Printer) applicationContext.getBean("printer");
//随便输入一段文字
String str = "山地车不愉快就比后级别将会吧不可,击败开,不开机" +
" 集合把是就和V记笔记包铝,板护具哭保驾护航不不不不或或是处反,观哈嘎嘎嘎个个发" +
" 成VG和成功闭集合不有有,该方法该会话个放顶顶,顶个环境京,沪高铁VB那句,易方达好吧" +
"你觉得饱餐一顿努埃尔文IC别,人湖北吃饭吧阶段开始及逆行,流程部也不懂" +
"看基本都是进出口预测各发发图" +
"课程表时代峻峰,文本更残酷有多个成功IE无大红包我也爱过㝉户,路径和. ";
printer.print(str);
}
}