SSM(十四)实现动态组装的打印机

一、问题:如何开发一个打印机

可灵活配置使用彩色墨盒或灰色墨盒

可灵活配置打印页面的大小

二、分析

打印机功能的实现依赖于墨盒和纸张

步骤:

定义墨盒和纸张的接口标准

使用接口标准开发打印机

组装打印机

运行打印机

三、代码

cn.printer.Ink

package cn.printer;
/**
 * 墨盒接口
 * @author xie
 *
 */
public interface Ink {
	/**
	 * 定义打印机采用的颜色值
	 * @param r
	 * @param g
	 * @param b
	 * @return
	 */
	public String getColor(int r, int g, int b);
}

cn.printer.paper

package cn.printer;
/**
 * 打印采用的纸张接口
 * @author xie
 *
 */
public interface Paper {
	public static final String newLine = "\r\n";
	
	/**
	 * 往纸张里逐个字符输入内容
	 * @param c
	 */
	public void putInChar(char c);
	
	/**
	 * 得到输出到纸张上的内容
	 * @return
	 */
	public String getContent();
}

cn.ink.GreyInk

package cn.ink;


import java.awt.Color;

import cn.printer.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);
		return "#" + Integer.toHexString(color.getRGB()).substring(2);
	}

}

cn.ink.ColorInk

package cn.ink;

import java.awt.Color;

import cn.printer.Ink;

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);
	}

}

cn.paper.Textpaper

package cn.paper;

import cn.printer.Paper;

public class Textpaper implements Paper {
	
	//每一行能输入的字符数
	private int charPerLine = 16;
	//每一页的行数
	private int linePerPage = 8;
	
	/**
	 * 设值注入所需要的setter方法,注入charPerLine的值
	 * @param charPerLine
	 */
	public void setCharPerLine(int charPerLine) {
		this.charPerLine = charPerLine;
	}

	/**
	 * 设值注入所需要的setter方法,注入linePerPage的值
	 * @param linePerPage
	 */
	public void setLinePerPage(int linePerPage) {
		this.linePerPage = linePerPage;
	}
	
	//当前的横向位置,从0到charPerLine - 1
	private int posx = 0;
	//当前的行号,从0到linePerPage - 1
	private int posy = 0;
	//当前页码,从1开始
	private int posp = 1;
	//纸张中的内容
	private String content = "";
	
	@Override
	public void putInChar(char c) {
		content += c;
		++posx;
		//判断是否换行
		if(posx == charPerLine - 1) {
			content += Paper.newLine;
			posx = 0;
			++posy;
		}
		//判断是否换页
		if(posy == linePerPage - 1) {
			content += "==第" + posp + "页==";
			content += Paper.newLine;
			content += Paper.newLine;
			posy = 0;
			++posp;
		}
	}

	@Override
	public String getContent() {
		String ret = content;
		if(!(posx == 0 && posy == 0)) {
			int count = linePerPage - 1 - posy;
			for(int i = 0; i < count; i++) {
				ret += Paper.newLine;
			}
			ret += "==第" + posp + "页==";
		}
		return ret;
	}

}

cn.printer.Printer

package cn.printer;
/**
 * 打印机类
 * @author xie
 *
 */
public class Printer {

	//面向接口编程,定义Ink接口变量
	private Ink ink;
	//面向接口编程,定义Paper接口变量
	private Paper paper;
	
	/**
	 * 提供Ink的setter方法,以进行设值注入
	 * @param ink
	 */
	public void setInk(Ink ink) {
		this.ink = ink;
	}
	
	/**
	 * 提供Paper的setter方法,以进行设值注入
	 * @param paper
	 */
	public void setPaper(Paper paper) {
		this.paper = paper;
	}
	
	/**
	 * 打印方法,获取打印机的颜色和纸张内容
	 * @param str
	 */
	public void print(String str) {
		System.out.println("打印采用的颜色为:" + ink.getColor(222, 234, 90));
		for(int i = 0; i < str.length(); i++) {
			paper.putInChar(str.charAt(i));
		}
		System.out.println(paper.getContent());
	}
	
}

ApplicationContext.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.xsd">
	<!-- 指定灰色墨盒的bean元素,该元素的id属性为对象名greyInk,class属性指定要实例化的类 -->
	<bean id="greyInk" class="cn.ink.GreyInk"></bean>
	<!-- 指定彩色墨盒的bean元素,该元素的id属性为对象名colorInk,class属性指定要实例化的类 -->
	<bean id="colorInk" class="cn.ink.ColorInk"></bean>
	<!-- 指定A4纸的bean元素,该元素的id属性为对象名a4paper,class属性指定要实例化的类 -->
	<bean id="a4paper" class="cn.paper.Textpaper">
		<!-- property元素指定需要赋值的属性 -->
		<property name="charPerLine" value="8"/>
		<property name="linePerPage" value="6"/>
	</bean>
	<!-- 指定B5纸的bean元素,该元素的id属性为对象名b5paper,class属性指定要实例化的类 -->
	<bean id="b5paper" class="cn.paper.Textpaper">
		<property name="charPerLine" value="6"/>
		<property name="linePerPage" value="4"/>
	</bean>
	<!-- 指定打印机的bean元素,该元素的id属性为对象名printer,class属性指定要实例化的类 -->
	<bean id="printer" class="cn.printer.Printer">
		<property name="ink" ref="greyInk"/>
		<property name="paper" ref="a4paper"/>
	</bean>
</beans>

cn.test.PrinterTest

package cn.test;

import static org.junit.jupiter.api.Assertions.*;

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

import cn.printer.Printer;

class PrinterTest {

	@Test
	void test() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		Printer printer = (Printer) applicationContext.getBean("printer");
		String content = "图书简介" 
				+ "作者简介" 
				+ "曾国藩,晚清名臣,清朝战略家、政治家,晚清散文“湘乡派”创立人。"
				+ "晚清“中兴四大名臣”之一,官至两江总督、直隶总督、武英殿大学士,封一等毅勇侯,谥曰文正。"
				+ "著有《治学论道之经》《持家教子之术》《冰鉴》《曾文正公全集》《曾国藩家书》等作品。";
		printer.print(content);
	}

}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值