外观模式封装系统的复杂性来提高程序的可用性,我们经常用的是数据访问外观类,它隔离了Ado.net的复杂性。
简单的外观例子
1using System;
2using System.Data;
3using System.Data.SqlClient;
4
5namespace Gof.Test.facade
6{
7 public class DB
8 {
9 public DB()
10 {}
11 public DataTable GetDataTable(string sql)
12 {
13 DataTable dt = new DataTable();
14 SqlConnection con = new SqlConnection();
15 SqlDataAdapter da = new SqlDataAdapter(sql,con);
16 try
17 {
18 if(con.State == ConnectionState.Closed)
19 {
20 con.Open();
21 }
22 da.Fill(dt);
23 return dt;
24 }
25 catch(Exception ex)
26 {
27 throw ex;
28 }
29 }
30 }
31}
在项目的开发过程中我们就可以通过重构来逐渐的丰富我们的外面类,从而使程序开发更加的简单。
1using System;
2using System.Data;
3using System.Data.SqlClient;
4
5namespace Gof.Test.facade
6{
7 public class DB
8 {
9 public DB()
10 {}
11 public DataTable GetDataTable(string sql)
12 {
13 DataTable dt = new DataTable();
14 SqlConnection con = new SqlConnection();
15 SqlDataAdapter da = new SqlDataAdapter(sql,con);
16 try
17 {
18 if(con.State == ConnectionState.Closed)
19 {
20 con.Open();
21 }
22 da.Fill(dt);
23 return dt;
24 }
25 catch(Exception ex)
26 {
27 throw ex;
28 }
29 }
30 }
31}
The Facade Pattern provides a interface to a set of interfaces in a subsystem. Facade defines a higherlevel interface that makes the subsystem easier to use.