ASP.NET的適配器設(shè)計(jì)模式(Adapter)應(yīng)用詳解
更新時(shí)間:2013年02月07日 11:34:24 作者:
有關(guān)設(shè)計(jì)模式的適配器模式(Adapter)確實(shí)不是很好理解理解,接下來將做一個(gè)簡單的例子簡要說明下,感興趣的朋友可不要錯(cuò)過了哈,希望本文可以幫助到你更好的理解適配器設(shè)計(jì)模式
前天有一網(wǎng)友問及有關(guān)設(shè)計(jì)模式的適配器模式(Adapter)時(shí),說不太好理解。讓Insus.NET能否舉個(gè)簡單的例子來說明一下。下面的動畫是Insus.NET做出來的效果:

上面的演示,兩個(gè)燈的規(guī)格一樣,要求輸入的電壓為15伏。
Light1是直接使用,而Light2是使用Adapter(電源適配器)。因此Light1只能接收15伏的電壓,小于15伏,會提示電壓過低,如果超過了15伏,Light1肯定被燒壞。
Light2使用了電源適配器,它接收15伏至220的電壓,在這電壓范圍之內(nèi),電源適配器會把電壓轉(zhuǎn)為15的電壓。小于15伏,會提示電壓過低,如果超過了220伏,適配器被燒壞。
好,我們程序開始,先創(chuàng)建一個(gè)燈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("危險(xiǎn)!電壓過大燈燒壞。");
else
value = 15;
_InputVoltage = value;
}
}
public Light()
{
//
// TODO: Add constructor logic here
//
}
}
}
再創(chuàng)建一個(gè)燈的電源適配器:
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("危險(xiǎn)!電壓過大電源適配器燒壞。");
else
value = 15;
_Light.InputVoltage = value;
}
}
}
}
如何測試它們,我們得模擬一個(gè)環(huán)境,創(chuàng)建一個(gè)網(wǎng)頁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">開關(guān)</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>
接下來,看看開關(guān)的事開與關(guān)的事件,有詳細(xì)的注解:
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());
//開關(guān)打開
if (cb.Checked)
{
try
{
//實(shí)例一個(gè)電燈
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;
}
//開關(guān)關(guān)閉
else
{
this.TextBox1.Text = string.Empty;
this.TextBox1.Enabled = true;
this.Image1.ImageUrl = offLight;
this.Image2.ImageUrl = offLight;
}
}
}
11:44分,補(bǔ)充下面內(nèi)容,有網(wǎng)友問及演示完整代碼(.NET Framework 4.0)

上面的演示,兩個(gè)燈的規(guī)格一樣,要求輸入的電壓為15伏。
Light1是直接使用,而Light2是使用Adapter(電源適配器)。因此Light1只能接收15伏的電壓,小于15伏,會提示電壓過低,如果超過了15伏,Light1肯定被燒壞。
Light2使用了電源適配器,它接收15伏至220的電壓,在這電壓范圍之內(nèi),電源適配器會把電壓轉(zhuǎn)為15的電壓。小于15伏,會提示電壓過低,如果超過了220伏,適配器被燒壞。
好,我們程序開始,先創(chuàng)建一個(gè)燈Light的類:
復(fù)制代碼 代碼如下:
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("危險(xiǎn)!電壓過大燈燒壞。");
else
value = 15;
_InputVoltage = value;
}
}
public Light()
{
//
// TODO: Add constructor logic here
//
}
}
}
再創(chuàng)建一個(gè)燈的電源適配器:
復(fù)制代碼 代碼如下:
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("危險(xiǎn)!電壓過大電源適配器燒壞。");
else
value = 15;
_Light.InputVoltage = value;
}
}
}
}
如何測試它們,我們得模擬一個(gè)環(huán)境,創(chuàng)建一個(gè)網(wǎng)頁Default.aspx:
復(fù)制代碼 代碼如下:
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">開關(guān)</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>
接下來,看看開關(guān)的事開與關(guān)的事件,有詳細(xì)的注解:
復(fù)制代碼 代碼如下:
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());
//開關(guān)打開
if (cb.Checked)
{
try
{
//實(shí)例一個(gè)電燈
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;
}
//開關(guān)關(guān)閉
else
{
this.TextBox1.Text = string.Empty;
this.TextBox1.Enabled = true;
this.Image1.ImageUrl = offLight;
this.Image2.ImageUrl = offLight;
}
}
}
11:44分,補(bǔ)充下面內(nèi)容,有網(wǎng)友問及演示完整代碼(.NET Framework 4.0)
相關(guān)文章
DataGridView展開與收縮功能實(shí)現(xiàn)
我們今天將要講到DataGridView之行的展開與收縮,包括功能是如何實(shí)現(xiàn)的,感興趣的小伙伴們可以參考一下2015-09-09
asp.net 數(shù)據(jù)綁定的實(shí)例代碼
這篇文章介紹了asp.net 數(shù)據(jù)綁定的實(shí)例代碼,有需要的朋友可以參考一下2013-07-07
.net 運(yùn)用二進(jìn)制位運(yùn)算進(jìn)行數(shù)據(jù)庫權(quán)限管理
.net 運(yùn)用二進(jìn)制位運(yùn)算進(jìn)行數(shù)據(jù)庫權(quán)限管理 ,需要的朋友可以參考一下2013-02-02
asp.net FileUpload控件實(shí)現(xiàn)文件格式判斷與文件大小限制
這篇文章主要介紹了有關(guān)asp.net fileupload控件判斷文件格式,以及進(jìn)行文件大小限制的方法,可以在web.config中配置,也可以在.cs文件中實(shí)現(xiàn),需要的朋友參考下2014-11-11

