gpt4 book ai didi

java - Java 套接字编程中使用的观察者模式不起作用

转载 作者:行者123 更新时间:2023-12-01 12:55:32 25 4
gpt4 key购买 nike

当我尝试根据来自服务器的数据更新客户端 View 时,我的观察者模式出现问题。

向客户端发送消息的代码:

package model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class PlayerThread extends Thread{
private BufferedReader inFromClient;
private PrintWriter outToClient;
private Socket socket;

public PlayerThread(Socket socket) throws IOException{
this.socket = socket;
inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outToClient = new PrintWriter(socket.getOutputStream(), true);

outToClient.println("Hey");
}

public void run(){
// needs to be implemented
}
}

负责在客户端接收数据的代码:

package connection;

import java.io.BufferedReader;

import mediator.ModelManager;

public class ClientRecieverThread extends Thread {

private ModelManager model;
private BufferedReader inFromServer;

public ClientRecieverThread(ModelManager model, BufferedReader inFromServer) {
this.model = model;
this.inFromServer = inFromServer;
}

@Override
public void run() {

String message = null;
try {
while (true) {
message = inFromServer.readLine();
//System.out.println(message);
model.setMessage(message);
}
} catch (Exception e) {

}
}

}

以及包含我在客户端使用的观察者模式的代码:

package model;
public class MessageHandler {
private String message;

public MessageHandler(){
message = "Welcome";
}

public void setMessage(String message){
this.message = message;
}

public String getMessage(){
return this.message;
}
}

package mediator;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Observable;

import model.MessageHandler;
import connection.Proxy;

public class ModelManager extends Observable{
private MessageHandler messageHandler;
private Proxy proxy; // establish the connection with the server

public ModelManager() throws UnknownHostException, IOException{
proxy = new Proxy(this);
messageHandler = new MessageHandler();
}

public synchronized void setMessage(String message){
setChanged();
messageHandler.setMessage(message);
notifyObservers(message);
}

public String getMessage(){
return messageHandler.getMessage();
}

public void send(String message){
proxy.send(message);
}
}



package view;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import model.Board;

public class View extends JPanel implements Observer{
private JFrame frame;
private Board board;
private Rectangle[][] squares;
private JLabel messageLabel;

public View(){
initComponents();
initVariables();
addComponents();
}

private void initComponents() {
frame = new JFrame("TicTacToe");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

messageLabel = new JLabel("");
messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
messageLabel.setVerticalAlignment(SwingConstants.CENTER);

this.setSize(300,300);
}

private void initVariables() {
board = new Board(3,3, 100);

squares = new Rectangle[board.getRow()][board.getCol()];
for (int i = 0; i < squares.length; i++){
for (int j = 0; j < squares[i].length; j++){
squares[i][j] = new Rectangle();
}
}
}

private void addComponents() {
frame.add(messageLabel, BorderLayout.NORTH);
frame.add(this);

frame.setVisible(true);
}


private void drawBoard(Graphics g, Board board) {
int positionX = this.getWidth()/2 - (board.getCellSize() + board.getCellSize()/2);
int positionY = 50;

Graphics2D g2d = (Graphics2D) g;

for (int i = 0; i < board.getRow(); i++) {
for (int j = 0; j < board.getCol(); j++) {

squares[i][j] = new Rectangle(positionX, positionY, 100, 100);
positionX += 100;

g2d.draw(squares[i][j]);
}
positionY += board.getCellSize();
positionX = this.getWidth()/2 - (board.getCellSize() + board.getCellSize()/2);
}
}

public void paintComponent(Graphics g){
drawBoard(g, board);
}


public void update(Observable o, Object arg) {
System.out.println((String)arg);
}


}

ModelManagerView 之间的连接是在 Controller 类中完成的,如下所示 (model.addObserver(view) );

问题是我不明白为什么尽管客户端收到了数据,但 View 没有更新。

提前谢谢您! :)

最佳答案

当模型发生变化时,您需要调用方法notifyObservers();,这将通知添加到模型中的观察者。

因此,在 setMessage(String message) 中,进行编码:

if(this.message!=message){
this.message = message;
notifyObservers();
}

关于java - Java 套接字编程中使用的观察者模式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23942204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com