W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵(lì)
Hello,大家好,我是 V 哥
命令模式是一種行為設(shè)計(jì)模式,它將一個(gè)請求封裝為一個(gè)對象,從而允許用戶使用不同的請求、隊(duì)列或日志請求來參數(shù)化其他對象。命令模式也支持可撤銷的操作。
命令模式通常與其他設(shè)計(jì)模式結(jié)合使用,如策略模式、觀察者模式、狀態(tài)模式等,以構(gòu)建一個(gè)靈活、可擴(kuò)展和易于維護(hù)的系統(tǒng)。
創(chuàng)建一個(gè)完整的手游后端服務(wù)端示例涉及到許多組件,包括網(wǎng)絡(luò)通信、數(shù)據(jù)庫交互、業(yè)務(wù)邏輯處理等。在這里,我將提供一個(gè)非常簡化的示例,它模擬了一個(gè)基本的游戲后端服務(wù),包括玩家注冊、登錄和獲取玩家信息的功能。這個(gè)示例將使用Java的Socket編程來處理客戶端請求。
注意:這個(gè)示例僅用于教學(xué)目的,實(shí)際的手游后端會更加復(fù)雜,需要考慮安全性、并發(fā)性、數(shù)據(jù)庫存儲、錯(cuò)誤處理等多個(gè)方面。
public class Player {
private String id;
private String username;
private String password;
public Player(String id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
// Getters and setters
public String getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
import java.util.HashMap;
import java.util.Map;
public class PlayerService {
private Map<String, Player> players = new HashMap<>();
public synchronized String registerPlayer(String username, String password) {
String playerId = Integer.toString(players.size() + 1);
players.put(playerId, new Player(playerId, username, password));
return playerId;
}
public synchronized Player login(String username, String password) {
for (Player player : players.values()) {
if (player.getUsername().equals(username) && player.getPassword().equals(password)) {
return player;
}
}
return null;
}
public synchronized Player getPlayer(String playerId) {
return players.get(playerId);
}
}
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class GameServer {
private ServerSocket serverSocket;
private final int PORT = 12345;
private PlayerService playerService = new PlayerService();
public void startServer() {
try {
serverSocket = new ServerSocket(PORT);
System.out.println("Game server is running on port " + PORT);
while (true) {
Socket socket = serverSocket.accept();
new Handler(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private class Handler extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Handler(Socket socket) {
this.socket = socket;
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received: " + inputLine);
if ("REGISTER".equals(inputLine)) {
String playerId = playerService.registerPlayer("username", "password");
out.println("REGISTERED " + playerId);
} else if ("LOGIN".equals(inputLine)) {
Player player = playerService.login("username", "password");
if (player != null) {
out.println("LOGIN_SUCCESS " + player.getId());
} else {
out.println("LOGIN_FAILURE");
}
} else if ("GET_PLAYER".equals(inputLine)) {
String[] parts = inputLine.split(" ", 2);
if (parts.length == 2) {
Player player = playerService.getPlayer(parts[1]);
if (player != null) {
out.println("PLAYER " + player.getId() + " " + player.getUsername());
} else {
out.println("PLAYER_NOT_FOUND");
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
GameServer server = new GameServer();
server.startServer();
}
}
import java.io.*;
import java.net.Socket;
public class GameClient {
private Socket socket;
private PrintWriter out;
private BufferedReader in;
public GameClient(String serverAddress, int port) {
try {
socket = new Socket(serverAddress, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendCommand(String command) {
out.println(command);
}
public String readResponse() {
try {
return in.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public void close() {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
GameClient client = new GameClient("localhost", 12345);
// 注冊玩家
client.sendCommand("REGISTER");
String registerResponse = client.readResponse();
System.out.println("Register response: " + registerResponse);
// 登錄玩家
client.sendCommand("LOGIN");
String loginResponse = client.readResponse();
System.out.println("Login response: " + loginResponse);
// 獲取玩家信息
String[] loginParts = loginResponse.split(" ", 2);
if (loginParts.length == 2 && "LOGIN_SUCCESS".equals(loginParts[0])) {
client.sendCommand("GET_PLAYER " + loginParts[1]);
String playerResponse = client.readResponse();
System.out.println("Player info response: " + playerResponse);
}
// 關(guān)閉連接
client.close();
}
}
以上是本文的全部內(nèi)容筆記,感謝老鐵們的支持和鼓勵(lì),不當(dāng)之處還請不吝賜教,歡迎關(guān)注威哥愛編程,努力的人相信總會有回報(bào)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: