我陷入了一个心理循环,或者至少对如何实现观察者模式缺乏了解。我打算Controller
实现的目的是Observer
因为它实际上是为了观察它所控制的实例。
被观察到的对象之所以扩展,是Observable
因为它们意味着被观察,字面意思是,被Controller
. 我调用.addObserver(responseHandler);
什么?另一个类向实例添加了一个观察者,从字面上看,它实现了“观察者”接口?这不可能。
------edit----- responseHandler 只是一些未知控制器的通用名称
顺便说一句,这种模式在 Java 中实现是否存在命名问题,或者只是我缺乏理解?
这是控制器,它现在已经变成了控制器的控制器:
public class MetaController implements Observer {
private final static Logger LOG = Logger.getLogger(MetaController.class.getName());
private Printer telnetPrinter = new Printer();
private telnetDataProcessor telnetDataProcessor = new telnetDataProcessor();
private StringReader stringReader = new StringReader();
private final ConcurrentLinkedQueue<Character> telnetData = new ConcurrentLinkedQueue();
public MetaController() {
}
//the printer and processor each spawn their own thread so that they don't
//block each other waiting for each other
public void readPrintParse(final InputStream inputStream) throws SocketException, IOException {
telnetPrinter.print(inputStream, telnetData); //populate telnetData in its own thread
telnetDataProcessor.read(telnetData); //process telnetData in its own thread
}
//the StringReader just looks for particular keywords like "press enter to continue"
//so that some phrases will trigger a response
//commands may also be typed manually by the user (not implemented yet)
@Override
public void update(Observable o, Object arg) {
//when telnetDataProcessor sends a String, send that String on to stringReader
String cmd = stringReader.parse(null); //parse what and how?
//send the command string back to the telnetClient
}
}
不幸的是,直接阅读API对我没有启发。
这是 ApacheWeatherTelnet
示例的扩展,是一种穷人 Netty,允许对简单的MUD 客户端进行并发脚本远程登录和实时远程登录响应,这需要实时处理未终止的远程登录数据流,以及实时输出和用户输入。