Spring 打印机
墨盒接口与实现
package cn.bean;
/**
* 墨盒接口
* */
public interface Ink {
/**
* 定义打印采用的颜色的方法。
* @param r 红色值
* @param g 绿色值
* @param b 蓝色值
* @return 返回
*/
public String getColor(int r,int g,int b);
}
package cn.bean.impl;
import java.awt.Color;
import cn.bean.Ink;
/**
* 彩色墨盒。ColorInk 实现 Ink 接口
* */
public class ColorInk implements Ink {
//打印采用彩色
@Override
public String getColor(int r, int g, int b) {
Color color = new Color(r, g, b);
return "#"+Integer.toHexString(color.getRGB()).substring(2);
}
}
package cn.bean.impl;
import java.awt.Color;
import cn.bean.Ink;
/**
* 灰色墨盒。GreyInk 实现 Ink 接口
*/
public class GreyInk implements Ink {
//打印采用灰色
@Override
public String getColor(int r, int g, int b) {
int c = (r+g+b)/3;
Color color = new Color(c, c, c);
return "#"+Integer.toHexString(color.getRGB()).substring(2);
}
}
纸张接口与实现
package cn.bean;
/**
* 纸张接口
* */
public interface Paper {
public static final String NEW_LIKE = "\r\n";
/**
* 输出一个字符得到纸张
* @param c
*/
public void putInChar(char c);
/**
* 得到输出到纸张上的内容
* @return
*/
public String getContent();
}
package cn.bean.impl;
import cn.bean.Paper;
/**
* 文本打印纸张实现。TextPaper 实现 Paper 接口
* */
public class TextPaper implements Paper {
//每行字符数
private int charPerLine = 16;
//每页行数
private int linePerPage = 5;
//纸张中内容
private String content = "";
//当前横向位置,从 0 到 charPerLine-1
private int posX = 0;
//当前行数,从 0 到 linePerPage-1
private int posY = 0;
//当前页数
private int posP = 1;
@Override
public String getContent() {
String ret = this.content;
//补齐本页空行,并显示页码
if(!(posX == 0 && posY==0)){
int color = linePerPage - posY;
for(int i=0;i<color;++i){
ret += Paper.NEW_LIKE;
}
ret += "==第"+posP+"页==";
}
return ret;
}
@Override
public void putInChar(char c) {
content +=c;
++posY;
//判断是否换行
if(posX == charPerLine){
content += Paper.NEW_LIKE;
posX = 0;
++posY;
}
//判断是否翻页
if(posY ==linePerPage){
content +="==第"+posP+"页==";
content += Paper.NEW_LIKE+Paper.NEW_LIKE;
posY=0;
++posP;
}
}
//setter 方法,用于属性注入
public void setCharPerLine(int charPerLine) {
this.charPerLine = charPerLine;
}
//setter 方法,用于属性注入
public void setLinePerPage(int linePerPage) {
this.linePerPage = linePerPage;
}
}
打印机
package cn.bean;
/**
* 打印机
*/
public class Printer {
//面向接口编程,而不是具体的实现类
private Ink ink = null;
//面向接口编程,而不是具体的实现类
private Paper paper = null;
/**
* 设值注入所需的 setter 方法
* @param ink 传入墨盒参数
*/
public void setInk(Ink ink) {
this.ink = ink;
}
/**
* 设值注入所需的 setter 方法
* @param paper 传入纸张参数
*/
public void setPaper(Paper paper) {
this.paper = paper;
}
/**
* 打印机打印方法
* @param str 传入打印内容
*/
public void print(String str){
//输出颜色标记
System.out.println("使用"+ink.getColor(255, 200, 0)+"颜色打印:\n");
//逐字符输出到纸张
for(int i=0; i < str.length();++i){
paper.putInChar(str.charAt(i));
}
//将纸张的内容输出
System.out.println(paper.getContent());
}
}
applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="colorInk" class="cn.bean.impl.ColorInk">
<description>彩色墨盒</description>
</bean>
<bean id="greyInk" class="cn.bean.impl.GreyInk">
<description>黑白墨盒</description>
</bean>
<bean id="a3" class="cn.bean.impl.TextPaper">
<description>纸张</description>
<property name="charPerLine" value="6"></property>
<property name="linePerPage" value="4"></property>
</bean>
<bean id="b4" class="cn.bean.impl.TextPaper">
<description>纸张</description>
<property name="charPerLine" value="5"></property>
<property name="linePerPage" value="3"></property>
</bean>
<bean id="printer1" class="cn.bean.Printer">
<description>打印机</description>
<property name="ink" ref="colorInk"></property>
<property name="paper" ref="a3"></property>
</bean>
<bean id="printer2" class="cn.bean.Printer">
<description>打印机</description>
<property name="ink" ref="greyInk"></property>
<property name="paper" ref="b4"></property>
</bean>
</beans>
测试打印机
package cn.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.bean.Printer;
public class TestPrinter {
public static void main(String[] args) {
//打开 spring 上下文,打开容器,参数是配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中取出 JavaBean
Printer printer1 = (Printer) context.getBean("printer1");
printer1.print("补齐本页空行,并显示页码补齐空行,并显示页码补齐本页空行,并显示页码补齐本页空行");
System.out.println();
Printer printer2 = (Printer) context.getBean("printer2");
printer2.print("补齐本页空行,并显示页码补齐空行,并显示页码补齐本页空行,并显示页码补齐本页空行");
}
}
效果图: