您的位置:首页 > 编程语言 > ASP

ASP.NET的适配器设计模式(Adapter)

2013-02-04 11:25 357 查看
前天有一网友问及有关设计模式的适配器模式(Adapter)时,说不太好理解。让Insus.NET能否举个简单的例子来说明一下。下面的动画是Insus.NET做出来的效果:
Default.aspx.cs

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 _Default : System.Web.UI.Page
{
string offLight = "~/Images/Light_C.gif";
string onLight = "~/Images/Light_O.gif";

protected void Page_Load(object sender, EventArgs e)
{

}

protected void CheckBoxSwitch_CheckedChanged(object sender, EventArgs e)
{
var cb = (CheckBox)sender;

//插座缺少电压为220伏
int input = Convert.ToInt32(string.IsNullOrEmpty(this.TextBox1.Text.Trim()) ? "220" : this.TextBox1.Text.Trim());

//开关打开
if (cb.Checked)
{
try
{
//实例一个电灯
Light light = new Light();

//插入插座,使用插座电压
light.InputVoltage = input;

//电灯被打开
this.Image1.ImageUrl = onLight;

//显示正常输出电压
this.Label1.Text = light.InputVoltage.ToString();
}
catch (Exception ex)
{
//如果电压不正常,电灯打不开或是被烧坏。
this.Image1.ImageUrl = offLight;

//显示异常信息。
this.Label1.Text = ex.Message;
}

try
{
Light light = new Light();

//使用电源适配器
PowerAdapter pa = new PowerAdapter(light);
pa.InputVoltage = input;

this.Image2.ImageUrl = onLight;
this.Label2.Text = pa.InputVoltage.ToString();

}
catch (Exception ex)
{
this.Image2.ImageUrl = offLight;
this.Label2.Text = ex.Message;
}

this.TextBox1.Enabled = false;
}
//开关关闭
else
{
this.TextBox1.Text = string.Empty;
this.TextBox1.Enabled = true;
this.Image1.ImageUrl = offLight;
this.Image2.ImageUrl = offLight;
}
}
}


11:44分,补充下面内容,有网友问及演示完整代码(.NET Framework 4.0)

http://download.cnblogs.com/insus/ASPDOTNET/AdapterDemo.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: