Spring IoC组装打印机

Spring IoC组装打印机
一、接口
======================================================================
package cn.dao;
public interface Ink {
    // 获得颜色的方法rgb颜色
    String getColor(int r, int g, int b);
}

package cn.dao;
public interface Paper {
    // 换行符
    public static final String newLine = "\r\n";

    // 输入一个字符到纸张
    void putInChar(char c);

    // 得到输入到纸张上的内容
    String getContent();
}
二、实体类
======================================================================
package cn.pojo;
import java.awt.Color;
import cn.dao.Ink;
public class ColorInk implements Ink {
    // 返回彩色值
    @Override
    public String getColor(int r, int g, int b) {
        // 获得16位的颜色值
        Color color = new Color(r, g, b);
        String st = "#" + Integer.toHexString(color.getRGB()).substring(2);
        return st;
    }
}

package cn.pojo;
import java.awt.Color;
import cn.dao.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);
        String st = "#" + Integer.toHexString(color.getRGB()).substring(2);
        return st;
    }
}

package cn.pojo;
import cn.dao.Ink;
import cn.dao.Paper;
public class Printer {
    // 墨盒接口
    private Ink ink;
    // 纸张接口
    private Paper paper;
    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(225, 200, 0) + "颜色打印:\n");
        // 在纸张中输入文字
        for (int i = 0; i < str.length(); i++) {
            paper.putInChar(str.charAt(i));
        }
        // 输出纸张的所有文字
        System.out.println(paper.getContent());
    }
}

package cn.pojo;
import cn.dao.Paper;
public class TextPaper implements Paper {
    // 1、每行的字符数
    private int charPerLine = 16;
    // 2、一页几行
    private int linePerPage = 5;
    // 3、纸张中的内容
    private String content = "";
    // 4、当前的横向位置0--perLine-1
    private int posX = 0;
    // 5、当前行数 0--linePerPage-1
    private int posY = 0;
    // 6、当前的页数
    private int page = 1;
    @Override
    public void putInChar(char c) {
        content += c;
        ++posX;
        // 判断是否换行
        if (posX == charPerLine) {
            content += Paper.newLine;
            posX = 0;
            ++posY;
        }
        // 判断是否翻页
        if (posY == linePerPage) {
            content += "==第" + page + "页==" + Paper.newLine;
            content += Paper.newLine;
            posY = 0;
            ++page;
        }
    }
    @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 += "==第" + page + "页==";
        }
        return ret;
    }
    public void setCharPerLine(int charPerLine) {
        this.charPerLine = charPerLine;
    }
    public void setLinePerPage(int linePerPage) {
        this.linePerPage = linePerPage;
    }
}

 三、测试类
======================================================================
package cn.test;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.pojo.Printer;
public class Tester {
    private Logger log = Logger.getLogger(this.getClass());
    private ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    @Test
    public void testPrint() {
        String str = "WARN util.NativeCodeLoader: Unable to load native-hadoop libra"
                + "ry for your platform... using builtin-java classes where applicable"
                + "java.lang.UnsatisfiedLinkError: " + //
                "org.apache.hadoop.io.nativeio.NativeIO$Windows.a"//
                + "ccess0(Ljava/lang/String;I)Z";
        Printer p1 = (Printer) context.getBean("printer");
        p1.print(str);
    }
}

四、spring-config.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans ;
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ;
    ">
    <!-- 1、定义墨盒 -->
    <bean id="colorInk" class="cn.pojo.ColorInk"/>
    <bean id="greyInk" class="cn.pojo.GreyInk"/>
    
    <!-- 2、定义纸张 -->
    <bean id="a4paper" class="cn.pojo.TextPaper" >
        <property name="charPerLine" value="10"/>
        <property name="linePerPage" value="8"/>
    </bean>
    <bean id="b5paper" class="cn.pojo.TextPaper" >
        <property name="charPerLine" value="6"/>
        <property name="linePerPage" value="5"/>
    </bean>
    
    <!-- 3、组装打印机 -->
    <bean id="printer" class="cn.pojo.Printer">
        <property name="ink" ref="colorInk"/>
        <property name="paper" ref="a4paper"/>
    </bean>
    
    <bean id="printer1" class="cn.pojo.Printer">
        <property name="ink" ref="greyInk"/>
        <property name="paper" ref="b5paper"/>
    </bean>
</beans>

五、输出结果
======================================================================
 使用#e1c800颜色打印:

WARN util.
NativeCode
Loader: Un
able to lo
ad native-
hadoop lib
rary for y
our platfo
==第1页==

rm... usin
g builtin-
java class
es where a
pplicablej
ava.lang.U
nsatisfied
LinkError:
==第2页==

 org.apach
e.hadoop.i
o.nativeio
.NativeIO$
Windows.ac
cess0(Ljav
a/lang/Str
ing;I)Z
==第3页==

======================================================================
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值