把【观察者模式】【MVC】【三层架构】说透

1/11/2010来源:ASP技巧人气:3627

最近发现好多朋友 观察者模式 MVC  三层架构不分,所以独立开篇,把他们说明白说透!而且有很多理解误区,列举如下:
误区一:观察者模式==MVC
误区二:三层架构==MVC
误区三:asp.net MVC==MVC
误区四:WebForm编程模型没有用到MVC



.NET内置观察者模式

MVC的由来?

三层架构的由来?

秉着重构的思想,一步一步从观察者模式重构到MVC重构到三层架构!



【一】.NET内置的观察者模式--事件

1     public class MyEventArgs : EventArgs
2         {
3             PRivate int id;
4             public int Id
5             {
6                 get{return this.id;}
7             }
8             public MyEventArgs(int id)
9             {
10                 this.id = id;
11             }
12         }
13
14         public class User
15         {
16             public event EventHandler<MyEventArgs> IdChanged;
17             private int id;
18             public int Id
19             {
20                 get { return id; }
21                 set
22                 {
23                     id = value;
24                     IdChanged(this, new MyEventArgs(id));
25                 }
26             }
27         }
28
29
30        public  class Controller
31        {
32          User user = new User();
33          private void OnIdChanged(object send, MyEventArgs args)
34          {
35            //把改变后的Id显示在网页里面
36             ShowInInerteNet(user);
37          }
38          public void Test()
39          {          
40            user.IdChanged += this.OnIdChanged;
41            //改变Id
42             user.Id="1314";
43          }
44        }



这就是观察者模式,就这么简单。



【二】MVC的由来

如果我不想让id显示在网页里,我要让他显示在winform窗体里,那么类Controller的程序要频繁滴改动,根据开放关闭原则----对扩展开发  对修改关闭,所以很容易让人想到抽象出显示方式的。所以有:

1 public interface IView
2 {
3         public void Show(User user);
4 }
5 public class IntertNetShow:IView
6 {
7         public void Show(User user)
8         {
9          //doSomething
10         }
11 }
12 public class WinFormShow:IView
13 {
14         public void Show(User user)
15         {
16          //doSomething
17         }
18 }
19       public  class Controller
20        {
21          //这里可以实现依赖注入
22          private  CustomShow customShow=new IntertNetShow();
23          User user = new User();
24          private void OnIdChanged(object send, MyEventArgs args)
25          {
26             customShow.Show(user);
27          }
28          public void Test()
29          {          
30            user.IdChanged += this.OnIdChanged;
31            //改变Id
32             GetIdFormDB(user);
33          }
34        }




上面是user.Id是直接赋值,现在我想从数据库中读出id的值或者从xml中读出user.Id的值,那么Control类就需要频繁的修改。根据开发关闭原则-----对扩展开发  对修改关闭,也和上面同样的思想,把改变Id的方法抽象出来。

1 public interface IModel
2 {
3         public void GetId(User user);
4 }
5 public class GetIdFromDB: IModel
6 {
7         public void GetId(User user)
8         {
9          //doSomething
10         }
11 }
12 public class GetIdFromXML:IModel
13 {
14         public void GetId(User user)
15         {
16          //doSomething
17         }
18 }
19    public  class Control
20        {
21          //这里可以实现依赖注入
22          private  IView view=new IntertNetShow();
23          //这里可以实现依赖注入
24           private  IModel model=new GetIdFromDB();
25          User user = new User();
26          private void OnIdChanged(object send, MyEventArgs args)
27          {
28             view.Show(user);
29          }
30          public void Test()
31          {          
32            user.IdChanged += this.OnIdChanged;
33            //改变Id
34            model.GetId(user);
35          }
36        }


所以重构完成===》MVC.   完整的代码如下:



代码
1 public interface IView
2 {
3         public void Show(User user);
4 }
5 public class IntertNetShow:IView
6 {
7         public void Show(User user)
8         {
9          //doSomething
10         }
11 }
12 public class WinFormShow:IView
13 {
14         public void Show(User user)
15         {
16          //doSomething
17         }
18 }
19
20 public interface IModel
21 {
22         public void GetId(User user);
23 }
24 public class GetIdFromDB: IModel
25 {
26         public void GetId(User user)
27         {
28          //doSomething
29         }
30 }
31 public class GetIdFromXML:IModel
32 {
33         public void GetId(User user)
34         {
35          //doSomething
36         }
37 }
38
39    public  class Controller
40        {
41          //这里可以实现依赖注入
42          private  IView view=new IntertNetShow();
43          //这里可以实现依赖注入
44          private  IModel model=new GetIdFromDB();
45          User user = new User();
46          private void OnIdChanged(object send, MyEventArgs args)
47          {
48             view.Show(user);
49          }
50          public void Test()
51          {          
52            user.IdChanged += this.OnIdChanged;
53            //改变Id
54           model.GetId(user);
55          }
56        }




细心的朋友可以发现,这很像webform和winform编程模型。对!webform和winform也用了MVC。

所以说ASP.NET MVC和WebForm都只不过是以不同的方式MVC罢了·····     那么三层架构又是怎么来的呢?三层架构是用来面对怎么的变化的呢?请听下回分解。