东莞网站关键词优化公司,学建设网站去哪里学,wordpress站点进入时弹窗,天津企业网站建设一般多少钱说到外观模式#xff0c;很容易想到的是设计一件漂亮的衣服然后穿上自己的身上#xff0c;让自己看起来更加的漂亮#xff0c;但是这个可能并不是这样子的#xff0c;从更深层次的来说#xff0c;外观更应该是所见即所得的#xff0c;对于观众来说#xff0c;看起来可能… 说到外观模式很容易想到的是设计一件漂亮的衣服然后穿上自己的身上让自己看起来更加的漂亮但是这个可能并不是这样子的从更深层次的来说外观更应该是所见即所得的对于观众来说看起来可能就是很简单但是里面所有的东西的复杂程度我们并不知道。 在程序开发的过程中我们时常的会用到一些类与类之间的关联关系或者直接通过一个操作来实现多个事情那么怎样做到呢很容易想到我们可以在一个方法里面实现多种行为将这些方法放在一个类中这个类就成了我们的外观类在进行与外界交互的时候我们不需要再进行复杂的操作直接调用外观类里面的方法就能实现了。 说了这么多那么什么是外观模式呢外观模式为子系统的一组接口提供了一个统一的界面此模式定义了一个高级的接口这个接口使得这一子系统更加容易使用。 外观模式的主要用于解耦、减少依赖关系、为新旧系统交互提供接口下面看一下外观模式的UML图 通过上图我们可以看出减少了Client与子系统的依赖关系降低了Client与子系统之间的耦合度同时Fecade也充当了接口的作用下面我们通过外观模式实现三层的表现层与业务逻辑层的解耦 三层UML图 首先建立Models层并添加相应的类包括Student类、Grade类、StudentAndGrade类代码如下 namespace Demo.Models
{public class Student{public int ID { get; set; }public string Name { get; set; }public int Age { get; set; }public int GradeID { get; set; }}
} Student namespace Demo.Models
{public class Grade{public int GradeID { get; set; }public string GradeName { get; set; }}
} Grade namespace Demo.Models
{public class StudentAndGrade{public Student stu { get; private set; }public Grade grade { get; private set; }public StudentAndGrade(int StuID, string Name, int Age, int GradeID, string GradeName){stu new Student() { ID StuID, Name Name, Age Age };grade new Grade() { GradeID GradeID, GradeName GradeName };}}
} StudentAndGrade 接着建立DAL层用于实现对Student和Grade的增加和查询包含StudentDAL类、GradeDAL类代码如下 namespace Demo.DAL
{/// summary/// 和数据库进行交互的先使用集合进行模拟/// /summarypublic class StudentDAL{private static ListStudent List new ListStudent();public ListStudent GetAllStudents(){return List;}public bool Add(Student stu){try{List.Add(stu);return true;}catch (Exception){return false;}}}
} StudentDAL namespace Demo.DAL
{public class GradeDAL{private ListGrade List new ListGrade(){//new Grade() { GradeID1, GradeName C#},//new Grade() { GradeID2,GradeNameADO.Net},//new Grade() { GradeID3,GradeNameAsp.Net}};public ListGrade GetAllGrades(){return List;}public bool Add(Grade grade){try{List.Add(grade);return true;}catch (Exception){return false;throw;}}}
} GradeDAL 然后建立Facade层定义FacadePattern类实现添加StudentAndGrade对象的方法并且实现获取所有所有的学生、年级信息的方法代码如下 namespace Demo.Facade
{public class FacadePattern{private StudentBLL studentBLL null;private GradeBLL gradeBLL null;public FacadePattern(){studentBLL new StudentBLL();gradeBLL new GradeBLL();}public void Add(StudentAndGrade StudentAndGrade){studentBLL.Add(StudentAndGrade.stu);gradeBLL.Add(StudentAndGrade.grade);}public ListStudent GetAllStudents(){return studentBLL.GetAllStudents();}public ListGrade GetAlllGrades(){return gradeBLL.GetAllGrades();}}
} FacadePattern 最后建立UI层用于数据的添加和展示具体代码如下 class Program{static void Main(string[] args){StudentAndGrade StudentAndGrade new StudentAndGrade(1, 张三, 20, 1, 大一新生);FacadePattern facade new FacadePattern();facade.Add(StudentAndGrade);ListStudent stuList facade.GetAllStudents();foreach (Student stu in stuList){Console.WriteLine({0},{1},{2}, stu.ID, stu.Name, stu.Age);}ListGrade gradeList facade.GetAlllGrades();foreach (Grade grade in gradeList){Console.WriteLine({0},{1}, grade.GradeID, grade.GradeName);}Console.ReadKey();}} Main 整个代码已经实现了表现层与业务逻辑层的解耦减少了它们之间的依赖关系同时说明了Facade也能作为新旧系统之间的接口使用这也是外观模式的典型应用。 本篇文章外观设计模式至此谢谢您收看我的博客。 转载于:https://www.cnblogs.com/mointor/p/5169564.html