外观模式(三层解耦)

  说到外观模式,很容易想到的是设计一件漂亮的衣服然后穿上自己的身上,让自己看起来更加的漂亮,但是这个可能并不是这样子的,从更深层次的来说,外观更应该是所见即所得的,对于观众来说,看起来可能就是很简单,但是里面所有的东西的复杂程度,我们并不知道。

  在程序开发的过程中,我们时常的会用到一些类与类之间的关联关系,或者直接通过一个操作来实现多个事情,那么怎样做到呢,很容易想到,我们可以在一个方法里面实现多种行为,将这些方法放在一个类中,这个类就成了我们的外观类,在进行与外界交互的时候,我们不需要再进行复杂的操作,直接调用外观类里面的方法就能实现了。

  说了这么多,那么什么是外观模式呢?外观模式,为子系统的一组接口提供了一个统一的界面,此模式定义了一个高级的接口,这个接口使得这一子系统更加容易使用。

  外观模式的主要用于解耦、减少依赖关系、为新旧系统交互提供接口,下面看一下外观模式的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>
    /// 和数据库进行交互的,先使用集合进行模拟
    /// </summary>
    public class StudentDAL
    {
        private static List<Student> List = new List<Student>();
        public List<Student> 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 List<Grade> List = new List<Grade>()
        {
            //new Grade() {  GradeID=1, GradeName= "C#"},
            //new Grade() {  GradeID=2,GradeName="ADO.Net"},
            //new Grade() { GradeID=3,GradeName="Asp.Net"}
        };

        public List<Grade> 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 List<Student> GetAllStudents()
        {
            return studentBLL.GetAllStudents();

        }
        public List<Grade> 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);

            List<Student> stuList = facade.GetAllStudents();
            foreach (Student stu in stuList)
            {
                Console.WriteLine("{0},{1},{2}", stu.ID, stu.Name, stu.Age);
            }

            List<Grade> 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