软考2016年上半年第六题(适配器模式)与手术训练系统项目适配器模式的应用

软考2016年上半年第六题

bash 复制代码
public class Address {
    public void street(){
        System.out.println("a");
    };
    public void zip(){};
    public void city(){};
}
bash 复制代码
package org.example.适配器模式;

/**
 * 适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。
 *
 * @author lst
 * @date 2023年12月01日 9:13
 */
public class DutchAddressAdapter extends DutchAdress {
    private (1);
    
	//原题没有这个参数
    private Boolean isHenan;

    /**
     * 这个适配器的设计思路是,当处理河南地址时,调用原先 DutchAdress 类的方法,而对于其他地址,调用 Address 接口的方法。适配器的作用是在两个不同的接口之间建立桥梁,使得它们可以协同工作,从而达到复用已有功能的目的。
     *
     * @param addr
     * @param isHenan
     * @return null
     * @author lst
     * @date 2023/12/1 9:53
     */
    public DutchAddressAdapter(Address addr) {
        address = addr;
    }

  //  public void straat() {
  //      if (isHenan) {
//			
//        } else {
 //           super.straat();  // 如果是河南,调用原先的继承方法
 //       }
//    }
  	public void straat() {
        (2)
    }
    public void postcode() {
        (3)
    }

    public void plaats() {
        (4)
    }
}
bash 复制代码
package org.example.适配器模式;

/**
 * @author lst
 * @date 2023年12月01日 9:11
 */
class DutchAdress {
    public void straat(){
        System.out.println("b");
    };
    public void postcode(){};
    public void plaats(){};
}
bash 复制代码
package org.example.适配器模式;

/**
 * @author lst
 * @date 2023年12月01日 9:21
 */
public class Text {
    public static void main(String[] args) {
        Address addr=new Address();
        (5)
        // DutchAddressAdapter addrAdapter=new DutchAddressAdapter(addr,false);
        System.out.println("\n The DutchAddress \n");
        testDutch(addrAdapter);
    }

    private static void testDutch(DutchAdress addr) {
        addr.straat();
        addr.postcode();
        addr.plaats();
    }
}

答案

(1)Address address

(2)address.street();

(3)address.zip();

(4)address.city();

(5)DutchAddressAdapter addrAdapter=new DutchAddressAdapter(addr)

适配器模式的应用

这里是对象的适配器模式的用法,这里的设计看起来是为了在 SpecRecorderService 接口中提供一个更特定的方法 getRecordByTaskIdFormatList,以适应控制器的需要,而 RecorderService 接口提供了更通用的方法。

作用就是前端要求传过去的数据类型是List替代Map<String, RemarkScoreEx>

java 复制代码
/**
 * 针对表user的数据库操作Service
 */
public interface RecorderService extends IService<Recorder> {
    /**
     * 根据任务id获取成绩
     *
     * @param taskId
     * @return java.util.Map<java.lang.String, com.wego.training.dto.RemarkScoreEx>
     * @author lst
     * @date 2023/10/7 9:38
     */
    Map<String, RemarkScoreEx> getRecordByTaskId(String taskId);
}

@Component
public class SpecRecorderServiceAdapter implements SpecRecorderService {
    private RecorderService recorderService;

    public SpecRecorderServiceAdapter(RecorderService recorderService) {
        this.recorderService = recorderService;
    }

    @Override
    public List<RemarkScoreEx> getRecordByTaskIdFormatList(String taskId) {
        return recorderService.getRecordByTaskId(taskId).values().stream().collect(Collectors.toList());
    }
}

@RestController
@RequestMapping("record")
public class RecordController {
    @Autowired
    private RecorderService recorderService;
    @GetMapping("getRecordByTaskIdFormatList")
    public CommonResult getRecordByTaskIdFormatList(String taskId) {
        SpecRecorderServiceAdapter specRecorderServiceAdapter = new SpecRecorderServiceAdapter(recorderService);
        List<RemarkScoreEx> list = specRecorderServiceAdapter.getRecordByTaskIdFormatList(taskId);
        return CommonResult.success(list);
    }
}
相关推荐
折哥的程序人生 · 物流技术专研5 天前
Java 23 种设计模式:从踩坑到精通 | 适配器模式 —— 让不兼容的接口也能一起工作
java·设计模式·面试·适配器模式·单一职责原则
basketball61614 天前
设计模式入门:3. 适配器模式详解 C++实现
c++·设计模式·适配器模式
晚风吹红霞14 天前
C++ stack 和 queue 完全指南:适配器模式与双端队列的奥秘
c++·算法·适配器模式
Rick199315 天前
代理模式 vs 适配器模式
代理模式·适配器模式
老码观察18 天前
设计模式实战解读(七):适配器模式——让不兼容的接口无缝协作
java·设计模式·适配器模式
nnsix1 个月前
设计模式 - 适配器模式 笔记
笔记·设计模式·适配器模式
雪碧聊技术1 个月前
什么是适配器模式?一文详解
适配器模式
蜡笔小马1 个月前
05.C++设计模式-适配器模式
c++·设计模式·适配器模式
c++之路1 个月前
适配器模式(Adapter Pattern)
java·算法·适配器模式
Forget the Dream1 个月前
基于适配器模式的 Axios 封装实践
设计模式·typescript·axios·适配器模式