简体   繁体   English

拥有90%静态成员的Java类。 好的做法还是什么?

[英]Java Class That Has 90% Static Members. Good Practice Or What?

I'm 14 and have been learning java for about 4/5 months. 我14岁,已经学习了大约4/5个月的java。 I'm coding a game now called super mario winshine and i wanted to know if it is good practice to have a class that is mostly static variables. 我正在编写一个现在叫做super mario winshine的游戏,我想知道一个主要是静态变量的类是否是一个好习惯。

The class is the one that holds all the information for the game's level/world. 该类是保存游戏关卡/世界的所有信息的类。 Since I only need one version of this class, and lots of other classes will be using it, I choose to make all the variables static. 由于我只需要这个类的一个版本,并且许多其他类将使用它,我选择将所有变量设置为静态。 Is this good practice? 这是好习惯吗?

I have considered the fact that i could keep the variables "non-static" and just clone the main object i use for that class, but I thought i would rather sacrifice "OO" for memory in this case. 我已经考虑过这样一个事实:我可以保持变量“非静态”并且只是克隆我用于该类的主要对象,但我认为在这种情况下我宁愿为内存牺牲“OO”。

As soon as you want to have two or more worlds this will fail. 一旦你想拥有两个或更多的世界,这将失败。 Say, when your first release is a runaway success and you want to add the "parallel universe" expansion set. 比如,当您的第一个版本取得巨大成功并且您想要添加“并行Universe”扩展集时。

In my experience, 90% of the time when marketing says "oh, don't worry, there will only be one Application/Window/Database/User" they are wrong. 根据我的经验,90%的时间营销说“哦,别担心,只有一个应用程序/窗口/数据库/用户”他们错了。

ADDED 添加

I would also avoid using a true Singleton pattern with World.getInstance() etc. Those are for the rare cases where it really is an essential requirement that there only be one of something. 我也会避免在World.getInstance()等中使用真正的Singleton模式。这些是在极少数情况下,它确实是一个基本要求 ,只有一个东西。 In your case, you are using it as a convenience, not a requirement. 在您的情况下,您使用它是为了方便,而不是要求。

There is no perfect fix, YMMV, but I'd consider a single static method, something like 没有完美的修复,YMMV,但我会考虑一个单一的静态方法,类似于

   World World.getWorld(String name)

and then you call real (non-static) methods on the World that is returned. 然后在返回的World上调用真实(非静态)方法。 For V1 of your program, allow null to mean "the default world". 对于程序的V1,允许null表示“默认世界”。

Some might put that method into a class named WorldManager, or, perhaps showing my age, a more clever name like Amber. 有些人可能会把这个方法放到一个名为WorldManager的类中,或者,或许显示我的年龄,一个更聪明的名字,如Amber。 :-) :-)

It all depends upon what your methods and classes are. 这一切都取决于你的方法和类是什么。 There is no problem in defining utility methods as static methods in a class. 实用程序方法定义为类中的静态方法没有问题。 There is no need to make it a singleton as others are suggesting. 没有必要像其他人所暗示的那样使它成为单身人士。 Look at the Math class from java.lang package. java.lang包中查看Math类。 It has lot of utility methods but it isn't a singleton. 它有很多实用方法,但它不是单例。

Also check out static imports functionality. 还要检查静态导入功能。 Using this you doesn't need to qualify method calls with the class name. 使用此方法,您无需使用类名限定方法调用。

Well, what you are doing is definitely an option. 那么,你在做什么绝对是一个选择。 Or you could use a singleton pattern: 或者你可以使用单例模式:

public class World {
   private static World instance = new World();

   private World() {
   }

   public static World getInstance() {
       return instance;
   }
}

Now just use World.getInstance() everywhere to have a unique object of this type per application. 现在只需使用World.getInstance() ,每个应用程序都有一个这种类型的唯一对象。

I would say it's definitely not a good practice. 我会说这绝对不是一个好习惯。

I've not seen your code, but having several static variables in a class that other classes access freely seems to indicate that you're not really using object orientation/classes but more just writing procedural code in Java. 我没有看到你的代码,但是在其他类自由访问的类中有几个静态变量似乎表明你并没有真正使用面向对象/类,而只是在Java中编写过程代码。 Classes should generally encapsulate/hide all their variables - static or not - from access from other classes so that other classes don't depend on how the class is implemented. 类通常应该封装/隐藏所有变量 - 静态或非静态 - 来自其他类的访问,以便其他类不依赖于类的实现方式。

The static part also causes problems with making threads work (global variables are hard to lock in a good way so that nothing deadlocks) and with unit testing (mocking is all but impossible) 静态部分也会导致线程工作出现问题(全局变量很难以很好的方式锁定,因此没有死锁)和单元测试(模拟几乎不可能)

I also agree with the other posters, if you need "global variables", at least make them singletons. 我也同意其他海报,如果你需要“全局变量”,至少要让它们成为单身人士。 That allows you to change strategy easier later and does not lock you to one world. 这使您可以在以后更轻松地更改策略,而不会将您锁定到一个世界。

Edit: I'm definitely not advocating singletons as a good pattern here if someone read it like that, but it does solve some problems with static variables, esp. 编辑:如果有人像这样阅读它,我绝对不会提倡单身人士作为一个好的模式,但它确实解决了静态变量的一些问题,尤其是。 regarding testing/mocking compared to just statics so I'd say it's a ever so slightly lighter shade of gray :) It is also a pattern that is easier to gradually replace with better patterns by for example using a IoC container. 关于测试/模拟与静态相比,所以我会说它是一个稍微浅一点的灰色:)它也是一种模式,通过例如使用IoC容器更容易逐渐替换为更好的模式。

You may wish to look into implementing this class as a singleton , while there is nothing particularly wrong with your approach it may lead to some inflexibility further down the road. 你可能希望将这个类作为一个单例来实现 ,虽然你的方法没有什么特别的错误,它可能导致一些不灵活性。

Also you should take in to consideration the purpose of static members which is to be a member of the class and 'act' on/with the class not an instance of it. 此外,您应该考虑static成员的目的,该static成员将成为该类的成员,并且对该类进行“操作”而不是该类的实例。 For example the static method in a singleton returns either a new instance of the class if one doesn't already exist or returns the instance, and because the method is static you do not instantiate a new one. 例如,单例中的静态方法返回类的新实例(如果尚未存在)或返回实例,并且因为该方法是static ,所以不会实例化new实例。 This is probably worth a read because it can be somewhat confusing when determining the appropriate use of static members 可能值得一读,因为在确定static成员的适当使用时可能会有些混乱

I think it is fine as long as you don't need anything more sophisticated, in other words, static fields are OK as long as different objects (including subclasses if there will be any) do not need different values. 我认为只要您不需要任何更复杂的东西就可以了,换句话说,只要不同的对象(包括子类,如果有的话)不需要不同的值,静态字段就可以了。

You code by yourself, refactoring is easy with modern tools, me says don't fix it until it is broken, and focus on the algorithmic aspects of your project. 您可以自己编写代码,使用现代工具进行重构很简单,我说不要修复它直到它被破坏,并专注于项目的算法方面。

Perhaps you may think to encapsulate all those static fields within a different static class, as it is a good principle to "keep what changes seperate from what does not". 也许您可能会考虑将所有这些静态字段封装在不同的静态类中,因为“保持哪些更改与不存在的更改”是一个很好的原则。 Chances are one day you will want to initiate that static class with different values, for example want to read the initial values from an XML file and/or registry, add behaviour, etc. so instead of a static class you will implement it with a Singleton pattern. 有一天你可能希望用不同的值启动那个静态类,例如想要从XML文件和/或注册表中读取初始值,添加行为等等,而不是使用静态类来实现它。单身模式。

But clearly that is not the concern of today. 但显然这不是今天的关注点。 Till then, enjoy! 直到那时,享受!

I'm not sure what you are really talking about from your short description, so I'll try this: 我不确定你从简短的描述中谈到了什么,所以我会试试这个:

public class Level {
  static List<Mushroom> mushrooms;
  static List<Coin> coins;
  ...
}

Is that you were describing? 你在描述吗?

You asked if this is "good practice" and I can tell you that this looks very odd, so, no, it's not. 你问这是不是“好习惯”,我可以告诉你这看起来很奇怪,所以,不,不是。

You gain absolutely nothing by doing this. 这样做你什么都得不到。 You make it impossible to have more than one Level , which brings no advantage, but it could cause trouble in the future. 你不可能拥有多个Level ,这没有任何优势,但它可能在将来造成麻烦。

I didn't understand your last paragraph where you say you made most things static to save memory. 我不明白你的最后一段,你说你为了节省记忆你做了大多数事情。 You would usually create one Level and it would be passed around (without cloning) to the various classes/methods that read from it or modify it. 您通常会创建一个Level ,它将被传递(不克隆)到从中读取或修改它的各种类/方法。

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

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