Java 核心技术Java 进阶java学习快到碗里来

设计模式(十一)外观(门面)模式

2021-01-11  本文已影响0人  我犟不过你

1、概述

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

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

2、适用场景

1)如果你需要一个指向复杂子系统的直接接口, 且该接口的功能有限, 则可以使用外观模式。
外观将会提供指向子系统中最常用功能的快捷方式, 能够满足客户端的大部分需求。
2)如果需要将子系统组织为多层结构, 可以使用外观。
你可以为每个层次创建一个外观, 然后要求各层的类必须通过这些外观进行交互。

3、实例

有以下场景:

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

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

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

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

3.1 不使用外观模式时候的代码:

/**
 * 学生
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
public class Student {

    private String name = "张三";

    private int age = 11;

    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;
    }
}
/**
 * 学生地址
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@Service
public class StudentAddressService implements IStudentAddress{

    @Override
    public String getAddress() {
        Student student = new Student();
        return student.getAddress();
    }
}
import org.springframework.stereotype.Service;

/**
 * 年龄接口
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@Service
public class StudentAgeService implements IStudentAge{

    @Override
    public int getAge() {
        Student student = new Student();
        return student.getAge();
    }
}
import org.springframework.stereotype.Service;

/**
 * TODO
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@Service
public class StudentNameService implements IStudentName{

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

三个外部服务:

import com.cloud.bssp.designpatterns.faced.withoutdesign.student.StudentAddressService;
import com.cloud.bssp.designpatterns.faced.withoutdesign.student.StudentAgeService;
import com.cloud.bssp.designpatterns.faced.withoutdesign.student.StudentNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 教育服务
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@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());
    }
}
import com.cloud.bssp.designpatterns.faced.withoutdesign.student.StudentAddressService;
import com.cloud.bssp.designpatterns.faced.withoutdesign.student.StudentAgeService;
import com.cloud.bssp.designpatterns.faced.withoutdesign.student.StudentNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 考试服务
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@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());
    }
}
/**
 * 成绩服务
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 外观模式服务
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@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();
    }

}

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

import com.cloud.bssp.designpatterns.faced.usedesign.student.StudentFacedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 教育服务
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@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());
    }
}
import com.cloud.bssp.designpatterns.faced.usedesign.student.StudentFacedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 考试服务
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@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());
    }
}
import com.cloud.bssp.designpatterns.faced.usedesign.student.StudentFacedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 成绩服务
 * @date: 2021/1/11
 * @author weirx
 * @version 3.0
 */
@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、分析

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


不使用外观
外观模式

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

5、总结

优点:
让客户端代码独立独立于复杂的子系统,且减少对于子系统的依赖。
缺点:
过于庞大的外观,会使得该外观称成为上帝对象,造成所有类的耦合,可通过它操作所有的类功能。

上一篇 下一篇

猜你喜欢

热点阅读