软考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);
    }
}
相关推荐
S-X-S1 天前
策略+适配器模式详解
java·开发语言·适配器模式
kk\n2 天前
C++ -- stack的模拟实现 && 介绍适配器模式
开发语言·c++·适配器模式
LuckyLay2 天前
Golang学习笔记_32——适配器模式
笔记·学习·golang·适配器模式
好好学习++3 天前
【HF设计模式】07-适配器模式 & 外观模式
java·c++·设计模式·适配器模式·外观模式
ThereIsNoCode4 天前
「软件设计模式」适配器模式(Adapter)
c++·设计模式·适配器模式
管大虾7 天前
设计模式-适配器模式
java·设计模式·适配器模式
鎈卟誃筅甡8 天前
JavaScript设计模式 -- 适配器模式
设计模式·适配器模式
小王子102415 天前
设计模式Python版 适配器模式
python·设计模式·适配器模式
王·小白攻城狮·不是那么帅的哥·天文21 天前
适配器模式
java·适配器模式
Cikiss22 天前
「全网最细 + 实战源码案例」设计模式——适配器模式
java·后端·设计模式·适配器模式