通过接口,分开业务层与视图层,使代码变得集中而有效率。
接口是负责沟通业务和视图的,所以业务层需要的功能要在接口层定义,反之接口层定义的东东也要在视图层实现。
然后在视图层对接口进行引用
最后在业务层处理事务
27天整体构造完全理解以及应用
接口是负责沟通业务和视图的,所以业务层需要的功能要在接口层定义,反之接口层定义的东东也要在视图层实现。
接口代码如下:
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天整体构造完全理解以及应用