package entiey; public interface Ink { /** * 定义打印采用的颜色的方法 * r(红色) * g(绿色) * b(蓝色) */ public String getColor(int r,int g,int b); }
package entiey; public interface Paper { /** * 纸张接口 */ public static final String newine="\r\n"; //输入一个字符到纸张 public void putInChar(char c); //得到输出到纸张上的内容 public String getContent(); }
package printer; import entiey.Ink; import entiey.Paper; /** * 打印机程序 * @author ASUS * */ public class Printer { //面向接口编程,而不是具体的实现类 private Ink ink=null; private Paper paper=null; /** * 设置注入所需的setter方法 */ 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, 200, 0)+"颜色打印:\n"); //逐字符输出到纸张 for(int i=0;i<str.length();++i) { paper.putInChar(str.charAt(i)); } //将纸张的内容输出 System.out.print(paper.getContent()); } }
package entiey; import java.awt.Color; public class ColorInk implements Ink{ //打印采用彩色 /** * 彩色墨盒,ColorInk实现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); } }
GreyInk.java文件内容如下:
package entiey; import java.awt.Color; public class GreyInk implements Ink{ /** * 灰色墨盒,GreyInk实现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 entiey; 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 void putInChar(char c) { content+=c; ++posX; //判断是否换行 if(posX==charPerLine) { content+=Paper.newine; posX=0; ++posY; } //判断是否翻页 if(posY==linePerPage) { content+="==第"+posP+"页=="; content+=Paper.newine+Paper.newine; 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.newine; } ret+="==第"+posP+"页=="; } return ret; } //setter方法 public void setCharPerLine(int charPerLine) { this.charPerLine = charPerLine; } public void setLinePerPage(int linePerPage) { this.linePerPage = linePerPage; } }
<?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-3.2.xsd"> <!-- 定义彩色墨盒Bean,id是ColorInk --> <bean id="colorInk" class="entiey.ColorInk"/> <!-- 定义灰色墨盒Bean,id是GreyInk --> <bean id="greyInk" class="entiey.GreyInk"/> <!-- 定义A4纸张Bean,id是a4Paper --> <!-- 通过setCharPerLine()方法为charPerLine属性注入每行字符数 --> <!-- 通过setLinePerPage()方法为linePerPage属性注入每页行数 --> <bean id="a4Paper" class="entiey.TextPaper"> <property name="charPerLine" value="10"/> <property name="linePerPage" value="8"/> </bean> <!-- 定义B5纸张Bean,id是b5Paper --> <bean id="b5Paper" class="entiey.TextPaper"> <property name="charPerLine" value="6"/> <property name="linePerPage" value="5"/> </bean> <!-- 组装打印机,定义打印机Bean该Bean的id是printer.class指定该Bean实例的实现类 --> <bean id="printer" class="printer.Printer"> <!-- 通过ref属性注入已经定义好的Bean --> <!-- 注入彩色墨盒 --> <property name="ink" ref="colorInk"/> <!-- 注入灰色墨盒 --> <property name="paper" ref="b5Paper"/> </bean> </beans>
package test;
/**
* 测试打印机
* @author ASUS
*
*/
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import printer.Printer;
public class testa {
@Test
public void m1() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//通过Printer bean的id来获取Printer实例
Printer printer=(Printer) context.getBean("printer");
String content="欸 朋友们好啊" +
"我是浑元形意太极门掌门人马保国" +
"刚才有个朋友问我" +
"马老师发生什么事啦" +
"我说怎么回事" +
"给我发了几张截图"
+"我一看" +
"哦" +
"原来是昨天 "+
"有两个年轻人" +
"三十多岁" +
"一个体重九十多公斤" +
"一个体重八十多公斤" +
"他们说" +
"欸" +
"有一个说是" +
"我在健身房练功" +
"颈椎练坏了" +
"马老师你能不能教教我浑元功法" +
"欸" +
"帮助治疗一下我的颈椎病" +
"我说可以" +
"我说你在健身房练死劲儿不好用" +
"他不服气" +
"欸" +
"我说小朋友" +
"你两个手来折我一个手指头" +
"他折不动" +
"他说你这也没用" +
"我说我这有用" +
"这是化劲儿" +
"传统功夫是讲化劲儿的" +
"四两拨千斤" +
"二百多斤的英国大力士" +
"都握不动我这一个手指" +
"他说要和我试试" +
"我说可以" +
"欸" +
"我一说他啪一下就站起来了" +
"很快啊" +
"然后上来就是" +
"一个左正蹬" +
"一个右鞭腿" +
"一个左刺拳" +
"我全部防出去了啊" +
"防出去以后自然是" +
"传统功夫以点到为" +
"右拳放在他鼻子上" +
"没打他" +
"我笑一下" +
"准备收拳" +
"因为这时间" +
"按照传统功夫的点到为止" +
"他就输了" +
"如果我这一拳发力" +
"一拳就把他鼻子打骨" +
"放在他鼻子上没用打他" +
"他也承认" +
"我先打到他面部" +
"他不知道拳放在他鼻子上" +
"他承认我先打到他面部啊" +
"我收拳的时间不打了" +
"他突然袭击左刺拳来打我脸" +
"啊" +
"我大意了啊" +
"没有闪" +
"欸" +
"他的左拳给我眼" +
"给我右眼蹭了一下" +
"但没关系啊" +
"他也说" +
"他结束也说了" +
"两分多钟以后" +
"当时流眼泪了捂着眼" +
"我说停停" +
"然后两分钟以后" +
"两分多钟以后" +
"就好了" +
"我说小伙子你不讲武德你不懂" +
"他说马老师对不起对不起" +
"我不懂规矩" +
"啊" +
"我是" +
"他说他是乱打的" +
"他可不是乱打的啊" +
"正蹬" +
"鞭腿" +
"左刺拳" +
"训练有素" +
"后来他说他练过三四年泰拳啊" +
"看来是有备而来" +
"这两个年轻人" +
"不讲武德" +
"来" +
"骗" +
"来" +
"偷袭" +
"我69岁" +
"老同志" +
"这好吗" +
"这不好" +
"我劝" +
"这位" +
"年轻人" +
"好自为之" +
"好好反思" +
"不要再犯这样的聪明" +
"小聪明" +
"啊" +
"额" +
"武林要以和为贵" +
"要讲武德" +
"不要搞" +
"窝里斗" +
"谢谢朋友们";
printer.print(content);
}
}
原文:https://www.cnblogs.com/beiweihaohao/p/14083909.html