简体   繁体   English

如何组织和减少应用程序中的课程数量

[英]How Can I Organize and Reduce The Amount of Classes in My Application

I guess this is more of a Design Pattern question than coding question. 我猜这更多是设计模式问题,而不是编码问题。 So here we go... I have an android application that contain many classes and activities. 所以我们开始...我有一个包含许多类和活动的android应用程序。 I have classes grouped inside directory according to their function eg: DownloadTask, ScanTask inside the AsyncTask directory. 我根据其功能将类分组在目录内,例如:AsyncTask目录内的DownloadTask,ScanTask。 Then I have all all activities inside the activity directory and so on. 然后,我将所有所有活动都放在活动目录中,依此类推。 But I notice that a lot of my code is repeated. 但是我注意到很多代码都重复了。 For example, I have 3 Activities PingActivity, NetstatActivity, WhoisActivity that basically look entriely the same from the point of view of the code. 例如,我有3个活动PingActivity,NetstatActivity,WhoisActivity,从代码的角度来看,它们基本上是完全相同的。 The only difference is the command their using eg: ping host for PingActivity, netstat -ant for NetstatActivity and so on.. they all instanciate the views from an xml layout that looks very similar since all I have is EditText, button, and TextView. 唯一的不同是它们使用的命令,例如:PingActivity的ping主机,NetstatActivity的netstat -ant等等。它们都从看起来非常相似的xml布局实例化视图,因为我所拥有的只是EditText,button和TextView。 And the classes Call AsyncTask to perform the background process separate from the main thread. 并且类调用AsyncTask来执行与主线程分离的后台进程。 With the exception of WhoisActivity which use rest api service to query for domain name. 除了WhoisActivity之外,WhoisActivity使用rest api服务来查询域名。 So my question is how can I simplify my design? 所以我的问题是如何简化设计? can I use one single class and pass the command or api depending what menu option the user selected? 我可以使用一个单一的类并根据用户选择的菜单选项传递命令或api吗? how can I do that? 我怎样才能做到这一点? thanks 谢谢

Just a few suggestions, in my humble opinion ... 以我的拙见只提供了一些建议...

  • Reducing the number of classes should never be a goal by itself. 减少类的数量本身绝不是目标。
  • Multiple pieces of code that look the same, may actually do different things in terms of logic, and may evolve in very different ways in the future ( DRY is about knowledge, not code ). 看起来相同的多段代码实际上可能在逻辑上做不同的事情,并且将来可能以非常不同的方式发展( DRY是关于知识,而不是代码 )。
  • Always design with SOLID in mind, when you say "can I use one single class and pass the command or api", that sounds worse for the Single-Responsibility and the Open-Closed principles. 当您说“我可以使用一个单一的类并传递命令或api”时,请始终牢记SOLID ,这对于单一职责和开放式封闭原则而言听起来更糟。

Having said that, if there are pieces of code that look (and actually are) equal, try to factor them out into services that get injected in your classes. 话虽如此,如果有一些看起来(实际上)相等的代码,请尝试将它们分解为注入类的服务。 Just do not try to reduce complexity by putting all the code that looks similar in a class/method with a few parameters and some if-else/switch blocks, because in long term that's usually much worse than having a lot of small and organised classes. 只是尝试把所有看起来类似代码的类/方法的几个参数,以降低复杂性和一些的if-else /开关块,因为在长期这通常不是有很多小的和有组织的阶级差很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM