gpt4 book ai didi

java - 来自抽象父类(super class)的抽象方法作为子类中的静态方法

转载 作者:行者123 更新时间:2023-12-02 13:09:58 24 4
gpt4 key购买 nike

我开发了一个自定义通信协议(protocol)。在此协议(protocol)中,每个数据包由 header 和有效负载组成。每个有效负载包含多个命令。

我想在每个命令(Command0,...,命令 N)中实现静态“解码”方法,因为相同类型的所有命令都以相同的方式解码(它们具有相同的字段结构)。我使用摘要类“Command”作为模板,其中包含一些常见字段和方法以及用于我的命令的抽象“解码”方法。但由于父类(super class)方法是抽象的,Command0、...、CommandN 中的“解码”方法不能是静态的。

有什么解决办法吗?我想避免每次必须解码某些数据包时都实例化每个命令。

作为替代方案,我摆脱了抽象的“解码”方法,并在每个扩展 Command 的命令中包含静态“解码”方法。但是,如果没有父类(super class)或接口(interface),其他程序员可能会忘记实现解码方法。这种替代方案将导致以下代码:

{
switch(commandIdentifier)
{
case 0:
{
Command0 command0 = Command0.decode(dbConnection, header, data, offset);
payload.getCommands().add(command0);
break;
}
//...
case N:
{
CommandN commandN = CommandN.decode(dbConnection, header, data, offset);
payload.getCommands().add(commandN);
break;
}
default:
{
//some code
}
}
}

我首先必须检查命令标识符。

我最初是这样实现这些类的:

public class Packet
{
private Header header;
private Payload payload;

public static Packet decode(Connection dbConnection, byte[] data, int offset) throws Exception
{
//...
}
}

public class Header
{
public static Header decode(byte[] data, int offset)
{
//...
}
}

public class Payload
{
private List<Command> commands;
public static Payload decode(Connection dbConnection, Header header, byte[] data, int offset)
{
//iterate over the data bytes to populate commands
}
}

public abstract class Command
{
public abstract Command decode(Connection dbConnection, Header header, byte[] data, int offset) throws Exception;
}

public class Command0
{
int field1;
String field2;
float field3;
public Command decode(Connection dbConnection, Header header, byte[] data, int offset) throws Exception; //I can't make it static and I'd like to because all commands of class Command0 are decoded the same way.
}

//...

public class Command N
{
int field1;
Map<Integer, ConfigBean> field2;
public Command decode(Connection dbConnection, Header header, byte[] data, int offset) throws Exception; //I can't make it static and I'd like to because all commands of class CommandN are decoded the same way.
}

最佳答案

您可以有一个 enum 类,其中包含与命令是什么以及如何解码它相关的信息。使用抽象方法而不是静态方法将迫使您为每个方法实现解码:

public enum CommandType {
COMMAND_0(0) {
@Override
public <T extends Command> T decode(Connection dbConnection, ResponseBuilder.Header header, byte[] data, int offset) throws Exception {
// decode Command 0
...
}
},
COMMAND_1(1) {
@Override
public <T extends Command> T decode(Connection dbConnection, ResponseBuilder.Header header, byte[] data, int offset) throws Exception {
// decode Command 1
...
}
}
...
COMMAND_N(N) {
@Override
public <T extends Command> T decode(Connection dbConnection, ResponseBuilder.Header header, byte[] data, int offset) throws Exception {
// decode Command N
...
}
}
;

private final int commandIdentifier;

CommandType(int commandIdentifier) {
this.commandIdentifier = commandIdentifier;
}

public abstract <T extends Command> T decode(Connection dbConnection, ResponseBuilder.Header header, byte[] data, int offset) throws Exception;


private static Map<Integer, CommandType> map = new HashMap<Integer, CommandType>();
static {
for (CommandType commandType : CommandType.values()) {
if (map.get(commandType.commandIdentifier) != null)
throw new IllegalStateException("There are several commands with the same identifier");
map.put(commandType.commandIdentifier, commandType);
}
}

public static CommandType fromIdentifier(int commandIdentifier) throws IllegalArgumentException {
CommandType commandType = map.get(commandIdentifier);
if (commandType == null)
throw new IllegalArgumentException("Unkown command identifier");
return commandType;
}

}

之后您可以简单地使用:

Command c = CommandType.fromIdentifier(commandIdentifier).decodedecode(dbConnection, header, data, offset);

我认为这是一种更时尚的处理方式

关于java - 来自抽象父类(super class)的抽象方法作为子类中的静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003817/

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