减少选择语句 [英] Reducing A Select Statement

查看:70
本文介绍了减少选择语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是select语句中三个选择的摘要.有利地,将会有更多.答案将始终是相同的(IP地址,端口也许"),我不在乎它是UDP,TCP还是什么,我只需要回退"IP地址",也可以放回端口" ".我该怎么办?

This is just a snippet of three choices on a select statement. Evantually there will be a lot more. The answer will always be the same (IP Address, Port"Maybe"), I don''t care if it''s UDP, TCP or what, I just need to kick back the "IP Address" and maybe the "Port". How can I do this?

<pre lang="cs">// Read http://www.ietf.org/rfc/rfc1700.txt?number=1700<br />
      switch( ip_header->protocol )<br />
      {<br />
         case 1: // ICMP<br />
            if ( bShowICMP )<br />
            {<br />
             printf("\n         ICMP Source IP  : %s", ipSrc);<br />
             decode_icmp(&packet[ip_header_size]);<br />
            }<br />
            break;<br />
         case 6: // TCP<br />
            if ( bShowTCP )<br />
            {<br />
             printf("\n         TCP Source IP  : %s", ipSrc);<br />
             decode_tcp(&packet[ip_header_size]);<br />
            }<br />
            break;<br />
         case 17: // UDP<br />
            if ( bShowUDP )<br />
            {<br />
            printf("\n         UDP Source IP  : %s", ipSrc);<br />
            decode_udp(&packet[ip_header_size]);<br />
                        }<br />
            break;</pre><br />

推荐答案

为什么不创建值(数字)到字符串的映射(TCP源IP,
等),以使此代码更简单,更易于维护,它将
只需根据int的映射查找吐出一个字符串?
Why not create a map of values (numbers) to strings (TCP Source IP,
etc), to make this code a ton simpler and more maintainable, it will
just spit out a string based on a map lookup of the int ?


通过结合OOP和映射的用法,我们可以大大改善该代码.

该地图将用于根据协议选择合适的对象.之后,我们保留对适当对象的引用,并使用该对象来处理请求.

该代码可能比原始代码更长,但由于每个类都将负责其自己的协议,因此它的可维护性也要高得多.

By combining OOP and the usage of a map, we can improve that code a lot.

The map will be used to select the appropriate object according to the protocole. Afterward, we keep a reference to the appropriate object and use that object to handle the request.

The code might be longer that original code but it is much more maintainable also since each class will be responsible for its own protocole.

class ProtocoleBase 
{
public:
    ProtocoleBase() : show(true) {}
    virtual ~ProtocoleBase() { }
    virtual char const *name() const = 0;
    virtual void decode(unsigned char *data) = 0;

    void setshow(bool newValue) { show = newValue; }
    bool getshow() const { return show; }

private:
    bool show;
};

class ProtocoleICMP : public ProtocoleBase 
{
public:
    static int const code = 1;
    virtual char const *name() const { return "ICMP"; }
    virtual void decode(unsigned char *data) { decode_icmp(data); }
};

class ProtocoleTCP : public ProtocoleBase
{
public:
    static int const code = 6;
    virtual char const *name() const { return "TCP"; }
    virtual void decode(unsigned char *data) { decode_tcp(data); }
};

class ProtocoleUDP : public ProtocoleBase
{
public:
    static int const code = 17;
    virtual char const *name() const { return "UDP"; }
    virtual void decode(unsigned char *data) { decode_udp(data); }
};

std::map<int, std::shared_ptr<ProtocoleBase> > protocoleMap;

protocoleMap[ProtocoleICMP::code] = 
    std::shared_ptr<ProtocoleBase>(new ProtocoleICMP());

protocoleMap[ProtocoleTCP::code] = 
    std::shared_ptr<ProtocoleBase>(new ProtocoleTCP());

protocoleMap[ProtocoleUDP::code] = 
    std::shared_ptr<ProtocoleBase>(new ProtocoleUDP());

// Usage:
ProtocoleBase &protocole = *protocoleMap[ip_header->protocol].get();
if (protocole.getshow())
{
    printf("%s Source IP  : %s", 
        protocole.name(), ipSrc);
    protocole.decode(&packet[ip_header_size]);
}


这篇关于减少选择语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆