PDA

View Full Version : Initialization of remote APIs in a MVC pattern


glantucan
12-03-2010, 04:34 PM
Hi all

I'm trying to buid a video player application usin the Youtube API, which is loaded remotely from their servers as described http://code.google.com/intl/es-ES/apis/youtube/flash_api_reference.html
Basically I want to add a playList via XML or flashVars which would be stored in the Model among other data taken from the video feeds of youtube by HTTP request .

The issues are:
1.- I don't know where the youtube API would fit in the MVC pattern, because it has two parts, the Loader instance that you can add to the display list and the API Object itself that is more like a controller.
My first approach has been to create a controller class which loads it and initiates it. But then I have to provide a reference of the loader to the View. Should I do it using a setter in the view, dispatching an event and then a listener in the view gets this reference from the controller, or including the reference in the model??? I'm a little bit lost here.

2.- On the other side, I don't know how to implement the strategy pattern betwen the view and the controller in this scenario. Should I leave the whole initialization process outside the MVC pattern?

It's not that I'm obsesed with design patters, I'm just trying to learn how to use them and put myself this as an exercise: Build a videolist player for youtube so that it's easy to add, let's say Vimeo support later.

Thanks in advance
Cheers

glantucan
12-03-2010, 10:56 PM
Ok, At least I'm getting somewhere, but it looks pretty complex to me.

I drawed a draft UML diagram of a possible solution (Well just bare classes and interfaces).
35877
Let's say we want two possible GUIs, one with the classical video player buttons and another using mouse gestures.
On the video APIs part let's assume we want our player to get videos from Youtube and from Vimeo.
We build our playlist with its medada on our Model (not depicted here, I think it's complex enough), and the API's are already initialized (let's leave the how and who of that for the moment)
So, one view class for holding the GUIS and the players Loader and two for the two GUIs

Both of the latter use one specific controller provided by the strategy pattern. This two controller classes share the same playerStateMachine which plays the context rol on a State pattern.
The State Classes comunicate with the youtube and vimeo APis through and Adapter interface and classes, which control both the API objects and the Loader instance in the display list.

Uff!!!

Am I getting all this wrong?
Sound this reasonable to you?
Can be done more efficiently (sure)?
Am I using a cannon to kill flies (sure too)?

Opinions, critics thoughts?

Thanks

drkstr
12-05-2010, 12:34 PM
I'm a bit confused why your state machine is speaking directly to the video adapter. Wouldn't it go state -> api adapter -> API -> video adapter -> video? Or are you using the same adapter to hook up the API to both your view and controller? Maybe I am just not reading the diagram correctly?

Regardless, textbook MVC implementation. Well done.

glantucan
12-05-2010, 04:04 PM
I'm right now fighting with the state machine... And I'm quite confused myself. I'm trying too many new things at once. Because I don't know how to make the stat machine aware of the state change once the API change the actual state. Events or change in the model? (I didn't plan to store that info in the model, bute i'm hesiteting now)

About your question I realize now my diagram is a litle confusing just beacuse I wanted to split the (lets say youtube) API in two parts to fit in the MVC some way (so ytView is the Viw part and the ytAPI the controller part. But they are actually contained in the same Loader object. So now I realize that as no part of this object is changing the model, I could just put them together in the View.

And further more, unless I decide to send the state changes to the model, the whole state machin should go in the view too. Am I right?


Thanks :)

glantucan
12-05-2010, 04:53 PM
Does this makes more sense?
35885

drkstr
12-06-2010, 04:52 AM
Well I think it really just comes down to personal programming style. I actually don't take a pure MVC approach myself. Instead, I take a more data-driven approach, where the state machine is the heart of my applications. This is probably why I was curious about the role your data states played. Everything looks like a nail when you're a hammer.

The approach I take is similar to MVC, except that my controllers are only responsible for managing data states. My views, and other controls, then subscribe to changes in those data states making the data the core driver of my application, rather than the controllers themselves. I find this method to be a lot more conducive for encapsulation. MVC purists would disagree.

As for your set up, I think any pre-planned architecture already puts you miles ahead of most programmers. As long as it works for you, then it's the right way to do it.


Cheers and good luck!

drkstr
12-06-2010, 06:14 AM
After looking at your updated UML closer, it actually looks like you're taking a similar approach as I would take. So to answer your question (I think), no this would not be considered a pure MVC approach. However, I would argue that it's actually a better approach to take then pure MVC. :)

The only thing I would change is to put all the data state management in your controller, and have the view subscribe to changes on it. That's really just personal preference though. Whatever works best for you is the way to go.

glantucan
12-06-2010, 10:44 AM
Thanks drkstr!
It's good to know, one is in the right path.
I made a pre-plan at the beginning of this project, which proposed myself in order to learn design patterns. I think I'm starting to get it and I'm happy for that. But now I'm changing thinks in the plan and in the implemetation at the same time, and getting to know how coupling make programmers suffer in the process :)

Couple of questions though

Isn't it pure MVC because Controller is talking directly to the View, right?

And to my original question: At the moment, I have the initalization as XML configuration in the Model. I mean, what remote services are active, what adapters and what model helpers (among the path to the source classes) they need to get aditional information from each particular server . I do that to allow adapters and helper classes to be added to the project just by changing that XML an the includes parameter in the flex compiler as I add more of them in the future.
I pass that info to the view through a hash object, but I'm getting stucked when trying to pass a copy of it instead of a reference. Is there away to clone an Object instance without copying its propertis one by one?. Is there a better approach.
Having the adapters in the view saves one step in the initialization process, cause I don't have to pass the necessary information to the controller an make him notify the view that he is ready when initialization ends.