7
\$\begingroup\$
[Bindable]
public class ProductsCache
{
 private static var _products:ArrayCollection;
 public function get products():ArrayCollection{
 return _products;
 }
 public function set products(value:ArrayCollection):void{
 _products = value;
 }
}

By the name of the class you see my intentions with this bit of code. I intend to set a "cache" of registries that I intend to use across modules and update accordingly. The get method is used for dropdown lists, the set method after a successful insert statement.

This works good for my needs. But I still need your feedback.

EDIT: I don't know what voting means here. Is this good code for its purpose? or do I really need to learn factories and dependency injections?

I was previously using a singleton for this, but I wasn't comfortable with that, I want to keep the singleton excusively for credential and config data. And use this for the rest of the app functionality.

asked Oct 4, 2011 at 22:43
\$\endgroup\$
3
  • \$\begingroup\$ Feedback on what? If you should use static variables? If you should do it like that? \$\endgroup\$ Commented Oct 5, 2011 at 7:06
  • \$\begingroup\$ Yep. That's important to me. \$\endgroup\$ Commented Oct 5, 2011 at 14:25
  • \$\begingroup\$ Damn it Jim, state in your question what you want! \$\endgroup\$ Commented Oct 5, 2011 at 14:26

1 Answer 1

6
\$\begingroup\$

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.

answered May 9, 2014 at 18:47
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.