Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Using a static variable will have the same effect as using a singleton!

Imagine if you had two instances of ProductsCache:

var a: ProductsCache = new ProductsCache();
var b: ProductsCache = new ProductsCache();
a.products = someArrayCollection;
b.products = anotherArrayCollection;

Now, because it's using a static variable, a.products will be anotherArrayCollection.

###"Do I really need to learn factories and dependency injections?"

"Do I really need to learn factories and dependency injections?"

Yes, please, learn Dependency injection! You won't regret it! I don't think for this particular case you need a Factory pattern, but it never hurts to learn that too.

Dependency Injection is not that hard actually, the golden rule is: Whenever an object needs your ProductsCache, give them a reference to your ProductsCache. Don't use something like ProductsCache.instance.products. Tell objects about the ProductsCache you want them to use, don't let them ask a singleton about it. Very easy rule to remember: Tell, don't ask.

Using a static variable will have the same effect as using a singleton!

Imagine if you had two instances of ProductsCache:

var a: ProductsCache = new ProductsCache();
var b: ProductsCache = new ProductsCache();
a.products = someArrayCollection;
b.products = anotherArrayCollection;

Now, because it's using a static variable, a.products will be anotherArrayCollection.

###"Do I really need to learn factories and dependency injections?"

Yes, please, learn Dependency injection! You won't regret it! I don't think for this particular case you need a Factory pattern, but it never hurts to learn that too.

Dependency Injection is not that hard actually, the golden rule is: Whenever an object needs your ProductsCache, give them a reference to your ProductsCache. Don't use something like ProductsCache.instance.products. Tell objects about the ProductsCache you want them to use, don't let them ask a singleton about it. Very easy rule to remember: Tell, don't ask.

Using a static variable will have the same effect as using a singleton!

Imagine if you had two instances of ProductsCache:

var a: ProductsCache = new ProductsCache();
var b: ProductsCache = new ProductsCache();
a.products = someArrayCollection;
b.products = anotherArrayCollection;

Now, because it's using a static variable, a.products will be anotherArrayCollection.

"Do I really need to learn factories and dependency injections?"

Yes, please, learn Dependency injection! You won't regret it! I don't think for this particular case you need a Factory pattern, but it never hurts to learn that too.

Dependency Injection is not that hard actually, the golden rule is: Whenever an object needs your ProductsCache, give them a reference to your ProductsCache. Don't use something like ProductsCache.instance.products. Tell objects about the ProductsCache you want them to use, don't let them ask a singleton about it. Very easy rule to remember: Tell, don't ask.

Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311

Using a static variable will have the same effect as using a singleton!

Imagine if you had two instances of ProductsCache:

var a: ProductsCache = new ProductsCache();
var b: ProductsCache = new ProductsCache();
a.products = someArrayCollection;
b.products = anotherArrayCollection;

Now, because it's using a static variable, a.products will be anotherArrayCollection.

###"Do I really need to learn factories and dependency injections?"

Yes, please, learn Dependency injection! You won't regret it! I don't think for this particular case you need a Factory pattern, but it never hurts to learn that too.

Dependency Injection is not that hard actually, the golden rule is: Whenever an object needs your ProductsCache, give them a reference to your ProductsCache. Don't use something like ProductsCache.instance.products. Tell objects about the ProductsCache you want them to use, don't let them ask a singleton about it. Very easy rule to remember: Tell, don't ask.

default

AltStyle によって変換されたページ (->オリジナル) /