views:

32

answers:

2

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 session 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!

+1  A: 

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.

Pbirkoff
Not too large. I'm guessing less than 5000 lines of code. Using PureMVC is a good idea but I want to focus on the concept here. In other words, the answer "use a framework and it gets put in the right place" is not sufficient. Maybe if you explain what a Proxy is and how that can be a better option that can help. Thanks!
Tony
+2  A: 

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

Karthik