ink层Colorlnk
package cn.happy.printer.ink;
/**
* Created by Happy on 2017-07-23.
*/
public class ColorInk implements Ink {
public String getColor() {
return "彩色墨盒";
}
}
ink层GrayInk
package cn.happy.printer.ink;
/**
* Created by Happy on 2017-07-23.
*/
public class GrayInk implements Ink {
public String getColor() {
return "黑白墨盒";
}
}
package cn.happy.printer.ink;
/**
* Created by Happy on 2017-07-23.
* 墨盒接口
*/
public interface Ink {
//获取颜色的方法
public String getColor();
}
paper层A4Paper
package cn.happy.printer.paper;
/**
* Created by lenovo on 2017/7/23.
*/
public class A4Paper implements Paper {
public String getpaper() {
return "A4纸";
}
}
paper层B5Paper
package cn.happy.printer.paper;
/**
* Created by lenovo on 2017/7/23.
*/
public class B5Paper implements Paper{
public String getpaper() {
return "B5纸";
}
}
paper层Paper接口
package cn.happy.printer.paper;
/**
* Created by lenovo on 2017/7/23.
*/
public interface Paper {
public String getpaper();
}
prink层Printer
package cn.happy.printer.print;
import cn.happy.printer.ink.Ink;
import cn.happy.printer.paper.Paper;
/**
* Created by lenovo on 2017/7/23.
*/
public class Printer {
private Ink ink;
private Paper paper;
public void print(){
System.out.println("用"+ink.getColor()+"\t颜色的墨盒在"+paper.getpaper()+"\t类型的纸张上打印出:我爱你中国");
}
public Ink getInk() {
return ink;
}
public void setInk(Ink ink) {
this.ink = ink;
}
public Paper getPaper() {
return paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
}
测试类
public class SpringTest0723 {
@Test
//打印机案例
public void test02(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//装配出一台打印机
Printer pp= (Printer)ctx.getBean("pinter");
pp.print();
}