ASP.NET的适配器设计模式(Adapter)应用详解
程序员文章站
2024-03-05 10:44:06
前天有一网友问及有关设计模式的适配器模式(adapter)时,说不太好理解。让insus.net能否举个简单的例子来说明一下。下面的动画是insus.net做出来的效果:...
前天有一网友问及有关设计模式的适配器模式(adapter)时,说不太好理解。让insus.net能否举个简单的例子来说明一下。下面的动画是insus.net做出来的效果:
上面的演示,两个灯的规格一样,要求输入的电压为15伏。
light1是直接使用,而light2是使用adapter(电源适配器)。因此light1只能接收15伏的电压,小于15伏,会提示电压过低,如果超过了15伏,light1肯定被烧坏。
light2使用了电源适配器,它接收15伏至220的电压,在这电压范围之内,电源适配器会把电压转为15的电压。小于15伏,会提示电压过低,如果超过了220伏,适配器被烧坏。
好,我们程序开始,先创建一个灯light的类:
light.cs
using system;
using system.collections.generic;
using system.linq;
using system.web;
/// <summary>
/// summary description for light
/// </summary>
namespace insus.net
{
public class light
{
private int _inputvoltage = 15;
public int inputvoltage
{
get { return _inputvoltage; }
set
{
if (value < 15)
throw new exception("电压过低。");
else if (value > 15)
throw new exception("危险!电压过大灯烧坏。");
else
value = 15;
_inputvoltage = value;
}
}
public light()
{
//
// todo: add constructor logic here
//
}
}
}
再创建一个灯的电源适配器:
poweradapter.cs
using system;
using system.collections.generic;
using system.linq;
using system.web;
/// <summary>
/// summary description for poweradapter
/// </summary>
namespace insus.net
{
public class poweradapter : light
{
light _light;
public poweradapter(light light)
{
this._light = light;
}
public int inputvoltage
{
get
{
return _light.inputvoltage;
}
set
{
if (value < 15)
throw new exception("电压过低。");
else if (value > 220)
throw new exception("危险!电压过大电源适配器烧坏。");
else
value = 15;
_light.inputvoltage = value;
}
}
}
}
如何测试它们,我们得模拟一个环境,创建一个网页default.aspx:
default.aspx
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function isnumeric(keycode) {
return ((keycode >= 48 && keycode <= 57) || keycode == 8)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td align="right">插座电压</td>
<td colspan="2">
<asp:textbox id="textbox1" runat="server" onkeydown="return isnumeric(event.keycode);" text="220"></asp:textbox></td>
</tr>
<tr>
<td align="right">开关</td>
<td colspan="2">
<asp:checkbox id="checkboxswitch" runat="server" autopostback="true" oncheckedchanged="checkboxswitch_checkedchanged" /></td>
</tr>
<tr>
<td align="right">灯</td>
<td>
<fieldset style="width: 200px;">
<legend>light 1
</legend>
<asp:image id="image1" runat="server" imageurl="images/light_c.gif" width="36" height="55" /><br />
<asp:label id="label1" runat="server" text=""></asp:label>
</fieldset>
</td>
<td>
<fieldset style="width: 250px;">
<legend>light 2
</legend>
<asp:image id="image2" runat="server" imageurl="images/light_c.gif" width="36" height="55" /><br />
<asp:label id="label2" runat="server" text=""></asp:label>
</fieldset>
</td>
</tr>
</table>
</form>
</body>
</html>
接下来,看看开关的事开与关的事件,有详细的注解:
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)
上面的演示,两个灯的规格一样,要求输入的电压为15伏。
light1是直接使用,而light2是使用adapter(电源适配器)。因此light1只能接收15伏的电压,小于15伏,会提示电压过低,如果超过了15伏,light1肯定被烧坏。
light2使用了电源适配器,它接收15伏至220的电压,在这电压范围之内,电源适配器会把电压转为15的电压。小于15伏,会提示电压过低,如果超过了220伏,适配器被烧坏。
好,我们程序开始,先创建一个灯light的类:
复制代码 代码如下:
light.cs
using system;
using system.collections.generic;
using system.linq;
using system.web;
/// <summary>
/// summary description for light
/// </summary>
namespace insus.net
{
public class light
{
private int _inputvoltage = 15;
public int inputvoltage
{
get { return _inputvoltage; }
set
{
if (value < 15)
throw new exception("电压过低。");
else if (value > 15)
throw new exception("危险!电压过大灯烧坏。");
else
value = 15;
_inputvoltage = value;
}
}
public light()
{
//
// todo: add constructor logic here
//
}
}
}
再创建一个灯的电源适配器:
复制代码 代码如下:
poweradapter.cs
using system;
using system.collections.generic;
using system.linq;
using system.web;
/// <summary>
/// summary description for poweradapter
/// </summary>
namespace insus.net
{
public class poweradapter : light
{
light _light;
public poweradapter(light light)
{
this._light = light;
}
public int inputvoltage
{
get
{
return _light.inputvoltage;
}
set
{
if (value < 15)
throw new exception("电压过低。");
else if (value > 220)
throw new exception("危险!电压过大电源适配器烧坏。");
else
value = 15;
_light.inputvoltage = value;
}
}
}
}
如何测试它们,我们得模拟一个环境,创建一个网页default.aspx:
复制代码 代码如下:
default.aspx
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function isnumeric(keycode) {
return ((keycode >= 48 && keycode <= 57) || keycode == 8)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td align="right">插座电压</td>
<td colspan="2">
<asp:textbox id="textbox1" runat="server" onkeydown="return isnumeric(event.keycode);" text="220"></asp:textbox></td>
</tr>
<tr>
<td align="right">开关</td>
<td colspan="2">
<asp:checkbox id="checkboxswitch" runat="server" autopostback="true" oncheckedchanged="checkboxswitch_checkedchanged" /></td>
</tr>
<tr>
<td align="right">灯</td>
<td>
<fieldset style="width: 200px;">
<legend>light 1
</legend>
<asp:image id="image1" runat="server" imageurl="images/light_c.gif" width="36" height="55" /><br />
<asp:label id="label1" runat="server" text=""></asp:label>
</fieldset>
</td>
<td>
<fieldset style="width: 250px;">
<legend>light 2
</legend>
<asp:image id="image2" runat="server" imageurl="images/light_c.gif" width="36" height="55" /><br />
<asp:label id="label2" runat="server" text=""></asp:label>
</fieldset>
</td>
</tr>
</table>
</form>
</body>
</html>
接下来,看看开关的事开与关的事件,有详细的注解:
复制代码 代码如下:
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)