Asp. NET Adapter design pattern (Adapter) Application Details _ Practical skills

Source: Internet
Author: User
Tags trim
The day before yesterday, a netizen asked about the design mode of the adapter mode (Adapter), said not very good understanding. Can insus.net give a simple example to illustrate. The following animation is the result of insus.net:

The above demo, two lamps of the same specification, requires the input voltage of 15 volts.
LIGHT1 is used directly, while LIGHT2 is using adapter (power adapter). So Light1 can only receive 15 volts of voltage, less than 15 volts, will prompt the voltage is too low, if more than 15 volts, Light1 must be burnt out.

The LIGHT2 uses a power adapter that receives a voltage of 15 volts to 220, within which the power adapter will convert the voltage to a voltage of 15. Less than 15 volts, will prompt the voltage is too low, if more than 220 volts, the adapter is burnt out.

OK, let's start the program by creating a light light class:
Copy Code code as follows:

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 ("voltage is too low." ");
else if (Value > 15)
throw new Exception ("Danger!") The voltage was too big for the lamp to burn out. ");
Else
value = 15;
_inputvoltage = value;
}
}
Public Light ()
{
//
Todo:add constructor Logic here
//
}
}
}

Then create a power adapter for the lamp:
Copy Code code as follows:

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 ("voltage is too low." ");
else if (value > 220)
throw new Exception ("Danger!") The voltage is too large for the power adapter to burn out. ");
Else
value = 15;
_light.inputvoltage = value;
}
}
}
}

How to test them, we have to simulate an environment, create a Web page default.aspx:
Copy Code code as follows:

Default.aspx
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE html>
<title></title>
<script type= "Text/javascript" >
function IsNumeric (keycode) {
Return ((keycode >= && keycode <=) | | keycode = 8)
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<table>
<tr>
&LT;TD align= "right" > Socket voltage </td>
&LT;TD colspan= "2" >
<asp:textbox id= "TextBox1" runat= "Server" onkeydown= "return IsNumeric (Event.keycode);" "Text=" ></asp:TextBox></td>
</tr>
<tr>
&LT;TD align= "Right" > Switch </td>
&LT;TD colspan= "2" >
<asp:checkbox id= "Checkboxswitch" runat= "Server" autopostback= "true" oncheckedchanged= "Checkboxswitch_" CheckedChanged "/></td>
</tr>
<tr>
&LT;TD align= "Right" > Light </td>
<td>
<fieldset style= "width:200px;" >
<legend>light 1
</legend>
<asp:image id= "Image1" runat= "imageurl= images/light_c.gif" width= "height="/><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= "imageurl= images/light_c.gif" width= "height="/><br "/>"
<asp:label id= "Label2" runat= "Server" text= "" ></asp:Label>
</fieldset>
</td>
</tr>
</table>
</form>
</body>

Next, look at the switch to open and close the event, there are detailed notes:
Copy Code code as follows:

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;
Socket missing voltage of 220 Volts
int input = Convert.ToInt32 (string. IsNullOrEmpty (this. TextBox1.Text.Trim ())? "The": this. TextBox1.Text.Trim ());
Switch Open
if (CB). Checked)
{
Try
{
Example of an electric light
Light Light = new Light ();
Insert socket, use socket voltage
Light. Inputvoltage = input;
The light is open.
This. Image1.imageurl = Onlight;
Display normal output voltage
This. Label1.Text = light. Inputvoltage.tostring ();
}
catch (Exception ex)
{
If the voltage is not normal, the light cannot be opened or burnt out.
This. Image1.imageurl = Offlight;
Displays exception information.
This. Label1.Text = ex. message;
}
Try
{
Light Light = new Light ();
Using the Power adapter
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;
}
Switch off
Else
{
This. TextBox1.Text = string. Empty;
This. Textbox1.enabled = true;
This. Image1.imageurl = Offlight;
This. Image2.imageurl = Offlight;
}
}
}

11:44 points, add the following content, a netizen asked to demonstrate the complete code (. NET Framework 4.0)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.