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

时间:2017-05-16 14:08:35

标签: java inheritance static abstract-class

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

我想在每个Command(Command0,...,Command N)中实现静态'decode'方法,因为所有相同类型的命令都以相同的方式解码(它们具有相同的字段结构)。我用了一个摘要 class'Command'作为模板,带有一些常见的字段和方法,以及一个抽象的'decode'方法用于我的命令。但由于超类方法是抽象的,Command0,...,CommandN中的'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.
}

1 个答案:

答案 0 :(得分:1)

您可以拥有一个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);

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