简体   繁体   English

这是一个好习惯吗? (类字段)

[英]Is this a good practice? (Class fields)

I need to have a relatively large number of categories defined (about 30 at start, we'll be adding more). 我需要定义相对大量的类别(开始时大约30个类别,我们将添加更多类别)。 Consider this code: 考虑以下代码:

public class Category {

    public static final Category DRESS = new Category("Dress");
    public static final Category SKIRT = new Category("Skirt");
    ...

    private static final List<Category> CATEGORIES = Arrays.aslist(DRESS, SKIRT, ...);

    private String name;

    public Category(String name) {
         this.name = name;
    }

    //Some static public method to iterate over categories
   ... 

I need to have the categories declared and also need a way to iterate over them. 我需要声明类别,还需要一种对它们进行迭代的方法。 I discard reflection because I think it's not a very good practice. 我放弃反射,因为我认为这不是一个很好的做法。

Is declaring a large name of static final fields of the same class and also having them inside a list a good practice? 声明同一类的静态final字段的大名称并将它们包含在列表中是否是一种好习惯? As an alternative, I thought about having a Map<Integer, Category> instead the list, and the fields were integers that would identify each category, so you would get the categories by getting them inside the map. 作为替代方案,我考虑过使用Map<Integer, Category>代替列表,并且字段是可以标识每个类别的整数,因此您可以通过将其放入地图中来获得类别。 Would this be better in terms of time and space performance? 就时间和空间性能而言,这会更好吗?

PS: It's for an android project, if it changes something PS:适用于android项目,如果有更改

Consider this code: 考虑以下代码:

public class Category {

    public static final Category DRESS = new Category("Dress");
    public static final Category SKIRT = new Category("Skirt");

Yeah this is literally what enums do in the background, so 是的,这实际上是枚举在后台执行的操作,因此

public enum Category {
    DRESS("Dress"),
    SKIRT("Skirt"),
    ...;

    private String name;

    private Category(String name) {
         this.name = name;
    }

    // Category.values() returns the elements as an array

You should use enum instead of creating an object with new Category("Dress"); 您应该使用enum而不是使用new Category("Dress");创建对象new Category("Dress"); because creating an object is expensive than using enum . 因为创建对象比使用enum 昂贵 Java enums are implemented more like classes, so you can change your code seamlessly: Java枚举的实现更像类,因此您可以无缝更改代码:

public enum Category {
  DRESS("Dress"), SKIRT("Skirt");

  private final String name;

  Category(String name) { 
    this.name = name; 
  }

  public String getName() { 
    return name; 
  }
}

Note: 注意:
The constructor for an enum type must be package-private or private access. 枚举类型的构造函数必须是程序包私有或私有访问。 It automatically creates the constants that are defined at the beginning of the enum body. 它会自动创建在枚举主体开头定义的常量。 You cannot invoke an enum constructor yourself. 您不能自己调用​​枚举构造函数。

Read more about enum at Enum Types Enum Types上了解有关枚举的更多信息

I would say using a List is good enough. 我会说使用List就足够了。

You should consider a Map only if you have to look up a particular Category very frequently via some key property (like an int your case). 仅当您必须通过某些关键属性(例如int案例)非常频繁地查找特定Category才应考虑使用Map

If There are no properties or methods in the Category class consider replace them with just Strings . 如果Category类中没有属性或方法,请考虑将其替换为Strings

If new Categories are created at runtime and you want to persist them consider using a DB or File to save the Categories. 如果在运行时创建了新Categories ,并且要保留它们,请考虑使用数据库或文件来保存类别。

Edit: Answering the question in the comment 编辑:回答评论中的问题

That would depend on the Category class. 那将取决于Category类。 If its only purpose is to enumerate all the categories and the class itself does not have any other instance methods or properties then in terms of space complexity an Integer and your Category class is similar (since in a Map integer will be boxed in the Integer class object) 如果其唯一目的是枚举所有类别,并且该类本身没有任何其他实例方法或属性,则就空间复杂度而言, Integer和您的Category类是相似的(因为Map整数将被装在Integer类中宾语)

I would still suggest that you use a class called Category and a list if the purpose is only iterating over them and/or using specific instances of the Category class elsewhere in your application eg. 我仍然建议您使用一个名为Category和list的Category ,如果其目的仅是在它们上进行迭代和/或使用应用程序中其他位置的Category类的特定实例。 Category.SOME_CATEGORY . Category.SOME_CATEGORY

The following example is a good use-case 以下示例是一个很好的用例

someMethod(Category category) {
    // do something
}

versus

someMethod(int category) {
   // before doing anything
   // lookup this category by an int key
   // in the the Map<Integer, Category>
}

The problem with the latter is that you could pass any int which may or may not be a valid key for a category. 后者的问题是,您可以传递可能不是类别的有效键的任何int值。 Using a class gives some bit for extra compile time check. 使用类可以提供一些额外的编译时间检查。 Though you could always use an int def too. 虽然您也可以始终使用int def But again I would repeat that it all boils down to whether Category class has any instance methods or properties. 但是我再次重申,这全都归结为Category类是否具有任何实例方法或属性。

For small list, it is okay to use List or Map . 对于小列表,可以使用ListMap

But for a large list, you may want to store them in a database. 但是对于较大的列表,您可能需要将它们存储在数据库中。

Also ArrayList of String will be slightly efficient than using ArrayList of Category 而且String ArrayList会比使用Category ArrayList稍微有效

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

相关问题 在数据类主构造函数中使用私有字段是一种好习惯吗? - Is it good practice to use private fields in data class primary constructor? 课堂上的良好实践摇篮风味 - Good Practice Gradle Flavors With Class 设置包装器类是一种好习惯吗? - Is setting wrapper class a good practice? 使用导入 static 字段是一种好的编程习惯吗? - Is it a good programming practice to use import static fields? 好的记忆课清理习惯? - good memory class clean up practice? Android-使用静态字段是否可以使活动重新启动仍然有效? - Android - Is using static fields a good practice for surviving activity restart? Android:将类作为全局变量存储在类中是一种好习惯吗? - Android: Is it good practice to store Views as global variable in class? 将Android Activity的对象传递给Thread类的构造函数是一个好习惯吗? - Is it a good practice to pass an object of Android Activity to a Thread class' constructor? 开发一个负责所有网络任务的辅助网络类是不错的做法? - Is it good practice to develop a helper network class that is responsible for all network tasks? 在 Repository class 中观察 Forever 是一个好习惯吗? db.network 分页列表 - Is it a good practice to observeForever in Repository class? db+network paged list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM