软件架构之多层级互动

通过接口,分开业务层与视图层,使代码变得集中而有效率。
接口是负责沟通业务和视图的,所以业务层需要的功能要在接口层定义,反之接口层定义的东东也要在视图层实现。

接口代码如下:

interface IForm
    {
        #region Events
        event EventHandler InnerTimerTick;
        event EventHandler button1_Click;
        event EventHandler button2_Click;
        event EventHandler button3_Click;
        #endregion Events
        
        #region Properties
    
        PictureBox AnimationImage
        {
            get;
            set;
        }

        int AnimationPlayIndex
        {
            get;
            set;
        }
        Timer InnerTimer
        {
            get;
            set;
        }
        #endregion Properties
    }
也就是说业务需要通过视图层如下进行控制。
然后在视图层对接口进行引用

public partial class Form1 : Form,IForm
引用后对接口进行实现

  #region Events

        public event EventHandler InnerTimerTick;
        public event EventHandler button1_Click;
        public event EventHandler button2_Click;
        public event EventHandler button3_Click;
        #endregion Events

        #region Properties
        public Timer InnerTimer
        {
            get
            {
                return this.time;
            }
            set
            {
                time = value;
            }
        }
        public PictureBox AnimationImage
        {
            get
            {
                return this.pictureBox1;
            }
            set
            {
                pictureBox1 = value;
            }
        }
视图对业务层的调用

    private Timer time=new Timer ();
        //需要调用业务层处理业务
        private readonly Yewu yewu;
        public Form1()
        {
            InitializeComponent();
            //通过业务层引用让业务层负责视图业务功能
            yewu = new Yewu(this);
            if (InnerTimerTick != null)
            {
                this.time.Tick += InnerTimerTick;
            }
            if (button1_Click != null)
            {
                this.button1.Click += button1_Click;
            }
            if (button2_Click != null)
            {
                this.button2.Click += button2_Click;
            }
            if (button3_Click != null)
            {
                this.button3.Click += button3_Click;
            }
            tianjia();
        }
同时在业务层

  //以接口为对象来处理视图层
        private readonly IForm iform;

最后在业务层处理事务

  //构造函数
        public Yewu(IForm _iform)
        {
            //下边这句话是把接口是实现联系起来
            iform = _iform;
            iform.InnerTimerTick += new EventHandler(iform_InnerTimerTick);
            iform.button1_Click += new EventHandler(iform_button1_Click);
            iform.button2_Click += new EventHandler(iform_button2_Click);
            iform.button3_Click += new EventHandler(iform_button3_Click);
        }

        void iform_button3_Click(object sender, EventArgs e)
        {
            chan = "RightTurn";
            iform.InnerTimer.Enabled = true;
        }

27天整体构造完全理解以及应用




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值