• 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 = "	
    ";
     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)+"颜色打印:
    ");
            //逐字符输出到纸张
            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);
        }
        
    }
  • 相关阅读:
    C# WinForm TreeView改变选中节点颜色,失去焦点选中节点仍突显
    TextBox的ReadOnly属性设置为True后获取不到值
    如何设置打印机共享
    细节啊,不注意真是会很纠结
    设置access类型
    未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
    带关闭的漂浮广告
    图片添加热点
    使用 WebDeploymentSetup VS 2010 Web项目部署
    动态操作表格
  • 原文地址:https://www.cnblogs.com/tengqiuyu/p/7676391.html
Copyright © 2020-2023  润新知