软考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);
    }
}
相关推荐
学步_技术3 天前
Python编码系列—Python适配器模式:无缝集成的桥梁
开发语言·python·适配器模式
努力编程的阿伟4 天前
二十三种设计模式之适配器模式
设计模式·适配器模式
coffee_baby9 天前
适配器模式详解和源码中应用
android·java·适配器模式
磊-10 天前
八、适配器模式
适配器模式
AI让世界更懂你10 天前
漫谈设计模式 [6]:适配器模式
python·设计模式·适配器模式
weixin_4177599910 天前
77-java 装饰器模式和适配器模式区别
java·适配器模式·装饰器模式
Aloha_up14 天前
适配器模式 adapter
适配器模式
认真的小羽❅17 天前
设计模式 -- 适配器模式(Adapter Pattern)
java·设计模式·适配器模式
犬余17 天前
设计模式之适配器模式:软件世界的桥梁建筑师
java·设计模式·适配器模式
我是回頭呀18 天前
设计模式结构型模式之适配器模式
android·设计模式·适配器模式