生活随笔
收集整理的這篇文章主要介紹了
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简单实现打印机功能,详细思路分析 小白上手的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。