开发者

Android Java - switch issue

I'm working on a test application which requires connection to server and save the response from it. The response actually is a data packet,so I need to understand what type of packet it is and to execute it.For now I can receive the response, calculate the data packet and now I need a little help with switch method where I want to get the packet type.

Here is the code I'm using :

                    RPCPacket packet=null;
                    switch(RPCPacketType.getPacketTypeByValue(pType)){
                        case ST_OBJECT_TYPE_INFO_START:
                        {
                            packet = new InfoStartRPCPacket(    objectIdentificator,
                                                                RPCPacketType.getPacketTypeByValue(pType),
                                                                RPCOperationType.getByValue(Integer.parseInt(operation)),
                                                                objectId,
                                                                id,
                                                                Integer.parseInt(size),
                                                                hash,
                                                                RPCPacketDataType.getByValue(dataType),
                                                                first );
                            break;
                        }
                        case ST_OBJECT_TYPE_INFO_END:{
                            packet = new InfoEndRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_INFO_ERROR:{
                            packet = new InfoErrorRPCPacket();

                            break;
                        }
                            // Basic packets
                        case ST_OBJECT_TYPE_COLLECTION:{
                            packet = new CollectionRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_CATEGORY:{
                            packet = new CategoryRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_CARD:{
                            packet = new CardRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MESSAGE:{
                            packet = new MessageRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_GENRE:{
                            packet = new GenreRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_TAG:{
                            packet = new TagRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_USER:{
                            packet = new UserRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_CARDSTATS_CATEGORY:{
                            packet = new CardStatsCategoryRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_CONTENT:{
                    开发者_如何学JAVA        packet = new ContentRPCPacket();

                            break;
                        }
                            // Media packets
                        case ST_OBJECT_TYPE_MEDIA_COLLECTION:{
                            packet = new MediaCollectionRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MEDIA_CATEGORY:{
                            packet = new MediaCategoryRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MEDIA_CARD:{
                            packet = new MediaCardRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MEDIA_TAG:{
                            packet = new MediaTagRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MEDIA_COLLECTION_BUTTON:{
                            packet = new MediaCollectionButtonRPCPacket();

                            break;
                        }
                            // unknown packet
                        default: {
                            packet=null;

                            break;
                        }
                    }

I need a way to be able to initialize the packet as InfoStartRPCPacket or something different after I check the all switches.Basically I want to be able to do something like this outside switch :

packet.executeInfoStartPacket(/*params*/);  , when executeInfoStartPacket is a method in InfoStartRPCPacket class.

Any suggestions how to do that?


You can't really do that and you shouldn't.

Instead you could have an abstract method in your RPCPacket class called process() (possibly giving any necessary context as parameters) and overload that in each specific sub-class.

This way the packet-specific code automatically has access to all the packet-specific fields and methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜