Java 将抽象超类中的抽象方法作为子类中的静态方法

Java 将抽象超类中的抽象方法作为子类中的静态方法,java,inheritance,static,abstract-class,Java,Inheritance,Static,Abstract Class,我开发了一个自定义通信协议。在该协议中,每个数据包由一个报头和一个有效载荷组成。每个有效负载包含多个命令 我想在每个命令(Command0,…,Command N)中实现一个静态“decode”方法,因为相同类型的所有命令都以相同的方式进行解码(它们具有相同的字段结构)。我用摘要 类“Command”作为模板,其中包含一些常用字段和方法以及用于我的命令的抽象“decode”方法。但是由于超类方法是抽象的,Command0中的“decode”方法,…,CommandN不能是静态的 有工作吗?我希望

我开发了一个自定义通信协议。在该协议中,每个数据包由一个报头和一个有效载荷组成。每个有效负载包含多个命令

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

有工作吗?我希望避免每次必须解码某个数据包时都实例化每个命令

作为替代,我已经去掉了抽象的“decode”方法,并在每个扩展命令中包含了一个静态的“decode”方法。然而,如果没有超类或接口,另一个程序员可能会忘记实现解码方法。 此替代方案将导致以下代码:

{
    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.
}
公共类数据包
{
私有报头;
专用有效载荷;
公共静态数据包解码(连接dbConnection,字节[]数据,整数偏移量)引发异常
{
//...
}
}
公共类标题
{
公共静态头解码(字节[]数据,整数偏移)
{
//...
}
}
公共类有效载荷
{
私有列表命令;
公共静态有效负载解码(连接dbConnection、报头、字节[]数据、整数偏移)
{
//迭代数据字节以填充命令
}
}
公共抽象类命令
{
公共抽象命令decode(Connection dbConnection,Header Header,byte[]data,int offset)抛出异常;
}
公共类命令0
{
int field1;
字符串字段2;
浮子场3;
公共命令decode(Connection dbConnection,Header Header,byte[]data,int offset)引发异常;//我不能将其设置为静态,我希望设置为静态,因为Command0类的所有命令都以相同的方式进行解码。
}
//...
公共类命令
{
int field1;
地图领域2;
公共命令decode(Connection dbConnection,Header Header,byte[]data,int offset)引发异常;//我不能将其设为静态,我想这样做,因为CommandN类的所有命令都以相同的方式进行解码。
}

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

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;
    }

}

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

为什么你所有的方法都是静态的
Packet#header
Packet#payload
都是非静态的,那么为什么
Packet#decode
是静态的呢?因为decode返回从数据缓冲区读取的新数据包。我的意思是,仍然没有数据包结构,就像工厂方法一样。还有一个将对象转换为字节的编码“方法”。这不是静态的。您可能正在滥用静态工厂方法。如果
命令
是抽象的,则不能在类中直接使用静态工厂来实例化它。您需要使用类似于
Executors
的设计,它为不同的
ExecutorService
子类型公开静态工厂。
enum
答案不是真正可伸缩的,特别是当您需要数百条命令时。抽象(
命令
)不应该试图提供它自己的工厂,否则它将被迫知道它的具体实现(子类型),这是糟糕的设计。只需将该方法作为实例方法。应该避免使用静态方法。
Command c = CommandType.fromIdentifier(commandIdentifier).decodedecode(dbConnection, header, data, offset);