Detailed description of ASP. NET Adapter design patterns

Source: Internet
Author: User

When a user asked about the Adapter mode of the design mode the day before yesterday, they said they were not very familiar with it. Let Insus. NET give a simple example. The following animation shows the effect of Insus. NET:

In the above demonstration, the two lights have the same specifications and the input voltage is 15 volts.
Light1 is used directly, while Light2 uses an Adapter (power Adapter ). Therefore, Light1 can only receive 15 volts, less than 15 volts, it will prompt that the voltage is too low, if more than 15 volts, Light1 must be burned out.

Light2 uses a power adapter that receives a voltage of 15 volts to 220. Within this voltage range, the power adapter converts the voltage to a voltage of 15. If the voltage is less than 15 volts, the system will prompt that the voltage is too low. If the voltage exceeds 220 volts, the adapter will be burned out.

Well, we started to create a Light class first:
Copy codeThe Code is 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 ("the voltage is too low. ");
Else if (value> 15)
Throw new Exception ("dangerous! The voltage is too high. ");
Else
Value = 15;
_ InputVoltage = value;
}
}
Public Light ()
{
//
// TODO: Add constructor logic here
//
}
}
}

Create a power adapter for the lamp:
Copy codeThe Code is 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 ("the voltage is too low. ");
Else if (values> 220)
Throw new Exception ("dangerous! Excessive voltage power supply adapter burned out. ");
Else
Value = 15;
_ Light. InputVoltage = value;
}
}
}
}

To test them, we have to simulate an environment and create a webpage Default. aspx:
Copy codeThe Code is as follows:
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"> outlet voltage </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"> switch </td>
<Td colspan = "2">
<Asp: CheckBox ID = "CheckBoxSwitch" runat = "server" AutoPostBack = "true" OnCheckedChanged = "CheckBoxSwitch_CheckedChanged"/> </td>
</Tr>
<Tr>
<Td align = "right"> light </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>

Next, let's take a look at the opening and closing events of the switch. There are detailed Annotations:
Copy codeThe Code is 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;
// The outlet lacks a voltage of 220 volts.
Int input = Convert. ToInt32 (string. IsNullOrEmpty (this. TextBox1.Text. Trim ())? "220": this. TextBox1.Text. Trim ());
// Switch on
If (cb. Checked)
{
Try
{
// An example of an electric lamp
Light light = new Light ();
// Insert the socket and use the outlet Voltage
Light. InputVoltage = input;
// The light is turned on
This. Image1.ImageUrl = onLight;
// Display the normal output voltage
This. Label1.Text = light. InputVoltage. ToString ();
}
Catch (Exception ex)
{
// If the voltage is abnormal, the light cannot be opened or burned out.
This. Image1.ImageUrl = offLight;
// Displays the exception information.
This. Label1.Text = ex. Message;
}
Try
{
Light light = new Light ();
// Use 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;
}
}
}

At, I added the following content. Some netizens asked about the Demo 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.