views:

1833

answers:

5

I'm developing a desktop application which depends on the XML data it receives from a server. There are several files, needed to be downloaded at different time.

A number of data structures is populated with parsed data. The correspondence between files and data structures isn't 1-to-1, as a matter of fact may be rather complicated.

Application states and transitions between states depends on contents (and their availability at the moment) of those pieces of downloaded information.

Obscure spagetti code handles all the download events, and interdependences.

I've been working for a while on some pattern to work with it in a more uniform way, but thought that the developer community has already figured out the most appropriate practices and patterns. Does anyone know of any?

A: 

Probably one of the best known design patterns for event driven systems is the observer pattern.

John Downey
Observer is just a basic thing. I was thinking of something more high-level.
Maleev
A: 

What about Model View Controller (MVC)?

willcodejavaforfood
You don't go anywhere with it. The logic we're talking of is going to be a part of controller used in controller classes. What I'm looking for is micro-architectural solution.
Maleev
You might want to be more specific in your question then. Not quite sure what problem you are looking to solve. Remember that a pattern is not always the solution. If you think you have spaghetti code then refactoring it using basic OO principles to start with.
willcodejavaforfood
+1  A: 

I have been working extensively with the Model-View-ViewModel pattern for a while now and I highly recommend it. While most articles about it around the web link it firmly with WPF, there's no reason to not use it with other technologies. I've used it against a web service, WPF client and command line (three "Views" sharing lower layers).

Here's my quick and dirty description: The pattern consists of three layers (from top down): View (usually GUI, but really any external interface), ViewModel (containing business logic and working set of data) and Model (domain objects etc). Each layer communicates directly with the layer(s) below and fires events for layer(s) above.

In .Net land the pattern relies heavily on the INotifyPropertyChanged interface. When your data structures in the Model change, they could fire an event. The ViewModel can update its state (the application state) in response and fire its events. The View can then update to display the new app state.

Here's a decent article: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Once again, that article is WPF centric.

I hope this answer was at the right level for you. If not, some more information about the problem could be useful. You're not talking about semaphores, queues and threads are you?

AndrewS
I was talking more about encapsulating downloading, parsing (and waiting for smth to be downloaded) logic.
Maleev
Well I would put all of that into the Model layer and fire events when something interesting happens for higher layers to respond to. As for the specific architecture, I think it's just time for you to break out the UML diagrams...
AndrewS
+1  A: 

Application states and transitions between states depends on contents (and their availability at the moment) of those pieces of downloaded information.

Looks like you have a need for usage of tiered patterns. What I mean by that is in the example you stated above is the usage of a strategy pattern sitting on top of a state pattern.

David Yancey
+3  A: 

When you have states you will definitely need State Pattern. When you have complex rules regarding state transitions and different BL connected to this states this is the best way to go.First draw state diagram, and after that it is easy to write needed classes.

I must also agree with John for Observer patter, you could use it to make needed dependency inversion, and handle state transitions easily.

In your case you can put all BL into State Classes and process when system hits that State, you will have code separation and no spaghetti code... Code will follow and execute BL according to state transitions.

zidane