炒粉还是煮面
儿子: 爸爸,今天早餐吃什么?是炒粉还是煮面?
爸爸: 你想吃哪种呢?
儿子: 我想吃煮面。
爸爸: 煮面也有很多种,你想吃鸡蛋煮面,虾煮面,肉片煮面......
儿子: 我选虾煮面。
从开发角度来说,不管是炒还是煮,它是只是一个动作或方法。因此可以定义成为两个接口:
ICookable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ICookable
/// </summary>
namespace Insus.NET
{
public interface ICookable
{
void Cook();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ICookable
/// </summary>
namespace Insus.NET
{
public interface ICookable
{
void Cook();
}
}
ISauteable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ISauteable
/// </summary>
namespace Insus.NET
{
public interface ISauteable
{
void Saute();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ISauteable
/// </summary>
namespace Insus.NET
{
public interface ISauteable
{
void Saute();
}
}
接下来,Insus.NET实现儿子的早餐:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class Breakfast : System.Web.UI.Page, ICookable
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Cook()
{
//主材料:虾,面,生菜叶;
//辅料:花生油,盐,酱油...
Response.Write("虾煮面做好了。");
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class Breakfast : System.Web.UI.Page, ICookable
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Cook()
{
//主材料:虾,面,生菜叶;
//辅料:花生油,盐,酱油...
Response.Write("虾煮面做好了。");
}
}