生活随笔
收集整理的这篇文章主要介绍了
spring简单实现打印机功能,详细思路分析 小白上手
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
标题
- 思路分析
- 接口分析
- 接口实现类
- 组装打印机 Printer
- spring 配置文件
- 测试类 PersonTest
- 小型Demo结构图
- 效果图
思路分析
总思路:接口 接口实现类 组装打印机 接口就是提供方法的 你需要什么我给你提供什么 实现类具体实现接口方法
打印机需要什么? 需要墨盒色彩的选择吧 ,黑白的 、彩色的 需要纸张大小吧 你要A4纸张还是B5纸张
因此得出结论需要确定墨盒颜色、纸张大小:如何实现 墨盒、纸张呢 通过接口定义方法 再由他们的实现类去实现各自的方法吧
我打印机就像是经理 我不需要真正的干活 我只要把那两手下弄好的拿过来 也叫组装打印机 然后启动我独有的打印功能 打印出来就好了 。 把打印机在注入spring配置文件 ApplicationContext 读取出来 通过getBean()获取到组装打印机 调用其打印方法即可。
接口分析
墨盒接口 Ink 他需要实现什么功能呢 定义打印采用的颜色的方法 写法如下
public String getColor(int r ,int g ,int b);
墨盒实现类 因为前面说了 要彩色墨盒 黑白墨盒 所以两个都要实现各自打印需要的颜色
所以都是 定义颜色 引入Color 返回颜色 记得加# 因为颜色是 #0033fA这种形式的
即 return “#”+Integer.toHexString(color.getRGB()).substring(2);
其中 tohexstring()就是16进制显示 说白了上面就是:将RGB颜色值转换为十六进制
ColorInk 彩色墨盒实现类
public class ColorInk implements Ink{public String
getColor(int r
, int g
, int b
) {Color color
= new Color(r
, g
, b
);return "#"+Integer
.toHexString(color
.getRGB()).substring(2);}
}
GreyInk灰色墨盒实现类
public class GreyInk implements Ink {public String
getColor(int r
, int g
, int b
) {int c
= r
+g
+b
;Color color
= new Color(c
);return "#"+Integer
.toHexString(color
.getRGB()).substring(2);}
}
接口实现类
纸张接口 Paper 接口要写什么方法、 属性啊 取决于你将来可能需要什么方法 、属性 纸能干嘛 ?
先看需要什么属性 我需要换行处理吧 (windows:\r\n(换行) 定义newline 到时候其他地方的调用 所以的静态
纸肯定是来接收文字的 这里叫char 字符 对吧 肯定是一个一个字符接收得吧! 最后汇总得到内容对吧
应该提供什么方法 1.输出一个字符到纸张 2.得到输出到纸张上的内容
public interface Paper {public static final String newLine
= "\r\n";
public void InPutChar(char c
);public String
getContent();
}
Paper实现类 精辟也算是难点
我纸张不仅要实现上面的方法 我还有换行、翻页功能实现处理
需要的属性特征 一行显示多少条文字,第几页了 一页要显示几行数据 当前光标横向位置X 当前所处行数位置Y 接收内容的容器吧 刚把把接收到的结果返回给Paper的getContent()方法
需要判断 是否换行 是否翻页
什么时候换行 ? 是不是我当前光标横向位置 == 一行显示多少条文字 就换啊
怎么换 调用我们Paper接口我们定义的换行属性对不对。换了光标是不是的从最开始位置 归零处理 对吧
**那什么时候翻页呢?**是不是当前所处行数位置Y 等于 一页要显示几行数据 顺道打印一下当前页
当前所处行数位置Y 是不是的做归零处理 页数是不是的加加处理
get set RowWordNu RowLineNu 可在配置文件中实现定义纸张
下面是我当时写的一个思路 然后再代码中实现 可能前后定义的参数不一样 思路都一样
实现一个个打印字符的方法
纸张内容这个参数来接收每一个字符 content
+=传进来的字符 进来一个先加加
++RowX
紧接着判断是否换行处理 RowX
== LineWordNu 换 怎么换 调那个我们定义在Paper的静态换行参数
content
+= Paper
.newLine; 换了行 立马横向位置归零处理并且当前行数
++ RowX
=0 ++LineY
上面换行弄好了 考虑什么时候翻页处理 当前行数
== 页行数 LineY
== LineRowNu
翻页的同时我们需要下面打印一下当前页数 content
+="第几"+NoPage
+"页==";
然后空开一点 content
+= Paper
.newLine
+ Paper
.newLine
当前行数归零处理 LineY
= 0; 页数在
++ ++NoPage
其中 LineWordNu LineRowNu 需要
get set 经过配置文件就变得灵活 可改
同时 注意 getContent方法中 把我们的内容
return给它 千万别忘记
public class PaperModel implements Paper {private Integer PageNo
= 1;private Integer RowWordNu
=10 ;private Integer RowLineNu
= 6;private Integer RowX
= 0;private Integer RowY
= 0;private String content
= "";public void InPutChar(char c
) {content
+= c
;++ RowX
;
if (RowX
== RowWordNu
){content
+= Paper
.newLine
;RowX
= 0;++RowY
;}
if (RowY
== RowLineNu
){content
+= Paper
.newLine
;content
+= "===当前是第几==="+PageNo
+"===页===";content
+= Paper
.newLine
+Paper
.newLine
;RowY
= 0;++ PageNo
;}}public String
getContent() {return content
;}public Integer
getRowWordNu() {return RowWordNu
;}public void setRowWordNu(Integer rowWordNu
) {RowWordNu
= rowWordNu
;}public Integer
getRowLineNu() {return RowLineNu
;}public void setRowLineNu(Integer rowLineNu
) {RowLineNu
= rowLineNu
;}
}
小弟们终于把苦活累活干完了 该总经理出马了
组装打印机 Printer
如何组装 我是不是把他两的接口拿过来 get set 到时候就可以在spring配置文件中定义了
除此之外 你毕竟是个总经理 多少的干点活吧 打印机 顾名思义 你要告诉客户你打的什么颜色得吧
实现打印方法 然后人家把内容给你 你是不是的一个一个字符遍历啊 完事了 把内容输出展示啊
package com
.kgc
.printerDemo
.printer
;
import com
.kgc
.printerDemo
.mohe
.Ink
;
import com
.kgc
.printerDemo
.paper
.Paper
;
public class PrinterInkAndPaper {private Ink ink
;private Paper paper
;public void prints(String content
){System
.out
.println("你正在使用"+ink
.getColor(225,200,0)+"颜色的墨打印\n");for (int i
= 0; i
<content
.length() ; i
++) {paper
.InPutChar(content
.charAt(i
));}System
.out
.println(paper
.getContent());}public Ink
getInk() {return ink
;}public void setInk(Ink ink
) {this.ink
= ink
;}public Paper
getPaper() {return paper
;}public void setPaper(Paper paper
) {this.paper
= paper
;}
}
spring 配置文件
现在去spring配置文件中
1.把彩色墨盒 灰色墨盒 的实现类 弄进去
2.定义纸张 定义两种纸张 我们前面 get set 行字数 页行数 就为了这里实现自定义 智能化
3.组装打印机 里面可以选着上面的墨盒类型 以及纸张类型 所以是ref来取的 不是设置value
<?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
:aop
="http://www.springframework.org/schema/aop"xmlns
:p
="http://www.springframework.org/schema/p"xsi
:schemaLocation
="http
://www
.springframework
.org
/schema
/beanshttp
://www
.springframework
.org
/schema
/beans
/spring
-beans
-3.2.xsdhttp
://www
.springframework
.org
/schema
/aophttp
://www
.springframework
.org
/schema
/aop
/spring
-aop
-3.2.xsd"
><!--服务于printerDemo start
--><bean id
="Grey" class="com.kgc.printerDemo.mohe.GreyInk"/><bean id
="Color" class="com.kgc.printerDemo.mohe.ColorInk"/><bean id
="A4" class="com.kgc.printerDemo.paper.PaperModel"><property name
="rowWordNu" value
="14"/><property name
="rowLineNu" value
="4"/></bean
><bean id
="B5" class="com.kgc.printerDemo.paper.PaperModel"><property name
="rowWordNu" value
="15"/><property name
="rowLineNu" value
="5"/></bean
><bean id
="printer" class="com.kgc.printerDemo.printer.PrinterInkAndPaper"><property name
="paper" ref
="A4"/><property name
="ink" ref
="Color"/></bean
><!--服务于printerDemo end
-->
</beans
>
测试类 PersonTest
加载配置文件 通过getBean 把组装的打印机id给他
在定义个内容 调用 打印机的打印方法 他需要传入内容参数 把上面定义的放进去 测试成功
import com
.kgc
.printerDemo
.printer
.PrinterInkAndPaper
;
import org
.junit
.Test
;
import org
.springframework
.context
.ApplicationContext
;
import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
public class PersonTest {@Testpublic void test(){ApplicationContext context
= new ClassPathXmlApplicationContext("applicationContext.xml");
PrinterInkAndPaper printer
= (PrinterInkAndPaper
)context
.getBean("printer");String conent
="几位轻量级容器的作者曾骄傲地对我说:这些容器非常有"+ "用,因为它们实现了“控制反转”。这样的说辞让我深感迷惑:控"+ "用,因为它们实现了“控制反转”。这样的说辞让我深感迷惑:控"+ "用,因为它们实现了“控制反转”。这样的说辞让我深感迷惑:控"+ "用,因为它们实现了“控制反转”。这样的说辞让我深感迷惑:控"+ "用,因为它们实现了“控制反转”。这样的说辞让我深感迷惑:控"+ "用,因为它们实现了“控制反转”。这样的说辞让我深感迷惑:控"+ "制反转是框架所共有的特征,如果仅仅因为使用了控制反转就认为"+ "这些轻量级容器与众不同,就好像在说“我的轿车是与众不同的," + "因为它有4个轮子。”";printer
.prints(conent
);}
}
小型Demo结构图
效果图
总结
以上是生活随笔为你收集整理的spring简单实现打印机功能,详细思路分析 小白上手的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。