Switch cases maximum implementation?

前端 未结 7 1667
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 12:27

I am using a single switch cases which will have more than 100 cases statement to be used. Are there any limit ?

The usage of cases are for the suggestions of my Aut

7条回答
  •  逝去的感伤
    2021-01-19 12:56

    I don't know exactly what your startActivity() method do, don't even know how the Intent object is implemented, but I think an alternative way to solve your issue could be:

    • Define a super-class or interface called Shop (for example);
    • Inherit all your classes like Adidas or Affin from it;
    • Call the specific implementation of startActivity() method for each class;

    For example:

    public interface Shop
    {
        void startActivity(Intent i);
    }
    

    Then, for each class...

    public class Adidas implements Shop
    {
        public Adidas(){
            // ...
        }
    
        public void startActivity(Intent i)
        {
            // specific implementation
        }
    }
    

    Finally, in your client code

    Shop[] shops = new Shop[]{ new Adidas(), new Affin(), ... };
    
    for (Shop shop : shops)
    {
       shop.startActivity(new Intent(Search.this));
    }
    

提交回复
热议问题