Spring學習筆記:Spring動態組裝打印機


一、如何開發一個打印機

  1.可靈活配置使用彩色魔盒或灰色魔盒

  2.可靈活配置打印頁面的大小

二、打印機功能的實現依賴於魔盒和紙張

 

三、步驟:

  1.定義墨盒和紙張的接口標准

 1 package cn.printer;
 2 /**
 3  * 墨盒接口
 4  * @author TengYiCheng
 5  *
 6  */
 7 public interface Ink {
 8     /**
 9      * 定義打印采用的顏色的方法
10      * @param r
11      * @param g
12      * @param b
13      * @return 返回打印采用的顏色
14      */
15     public String getColor(int r,int g,int b);
16 }
 1 package cn.printer;
 2 /**
 3  * 紙張接口
 4  * @author TengYiCheng
 5  *
 6  */
 7 public interface Paper {
 8     public static final String newline = "\t\n";
 9     /**
10      * 輸出一個字符到紙張
11      * @param c
12      */
13     public void putInChar(char c);
14     /**
15      * 得到輸出到紙張的內容
16      * @return
17      */
18     public String getContent();
19 }

  2.使用接口標准開發打印機

package cn.ink;

import java.awt.Color;

import cn.printer.Ink;
/**
 * 彩色墨盒。ColorInk實現Ink接口
 * @author TengYiCheng
 *
 */
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.ink;

import java.awt.Color;

import cn.printer.Ink;

/**
 * 灰色墨盒。GreyInk實現Ink接口
 * @author TengYiCheng
 *
 */
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.ink;

import cn.printer.Paper;

public class TextPaper implements Paper {
    //每行字符數
    private int charPerLine = 12;
    //每頁行數
    private int linePerPage = 10;
    //紙張中內容
    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.newline;
            posX = 0;
            posY++;
        }
        //判斷是否換頁
        if (posY==linePerPage) {
            content += "===第"+posP+"頁===";
            content += Paper.newline+Paper.newline;
            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.newline;
            }
            ret += "===第"+posP+"頁===";
        }
        return ret;
    }
    //setter方法,用於注入每行的字符數
    public void setCharPerLine(int charPerLine) {
        this.charPerLine = charPerLine;
    }
    //setter方法,用於注入每頁的行數
    public void setLinePerPage(int linePerPage) {
        this.linePerPage = linePerPage;
    }
    
}
package cn.printer;
/**
 * 打印機程序
 * @author TengYiCheng
 *
 */
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());
    }
}

  3.組裝打印機

<?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:jdbc="http://www.springframework.org/schema/cache" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/jdbc/spring-cache.xsd"> <!-- bean definitions here -->
    <!-- 通過bean元素聲明需要Spring創建的實例。該實例的類型通過class屬性指定,
    並通過id屬性為該實例指定一個名稱,以便於訪問 -->
    <!-- 定義彩色墨盒Bean,id是colorInk -->
    <bean id = "colorInk" class="cn.ink.ColorInk"/>
    <!-- 定義灰色墨盒Bean,id是greyInk -->
    <bean id = "greyInk" class="cn.ink.GreyInk"/>
    <!-- 定義A4紙張Bean,id是a4Paper -->
    <!-- 通過setCharPerLine()方法為charPerLine屬性注入每行字符數 -->
    <!-- 通過setLinePerPage()方法為linePerPage屬性注入每頁行數 -->
    <bean id = "a4Paper" class="cn.ink.TextPaper">
        <property name="charPerLine" value="10"/>
        <property name="linePerPage" value="8"/>
    </bean>
    <!-- 定義B5紙張Bean,id是b5Paper -->
    <bean id = "b5Paper" class="cn.ink.TextPaper">
        <property name="charPerLine" value="8"/>
        <property name="linePerPage" value="6"/>
    </bean>
    <!-- 組裝打印機。定義打印機Bean,該Bean的id是printer,class指定該Bean實例的實現類 -->
    <bean id = "printer" class="cn.printer.Printer">
        <!-- 通過ref屬性注入已經定義好的bean -->
        <!-- 注入彩色墨盒 -->
        <property name="ink" ref="colorInk"/>
        <!-- 注入B5打印紙張 -->
        <property name="paper" ref="b5Paper"/>
    </bean>
</beans>

 

  4.運行打印機

package cn.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.printer.Printer;

/**
 * 測試打印機
 * @author TengYiCheng
 *
 */
public class PrinterTest {
    @Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通過Printer bean的id來獲取Printer實例
        Printer printer = (Printer)context.getBean("printer");
        String content = "在本格男士的公司里,有一條明顯的階級分界線。一邊是設計師傅們,一水兒的頂配大屏幕蘋果機,夾雜着零星幾台頂配蘋果筆記本另一邊是編輯和運營師傅們,一水兒的集成顯卡普通筆記本電腦,不時有人抱怨內存硬盤不夠大。其實現在大多數男士都會把工作電腦和娛樂電腦分開,因為需求完全不同。本司有幾位科技發燒友,願意花幾萬塊攢一台電腦放家里玩游戲,但在公司,筆記本電腦就像學生手中的書和筆一樣,每天要花10個小時以上的時間對着它,還經常要隨身攜帶見客戶或者出差,輕,薄,耐用,電量足才是重點。";
        printer.print(content);
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM