6

最近、インスタンス変数にはグローバル変数と同じ問題があると感じています。これについてグーグルで調べたところ、私が見ている潜在的な問題について多かれ少なかれ説明しているこの古い記事を見つけました。

グローバル変数の同じ問題がインスタンス変数またはクラス変数に影響を与えることを回避するために、どのようなグッドプラクティスを使用しますか?

4

3 に答える 3

2

クラスはグローバル構造よりもはるかに小さいため、インスタンス変数の影響ははるかに小さくなります。クラスのサイズを小さく保ち、単一責任の原則を厳守することで、グローバル変数のマイナス面の多くを回避できます。インスタンス変数が渡されたパラメーターから作成されている場合、そのパラメーターをコンストラクターで必須にして、依存関係を明示的にすることがよくあります。また、インスタンス変数は適切にカプセル化されており、インスタンスのメソッドの外部で直接変更されることはなく、インスタンス変数が変更された場所を非常に簡単に特定できます。最後に、インスタンス変数は、クラス全体にとって意味のあるものにするか、プライベートにする必要があります。

于 2012-07-14T00:28:09.583 に答える
0

Instance variables are only accessible within a specific class. So to prevent instance variables being used too widely, keep classes small. If a class grows large, decide if parts of it that can be refactored into another, smaller class, that the original class uses.

于 2012-07-14T00:24:55.307 に答える
0

Nor Instance variables nor global variables nor any kind of variable have "problems"... They are all tools. The problem is that sometimes a lot of programmers choose to use the "wrong tool". You have to think carefully what your choices mean, so you can make the right choice.

Using a global variable for something, like CurrentUserName... Means that you are saying that he CurrentUserName is something universally know. And that "there can be only one" CurrentUserName at each time. And that will probably be false if you ever want to allow to users to be logged at the same time (unless you get really lucky, and both users have the same name)...

A realted wrong use with instance variables is if you put the e-mail address of a User as an instance variable, and you then realize that each user can have multiple e-mail addresses.

I'd also give an example with inheritance, because I think it'll make it more clear: A related problem with inheritance is for example if you are modeling the typical Student, Teacher problem, and you try making Student a subclass of Person and Teacher a subclass of Person. And then you realize that some persons might be both...

Student inheriting from Person is a static relationship that can't be changed at runtime. And Student and Teachers aren't static relationships... A person can be neither, and then start being a student, and then start being a teacher, and then stop being both, and yet it'll always be the same person, and that model can't handle that....

Coming back to the user, the user is "associated" with multiple e-mails account... If you put an instance variable you are stating that he is just "associated" with a single e-mail account, and you are contradicting your problem domain, and that's why you'll have problem...

The same applies if you say there is just a globally known current user name....

The problem in all cases is that you have a problem domain, and you are modeling it wrong... You have to make your program, and your model, behave similar to the problem domain.... If you don't do it, you'll have problems, whichever tool you choose to solve your problem.

BTW: I also think that User having a list of e-mail address is wrong, but that's for an entirely different set of motives. I'd actually use a

class ContactInformation
{
    User contact;
    EMailAddress email;
}

and remember that objects don't "own" nor "have" other objects... That's an implementation decision... Objects just "know" other objects...

于 2012-07-14T00:32:55.477 に答える