加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

依赖注入ioc 组装打印机

发布时间:2020-12-13 23:02:44 所属栏目:百科 来源:网络整理
导读:£ 如何开发一个打印机? £ 打印机 功能的实现依赖于 墨盒 和 纸张 。 £ 步骤: 1、定义墨盒和纸张的 接口 标准。 2、使用接口标准开发打印机。 3 、组装 打印机。 4、运行打印机。 1: 1. 定义组件接口 ? 墨盒接口:Ink ? 纸张接口:Page public interface

如何开发一个打印机?

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

步骤:

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

2、使用接口标准开发打印机。

3、组装打印机。

4、运行打印机。

1:

1. 定义组件接口
?墨盒接口:Ink
?纸张接口:Page
public interface Ink {
public String getColor(int r,int g,int b);
}
public interface Paper {
public static final String newline = "rn";
/**
* 输出字符到纸张
*/
public void putInChar(char c);
/**
* 得到输出到纸张上的内容
*/
public String getContent();
}
//我们在开发打印机时,使用了Ink和Paper接口。但并不关心其实现。
public class Printer {
public Ink ink = null;
public Paper paper = null;
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.print( paper .getContent()); // 将纸张的内容输出
}
}
//
3、组装打印机
1、为了方便组装,我们给Printer类的ink和paper属性增加setter方法
public class Printer {
public Ink ink = null;
public Paper paper = null;
... ...
public void setInk (Ink ink) {
this.ink = ink;
}
public void setPaper (Paper paper) {
this.paper = paper;
}
}
2、创建或得到Ink和Paper的实现类
3、使用Spring进行组装
编辑applicationContext.xml文件

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读