Oi pessoal,
Tenho um trabalho para fazer em Java do jogo do hangman e tenho de seguir as regras que o professor definiu, isto é, tenho de usar reactor pattern.
Mas o meu problema é que nunca programei assim e não estou a perceber as coisas muito bem. O professor deu estas 3 classes já que servem de base.
Dispatccher
package reactor;
import reactorapi.*;
public class Dispatcher {
public Dispatcher() {
}
public void handleEvents() throws InterruptedException {
}
public void addHandler(EventHandler<?> h) {
}
public void removeHandler(EventHandler<?> h) {
}
}
Depois tenho EventHandler
package reactorapi;
public interface EventHandler<T> {
public Handle<T> getHandle();
public void handleEvent(T s);
}
Handle
package reactorapi;
public interface Handle<T> {
public T read();
}
Por fim o professor deu ainda um exemplo supostamente para ajudar a perceber o funcionamento deste mecanismo
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package reactorexample;
import reactor.*;
import reactorapi.*;
import java.util.Random;
/**
* Really simple Reactor usage example that creates two Handles that send
* strings one character at a time and event handlers that collect these
* characters and finally print them.
*
* The purpose of this class is to provide a self-contained trivial test.
*
* Expected output: "Reactor test completed OK."
*/
public class ReactorExample {
Dispatcher d=new Dispatcher();
StringHandler sh1, sh2;
Random r=new Random();
String expected="Reactor test completed OK.";
public static void main(String[] args) {
for(int i=0; i<50; i++)
new ReactorExample().execute();
System.err.println("\nNo errors found; Reactor at least minimally compliant.");
}
public ReactorExample() {
sh1=new StringHandler("Reactor test ");
d.addHandler(sh1);
sh2=new StringHandler("completed OK.");
d.addHandler(sh2);
}
public void execute() {
/* Main loop. */
try {
d.handleEvents();
} catch(InterruptedException ie) {
return;
}
String result="a"+sh1+sh2;
if (!result.equals(expected)) {
System.err.println("Reactor test failed, expected:");
System.err.println(expected);
System.err.println("got:");
System.err.println(result);
System.exit(1);
}
System.err.print(".");
}
/**
* Handle that returns characters one at a time slowly from a
* creator-supplied String.
*/
public class StringHandle implements Handle<Character> {
int position=0;
String data;
public StringHandle(String s) {
data=s;
}
public Character read() {
System.out.println("Entrei");
try {
Thread.sleep(r.nextInt(50));
} catch(InterruptedException ie) {}
if (position>=data.length())
return null;
System.err.println("Returning: "+data.charAt(position));
return new Character(data.charAt(position++));
}
}
/**
* Event handler that receives characters from a (creator-supplied)
* StringHandle and collects them in a String.
*/
public class StringHandler implements EventHandler<Character> {
StringHandle sh;
StringBuffer sb=new StringBuffer();
public StringHandler(String s) {
sh=new StringHandle(s);
}
public StringHandle getHandle() {
return sh;
}
public void handleEvent(Character s) {
if (s==null) {
d.removeHandler(this);
} else
sb.append(s);
}
public String toString() {
return sb.toString();
}
}
}
No entanto o meu código do hangaman tem de funcionar com qualquer outra class do dispatcher de outro qualquer grupo.
Mas eu nessa class não tenho que adicionar algo aos métodos que estão lá? Porque o método está definido mas não tem “corpo”.
No exemplo do professor segundo percebo ele cria uma instância do Dispatcher e depois adiciona os dois eventos, mas quando
ele faz o d.handleEvents(); isto vai fazer alguma coisa?
Obrigado desde já a todos que poderem ajudar e desculpa o longo tópico.