博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET的适配器设计模式(Adapter)
阅读量:7228 次
发布时间:2019-06-29

本文共 4594 字,大约阅读时间需要 15 分钟。

前天有一网友问及有关设计模式的适配器模式(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 description for Light/// 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 description for PowerAdapter/// 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" %>            
插座电压
开关
Light 1
Light 2

接下来,看看开关的事开与关的事件,有详细的注解:

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)

 

转载地址:http://orbfm.baihongyu.com/

你可能感兴趣的文章
更好用的集群限流功能,Sentinel 发布 v1.4.2
查看>>
Python(生成执行文件)
查看>>
redis安装配置 - ttlsa教程系列之redis
查看>>
Linux --DHCP服务器配置;DHCP服务器中继
查看>>
IE版本多的可爱_已迁移
查看>>
eclipse查看jar包中class的中文注释乱码问题的解决
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
mariadb安装
查看>>
vue+vuex+axios+echarts画一个动态更新的中国地图
查看>>
5.8 volumetric post-processing--game programming gems5 笔记
查看>>
8086的地址空间
查看>>
Android开发动画效果被遮掉的解决方法
查看>>
Apache2.2.17源码编译安装以及配置虚拟主机
查看>>
2017年开发语言排名
查看>>
读二进制表的显示 Binary Watch
查看>>
我的友情链接
查看>>
linux基础:10、基础命令(4)
查看>>
linux中强大的screen命令
查看>>
放开那个程序员
查看>>