设计模式系列| 外观(门面)模式

狼王编程

共 5644字,需浏览 12分钟

 ·

2021-03-19 17:31

大家好,我是狼王,一个爱打球的程序员

这是设计模式的第九篇,这篇让我们来认识一下外观(门面)模式


1、概述

外观模式是一种结构型设计模式, 能为程序库、 框架或其他复杂类提供一个简单的接口。

避免多种不相关的功能污染单一外观, 使其变成又一个复杂结构。客户端和其他外观都可使用附加外观。

2、适用场景

1)如果你需要一个指向复杂子系统的直接接口, 且该接口的功能有限, 则可以使用外观模式。外观将会提供指向子系统中最常用功能的快捷方式, 能够满足客户端的大部分需求。

2)如果需要将子系统组织为多层结构, 可以使用外观。你可以为每个层次创建一个外观, 然后要求各层的类必须通过这些外观进行交互。

3、实例

有以下场景:

当前有学生子系统,该系统有三个接口,查询学生姓名,查询学生年龄,查询学生家庭地址。

有一个教学系统,要分别去调用这三个接口。

有一个成绩系统,要分别调用者三个接口。

有一个考试系统,也要分别调用这三个系统。

3.1 不使用外观模式时候

/**
 * 学生
 */

public class Student {

    private String name = "狼王";

    private int age = 25;

    private String address = "上海";

    public Student(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public Student(){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
/**
 * 学生地址
 */

@Service
public class StudentAddressService implements IStudentAddress{

    @Override
    public String getAddress() {
        Student student = new Student();
        return student.getAddress();
    }
}
/**
 * 年龄接口
 */

@Service
public class StudentAgeService implements IStudentAge{

    @Override
    public int getAge() {
        Student student = new Student();
        return student.getAge();
    }
}
@Service
public class StudentNameService implements IStudentName{

    @Override
    public String getName() {
        Student student = new Student();
        return student.getName();
    }
}

三个外部服务

/**
 * 教育服务
 */

@Service
public class EduService {

    @Autowired
    private StudentNameService studentNameService;

    @Autowired
    private StudentAgeService studentAgeService;

    @Autowired
    private StudentAddressService studentAddressService;

    public void getStudentName(){
        System.out.println("学生姓名是:" + studentNameService.getName());
    }

    public void getStudentAge(){
        System.out.println("学生年龄是:" + studentAgeService.getAge());
    }

    public void getStudentAddress(){
        System.out.println("学生地址是:" + studentAddressService.getAddress());
    }
}
/**
 * 考试服务
 */

@Service
public class ExamService {

    @Autowired
    private StudentNameService studentNameService;

    @Autowired
    private StudentAgeService studentAgeService;

    @Autowired
    private StudentAddressService studentAddressService;

    public void getStudentName(){
        System.out.println("学生姓名是:" + studentNameService.getName());
    }

    public void getStudentAge(){
        System.out.println("学生年龄是:" + studentAgeService.getAge());
    }

    public void getStudentAddress(){
        System.out.println("学生地址是:" + studentAddressService.getAddress());
    }
}
/**
 * 成绩服务
 */

@Service
public class ScoreService {

    @Autowired
    private StudentNameService studentNameService;

    @Autowired
    private StudentAgeService studentAgeService;

    @Autowired
    private StudentAddressService studentAddressService;

    public void getStudentName(){
        System.out.println("学生姓名是:" + studentNameService.getName());
    }

    public void getStudentAge(){
        System.out.println("学生年龄是:" + studentAgeService.getAge());
    }

    public void getStudentAddress(){
        System.out.println("学生地址是:" + studentAddressService.getAddress());
    }
}

3.2 使用外观模式

在学生服务这里增加一个外观service

/**
 * 外观模式服务
 */

@Service
public class StudentFacedService {

    @Autowired
    private StudentNameService studentNameService;

    @Autowired
    private StudentAgeService studentAgeService;

    @Autowired
    private StudentAddressService studentAddressService;

    public String getStudentName(){
        return studentNameService.getName();
    }

    public int getStudentAge(){
        return studentAgeService.getAge();
    }

    public String getStudentAddress(){
        return studentAddressService.getAddress();
    }

}

三个调用服务只需要引入外观服务

/**
 * 教育服务
 */

@Service
public class EduService {

    @Autowired
    private StudentFacedService studentFacedService;

    public void getStudentName() {
        System.out.println("学生姓名是:" + studentFacedService.getStudentName());
    }

    public void getStudentAge() {
        System.out.println("学生年龄是:" + studentFacedService.getStudentAge());
    }

    public void getStudentAddress() {
        System.out.println("学生地址是:" + studentFacedService.getStudentAddress());
    }
}
/**
 * 考试服务
 */

@Service
public class ExamService {

    @Autowired
    private StudentFacedService studentFacedService;

    public void getStudentName() {
        System.out.println("学生姓名是:" + studentFacedService.getStudentName());
    }

    public void getStudentAge() {
        System.out.println("学生年龄是:" + studentFacedService.getStudentAge());
    }

    public void getStudentAddress() {
        System.out.println("学生地址是:" + studentFacedService.getStudentAddress());
    }
}
/**
 * 成绩服务
 */

@Service
public class ScoreService {

    @Autowired
    private StudentFacedService studentFacedService;

    public void getStudentName() {
        System.out.println("学生姓名是:" + studentFacedService.getStudentName());
    }

    public void getStudentAge() {
        System.out.println("学生年龄是:" + studentFacedService.getStudentAge());
    }

    public void getStudentAddress() {
        System.out.println("学生地址是:" + studentFacedService.getStudentAddress());
    }
}
4、分析

以上两种方式代码结构如下所示:

77ebde99040eca2ec6064f0be7db4e4d.webp

654f48c81d9cf3c9cdd64928e2e185d6.webp

从上面两张图可以看到,对于外部服务来说,极大的缩减了代码复杂度,只需要调用学生服务的一个接口。

5、总结

优点:

让客户端代码独立独立于复杂的子系统,且减少对于子系统的依赖。

缺点:

过于庞大的外观,会使得该外观称成为上帝对象,造成所有类的耦合,可通过它操作所有的类功能。

好了。今天就说到这了,我还会不断分享自己的所学所想,希望我们一起走在成功的道路上!

乐于输出干货的Java技术公众号:狼王编程。公众号内有大量的技术文章、海量视频资源、精美脑图,不妨来关注一下!回复资料领取大量学习资源和免费书籍!

转发朋友圈是对我最大的支持!
38af0cc0631fb2c9b47b382482e1fc7e.webp 觉得有点东西就点一下“赞和在看”吧!感谢大家的支持了!



浏览 38
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报