Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns 面向对象设计查询适配器_Design Patterns_Adapter_Ooad - Fatal编程技术网

Design patterns 面向对象设计查询适配器

Design patterns 面向对象设计查询适配器,design-patterns,adapter,ooad,Design Patterns,Adapter,Ooad,我有一个关于适配器模式的问题,即如何在这个场景中实现。 我有一个接口,返回类型是单对象 public interface MyInt { MyObj read(); } 然而,我的adaptee实现类说,MyAdaptee有返回MyObj对象列表的方法 public class MyAdaptee { public MyObj[] readTheInput() { // implementation here } 现在,我如何在MyAdaptee之上编写适配器? 既然我不能更改接口,我如何才

我有一个关于适配器模式的问题,即如何在这个场景中实现。 我有一个接口,返回类型是单对象

public interface MyInt {
MyObj read();
}
然而,我的adaptee实现类说,MyAdaptee有返回MyObj对象列表的方法

public class MyAdaptee {
public MyObj[] readTheInput() {
// implementation here
}
现在,我如何在MyAdaptee之上编写适配器? 既然我不能更改接口,我如何才能将MyObj的多个对象发送到需要单个对象的客户端


PS:MyObj也是接口。

您需要使用其语义实现适配器,因此,可能必须修改Adaptee实现,以便它通过跟踪先前返回的内容,逐个返回数组项

下面是指示性的实现,我并没有编译或测试它,所以把它当作它所代表的逻辑

public class MyAdaptee implements MyInt {
     private MyObj[] buffer = new MyObj[0];
     private indexOfLastBufferedItemReturned = 0;

  @Override
  public MyObj read() {
     if (indexOfLastBufferedItemReturned >= buffer.length) {
        buffer = readTheInput(); //use existing implementation here
        indexOfLastBufferedItemReturned = 0;
     }
     return buffer[indexOfLastBufferedItemReturned++];   
  }

  public MyObj[] readTheInput() {    
     // implementation here   
  }   

}

谢谢,但如何将多个对象返回给客户端?例如,reaTheInput()返回三个MyObj,现在,一旦您将第一个索引返回到适配器并将适配器发送到客户端,之后,您将如何再次发送到适配器(和客户端)?很抱歉,如果我遗漏了什么?客户端将多次调用该方法,因为客户端只知道MyInt,它支持一次读取一个MyObj,除非它仍然有元素要从以前的readInput调用返回,只有当缓冲区为空时,它才应该调用readInput以获取更多myobjsorry,我删除我的注释。非常感谢你的帮助\