对三层解耦更进一步地理解

解耦之前

所有的代码都集中在controller中。

	@RequestMapping("/listEmp")
    public Result list(){
        //1. 加载并解析emp.xml
        String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
        System.out.println(file);
        List<Emp> empList = XmlParserUtils.parse(file, Emp.class);

        //2. 对数据进行转换处理 - gender, job
        empList.stream().forEach(emp -> {
            //处理 gender 1: 男, 2: 女
            String gender = emp.getGender();
            if("1".equals(gender)){
                emp.setGender("男");
            }else if("2".equals(gender)){
                emp.setGender("女");
            }

            //处理job - 1: 讲师, 2: 班主任 , 3: 就业指导
            String job = emp.getJob();
            if("1".equals(job)){
                emp.setJob("讲师");
            }else if("2".equals(job)){
                emp.setJob("班主任");
            }else if("3".equals(job)){
                emp.setJob("就业指导");
            }
        });

        //3. 响应数据
        return Result.success(empList);
    }

解耦之后

Controller类变得更加简洁
在大型企业中,各分其职,更加高效的完成开发。

//EmpController
package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.service.impl.EmpServiceA;
import com.itheima.utils.XmlParserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {

    private EmpService empService = new EmpServiceA();

    @RequestMapping("/listEmp")
    public Result list(){
        //1、调用service,获取数据
        List<Emp> empList = empService.listEmp();

        //2、响应数据
        return Result.success(empList);
    }
}

定义了一个EmpDao接口

//EmpDao
package com.itheima.dao;

import com.itheima.pojo.Emp;

import java.util.List;

public interface EmpDao {
    //获取员工列表数据
    public List<Emp> listEmp();
}

用EmpDaoA类实现该接口

//EmpDaoA
package com.itheima.dao.impl;

import com.itheima.dao.EmpDao;
import com.itheima.pojo.Emp;
import com.itheima.utils.XmlParserUtils;

import java.util.List;

public class EmpDaoA implements EmpDao {

    @Override
    public List<Emp> listEmp() {
        //加载并解析xml文件
        String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();//动态地获取一个文件的路径
        System.out.println(file);
        List<Emp> empList = XmlParserUtils.parse(file, Emp.class);     //ctrl+alt+v快捷键
        return empList;
    }
}

定义了一个EmpService接口

//EmpService
package com.itheima.service;

import com.itheima.pojo.Emp;

import java.util.List;

public interface EmpService {
    //获取员工数据
    public List<Emp> listEmp();
}

用EmpServiceA来实现该接口

//EmpServiceA
package com.itheima.service.impl;

import com.itheima.dao.EmpDao;
import com.itheima.dao.impl.EmpDaoA;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;

import java.util.List;

public class EmpServiceA implements EmpService {
    private EmpDao empDao = new EmpDaoA();

    @Override
    public List<Emp> listEmp() {
        //1、调用Dao,获取数据
        List<Emp> empList = empDao.listEmp();

        //2、数据转换格式
        empList.stream().forEach(emp -> {
            //处理gender
            String gender = emp.getGender();
            if ("1".equals(gender)){
                emp.setGender("男");
            } else if ("2".equals(gender)) {
                emp.setGender("女");
            }

            //处理职业
            String job = emp.getJob();
            if ("1".equals(job)){
                emp.setJob("讲师");
            }else if ("2".equals(job)){
                emp.setJob("班主任");
            }else if ("3".equals(job)){
                emp.setJob("就业指导");
            }
        });

        return empList;
    }
}

:为什么要使用接口??
:在 Java 中,接口(Interface)是一种完全抽象的类型,它定义了一组方法,但不提供这些方法的具体实现。接口的主要作用包括:
定义规范:接口规定了类应该遵循的方法签名,为类提供了一个公共的协议或规范。
实现多态:通过接口,可以允许不同的类实现相同的方法,从而实现运行时多态。
解耦合:接口使得类之间的依赖关系基于抽象而不是具体实现,有助于降低模块间的耦合度。(关键)
扩展性:使用接口可以方便地扩展功能,如果需要新的功能,只需添加新的方法到接口中,已有的实现类不需要修改。
规范设计:接口可以作为类设计的一部分,指导实现类遵循统一的设计风格和功能集。
简而言之,接口在 Java 中用于定义一组方法,它是一种设计契约,用于规范实现类的行为,并支持多态和解耦。
菜鸟教程:java的接口

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值