明霞山资源网 Design By www.htccd.com
前天有一网友问及有关设计模式的适配器模式(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)
标签:
适配器,设计模式,Adapter
明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。