This question extends beyond the Facebook library and is really a general architecture question. I am using the Facebook AS3 lib which has a FacebookSession class. The class contains data for a facebook sessi开发者_开发技巧on so that I can make authenticated calls to the Facebook API.
I am using the Facebook API all over my Flex app and I am tempted to store it in a Singleton class. I think this is a pretty good option but was wondering if someone had a different point of view for a possible Architecture.
Thanks!
Building off Pbirkoff's answer, I would recommend using an MVC approach whether or not you decide to go with an explicit framework to make that happen.
In that case, FacebookSession would be a model class, and I usually like to have model be a top-level package (after all the com.domainname stuff). So in this case I might do something like com.tony.model.facebook.FacebookSession for the package structure.
For a small project like yours, a Singleton is fine. Once the codebase grows much larger, singletons become dangerous because now you have a lot of global state and this can get messy if you have a ton of classes accessing the singleton from all over the code (see http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/), because it breaks loose coupling.
Once your project gets larger you can look into using dependency injection to avoid this singleton problem. In this case a framework like Swiz, etc. can come in handy.
HTH,
Karthik
How large is your application going to be? You could consider using a framework like PureMVC (http://www.puremvc.org). It helps to decide where to put certain things (In this case, your FaceBookSession would be placed inside a Proxy in PureMVC).
Otherwise, a singleton sounds good enough.
精彩评论