简体   繁体   English

您如何知道何时需要添加单独的课程?

[英]How do you know when you need to add a separate class?

This is a pretty elementary general question, but it's pretty simple. 这是一个非常基本的通用问题,但是非常简单。 I understand that you create a class to create objects, but I'm wondering if that is the ONLY reason why you would need to create a separate class? 我知道您创建了一个用于创建对象的类,但是我想知道这是否是您需要创建单独的类的唯一原因吗? The reason I ask is because in a course I'm taking, all the students presented on the design of a program that we all had to build individually. 我问的原因是因为在我修读的一门课程中,所有学生都提出了我们都必须单独构建的程序设计。 Many students had 5-10 classes, while I only had 2. Both options seem to work just fine. 许多学生上了5-10堂课,而我只有2堂课。这两种选择似乎都很好。

If this is a subjective question, then what is the most accepted way to write programs. 如果这是一个主观问题,那么编写程序的最可接受的方式是什么。 What do industry leaders say about adding extra classes that might not exactly be 100% necessary? 行业领导者对添加不一定完全是100%必要的额外类有何看法?

Here are some guidelines: 以下是一些准则:

Single Responsibility Principle 单一责任原则

The single responsibility principle basically says that each class should only do one thing. 单一责任原则基本上说每个班级只能做一件事。 If there is a class that handles two or more things, split that into multiple classes. 如果有一个处理两个或多个事物的类,请将其拆分为多个类。

So check your classes now, is there any class that does a lot of things, like getting user input, doing caluclations, and printing the result? 因此,现在检查您的班级,是否有任何班级做很多事情,例如获取用户输入,进行计算并打印结果? If that's the case you probably want to split that into multiple classes. 如果是这种情况,您可能希望将其拆分为多个类。

Abstraction 抽象化

Abstraction in OOP is very important. OOP中的抽象非常重要。 It's basically the process of making real world things into classes or interfaces. 从根本上讲,这是将现实世界的东西变成类或接口的过程。 For example, if you were doing a calculation app, you would have a Calculator class that does the actual calculation. 例如,如果您正在计算应用程序,则将有一个进行实际计算的Calculator类。 You would also have a CalculatorWindow class that manages the window of the calculator, like listening for button events. 您还将拥有一个CalculatorWindow类,该类管理CalculatorWindow的窗口,例如侦听按钮事件。 This class can then give the user input to a Calculator object and have it calculate the result. 然后,此类可将用户输入提供给Calculator对象,并让其计算结果。

Design Patterns 设计模式

There are a lot of design patterns out there. 那里有很多设计模式。 By looking at these design patterns, you can see how classes interact and hopefully you'll get when to create a new class. 通过查看这些设计模式,您可以了解类之间的交互方式,并希望您能在何时创建新类。

There is no one-size-fits-all answer to this. 对此没有一个万能的答案。


If this is a subjective question, then what is the most accepted way to write programs. 如果这是一个主观问题,那么编写程序的最可接受的方式是什么。

Obviously, that question is subjective too. 显然,这个问题也是主观的。 (You don't expect us to do a survey for you do you?) (您不希望我们为您做调查吗?)

What do industry leaders say about adding extra classes that might not exactly be 100% necessary? 行业领导者对添加不一定完全是100%必要的额外类有何看法?

You mean like, Bill Gates, Larry Ellison? 您是说比尔·盖茨,拉里·埃里森吗?

They probably don't say anything. 他们可能什么也没说。

Sure, there are some people who will get up on their soap box and tell you with great authority that XYZ is "best practice" or some such. 当然,有些人会站起来在肥皂盒上,并极有权威地告诉您XYZ是“最佳实践”或诸如此类。 But most of the time these pronouncements are based on (at best) anecdotal evidence / personal experience. 但是大多数时候,这些声明是(最多)基于轶事证据/个人经验。 In other words, they are really opinions dressed up as facts. 换句话说,它们实际上是将事实打扮成事实的观点。


Bottom line: there is no single correct answer, so you learn: 底线:没有一个正确的答案,因此您将学习:

  • what works best for you (and your team), and 什么最适合您 (和您的团队),以及
  • it is not really worth worrying too much about it ... provided what you are doing "works for you". 如果您正在做什么“为您工作”,那么真的不必为此担心太多。

(The two extremes that don't "work" are when the classes are so fine-grained that you spend all of your time writing and reading boilerplate, OR the classes are so large and complex that you can no longer understand them.) (两个极端不起作用的情况是,这些类的粒度是如此之细,以至于您将所有时间都花在编写和阅读样板上,或者这些类又太大又复杂以至于您无法理解它们。)

Different class are used to do different tasks. 使用不同的类来执行不同的任务。 This reduces code duplication and increases code reuse . 减少了代码重复增加了代码重用 And this helps you to follow design pattern to solve some critical problem easily. 这可以帮助您遵循设计模式轻松解决一些关键问题。 This is good practice. 这是一个好习惯。

Here is a simple example. 这是一个简单的例子。

To sort data. 排序数据。

//Without using multiple class
public class A{
  public static void main(String[] args){
     int array[] = {1,6,1,8,34,5};
     for(int i=0; i< array.length; i++){
        //your procedure to sort the array
     }
     //other operations;
     //Now you need to sort another new array (new_array[])
     int new_array[] = {1,6,1,8,34,5};
     for(int i=0; i< new_array.length; i++){
        //your procedure to sort the new_array
     }
  }
}

Here in this example we used two for loop to sort two different array. 在此示例中,我们使用两个for循环对两个不同的数组进行排序。 Now see a example with multiple classes 现在看一个带有多个类的例子

public class A{
  public static void main(String[] args){
     int array[] = {1,6,1,8,34,5};

     int my_array[] = Opertaion.sortArray(array);     

     //other operations;
     //Now you need to sort another new array (new_array[])

     int new_array[] = {1,6,1,8,34,5};
     int my_new_array[] = Opertaion.sortArray(new_array); 
  }
}

public class Opertaion{
   public static int[] sortArray(int[] array){
     for(int i=0; i< array.length; i++){
        //your procedure to sort the array
     }
     return array;
   }
}

The above example is very simple example but when you need to do big projects using multiple class will reduce your time to code. 上面的示例是一个非常简单的示例,但是当您需要使用多个类进行大型项目时,则会减少编写代码的时间。

Suppose when you are in a big project 假设您在大型项目中

you will write a class to control database queries, a Service class to handle other operation with that database class, a controller class to control everything etc. 您将编写一个类来控制数据库查询,一个Service类来处理对该数据库类的其他操作,一个控制器类来控制所有内容等。

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

相关问题 当您不知道需要创建多少个对象时,如何将对象存储在ArrayList中? - How to Store objects in an ArrayList when you do not know how many object you need to create? 完成这个类方法 - 你知道怎么做吗? - Finish this class method - Do you know how? 您是否需要了解JSF或Spring中的JSTL - Do you need to know JSTL in JSF or Spring JAVA:如何访问单独类中的数据成员? - JAVA: How do you access data members in a separate class? 您知道如何从片段向类发送订单吗? - Do you know how to send an order from a fragment to a class? 使用JAXB时是否总是需要ObjectFactory类? - Do you always need an ObjectFactory class when using JAXB? 如何在不知道密码时重置密钥库? - How to reset keystores when you do not know their password? 为什么在扩展子类时需要将精确方法作为基类? - Why do you need to have the exact method as the base class when you extend a subclass? 为什么你需要知道抽象类的方法是否是抽象的 - why would you need to know whether a method of an abstract class is abstract 当您需要在Java中通过引用传递为多个参数赋值时,您是如何做到的? - When you need pass by reference in Java to assign values to multiple parameters, how do you do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM