简体   繁体   English

我们何时以及为什么需要将一个班级分为多个班级?

[英]when and why do we need to divide a class into many classes?

I am an android beginner developer. 我是android初学者开发人员。 Currently, I am developing an application. 目前,我正在开发一个应用程序。 However, my class is quite large because there are many UI components (to handle onClick, onProgressBarChanged, etc.). 但是,我的班级很大,因为有许多UI组件(用于处理onClick,onProgressBarChanged等)。 Most of my components are dynamic. 我的大部分组件都是动态的。 So, I have method to create those components. 因此,我有创建这些组件的方法。

Now I split some methods for initializing UI components into another class. 现在,我将一些用于初始化UI组件的方法拆分为另一个类。 At this point, I am trying to think/search for a good reason to split my class into several classes. 在这一点上,我正试图考虑/寻找一个很好的理由将我的课程分为几个班级。

Advantage: maintainability, testability, reusability 优势:可维护性,可测试性,可重用性

Disadvantage: reduce runtime performance 缺点:降低运行时性能

I am not sure that there is any advantage or disadvantage that I have missed? 我不确定我是否错过了任何优势或劣势?

Furthermore, I will divide a class when I find an overlap method I am not sure that there is another situation when a class must be divided. 此外,当我找到一个重叠方法时,我将划分一个类,但我不确定是否存在必须划分一个类的另一种情况。

First, if you've never looked into refactoring , then I would strongly encourage you to do so. 首先,如果您从未研究过重构 ,那么我强烈建议您这样做。 Martin Fowler has some excellent resources to get you started. 马丁·福勒(Martin Fowler)有一些出色的资源可以帮助您入门。 But, I'm getting slightly ahead of myself. 但是,我有点领先了。

To begin with, you split out classes to maintain a clear delineation of responsibilities. 首先,您要划分班级以明确划分职责。 You can think of the SOLID principle here - each class does one thing, and one thing very clearly. 您可以在这里想到SOLID原理-每个类都做一件事情,一件事情做得很清楚。

If you notice that a method , let alone a class , is doing more than one thing, then that is a good time to stop and refactor - that is, take the code you have, and apply a particular, focused refactoring to it to improve readability and flow, while maintaining the same functionality. 如果您注意到某个方法 (更不用说一个 )做了很多事情,那么这是停止和重构的好时机-也就是说,获取您拥有的代码,并对其进行特定的,有针对性的重构以进行改进。可读性和流程性,同时保持相同的功能。 You're essentially looking for code smells - parts of the code that are suspect, not following a specific contract or methodology, or are legitimate anti-patterns - which are, themselves, practices that developers strive to avoid . 您本质上是在寻找代码的味道 -代码的可疑部分,而不是遵循特定的合同或方法,或者是合法的反模式 -它们本身就是开发人员在努力避免的做法。

Programs that deal with UI (especially in Java) tend to be pretty verbose. 处理UI的程序(尤其是Java语言)往往非常冗长。 What you should avoid doing is placing any conditional business logic in the UI layer, for ease of separability, testing and clarity. 您应该避免在UI层中放置任何条件业务逻辑,以简化可分离性,测试和清晰度。 Make use of the Model-View-Controller pattern to understand and abstract away the necessary separations between the UI (Views), and the actual work that's needed to be done (Controllers), while maintaining some semblance of state (Models). 利用“ 模型-视图-控制器”模式来理解和抽象出UI(视图)与需要完成的实际工作(控制器)之间的必要分隔,同时保持状态的相似性(模型)。

We use OOPs Concept in Android(core java) Application Development. 我们在Android(core java)应用程序开发中使用OOPs Concept。 If we split our one class in many class it gives a good sense of maintainability, re-usability, Security and Easy change in Coding during Development. 如果我们将一个班级分成多个班级,则可以很好地理解开发过程中的可维护性,可重用性,安全性和易于更改的编码。 As for example:- Util class for Database handling, Network Class for Internet connection , Dialog class for different type dialog and so... This way we can categories our coding and change or re use it any time. 例如:-用于数据库处理的Util类,用于Internet连接的网络类,用于不同类型对话框的Dialog类,等等...这样,我们可以对编码进行分类并随时更改或重新使用它。 So it is good practice to follow the OOPS concept during Development. 因此,在开发过程中遵循OOPS概念是一个好习惯。

Thanks 谢谢

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

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