{
//定义可提供的墨盒类型以及对打印机供应商和墨盒供应商的墨盒类型的约束
//体现接口的定义:它是一种契约:能提供功能+约束能力
string GetInkType();
}
{
//表明提供兼容的纸张类型以及对打印机生产商和纸张生产商的纸张约定
string GetPaperType();
}
{
//打印机供应商和纸张供应商兼容A4纸张
public string GetPaperType()
{
return "A4";
}
}
{
public string GetPaperType()
{
return "A5";
}
}
{
//彩色墨盒
public string GetInkType()
{
return "ColorInkBox";
}
}
{
//黑色墨盒
public string GetInkType()
{
return "GrayInkBox";
}
}
/// 打印机供应商遵循纸张类型和墨盒类型的契约
/// </summary>
public class Printor
{
/// <summary>
/// 打印
/// </summary>
/// <param name="argPaper">约定的纸张类型:A4,A5</param>
/// <param name="argIntBox">约定的墨盒类型:Black,Color</param>
public void Print(IPaper argPaper,IInkBox argIntBox)
{
string paperType = argPaper.GetPaperType();
string inkBoxType = argIntBox.GetInkType();
if (paperType == "A4" && inkBoxType == "GrayInkBox")
{
//其他功能准备过程
//......
//插入黑色墨盒和A4纸
//打印.....
Console.WriteLine("提供{0}墨盒{1}纸张打印结束\n",inkBoxType,paperType);
}
else if (paperType == "A5" && inkBoxType == "GrayInkBox")
{
//其他功能准备过程
//......
// 插入黑色墨盒和A5纸
//打印.....
Console.Write("使用{0}墨盒{1}纸张打印结束\n", inkBoxType, paperType);
}
else if (paperType == "A4" && inkBoxType == "ColorInkBox")
{
//其他功能准备过程
//......
//插入彩色墨盒和A4纸
//打印.....
Console.Write("使用{0}墨盒{1}纸张打印结束\n", inkBoxType, paperType);
}
else if (paperType == "A5" && inkBoxType == "ColorInkBox")
{
//其他功能准备过程
//......
//插入彩色墨盒和A5纸
//打印.....
Console.Write("使用{0}墨盒{1}纸张打印结束\n", inkBoxType, paperType);
}
}
}
public class Program
{
//客户要求打印彩色+A5纸张内容
static void Main(string[] args)
{
//打印机供应商提供兼容式打印机
Printor printor=new Printor();
//根据用户要求重置打印机
//彩色墨盒+A4纸打印
var colorInkBox = new ColorInkbox();
var a4Paper = new A4Paper();
// 打印
printor.Print(a4Paper,colorInkBox);
//根据用户要求重置打印机
//彩色墨盒+A5纸打印
var a5Paper = new A5Paper();
// 打印
printor.Print(a5Paper, colorInkBox);
//根据用户要求重置打印机
//黑色墨盒+A4
var grayInkBox = new GrayInkBox();
//打印
printor.Print(a4Paper,grayInkBox);
//根据用户要求重置打印机
//黑色墨盒+A5
//打印
printor.Print(a5Paper, grayInkBox);
}
}