views:

73

answers:

2

I'm just getting into how to write a good architecture of a good software system, and I'm learning how to separate high level components into Layers. In this case, I'm trying to use Tiers, so as to model each Layer as a black box.

There are 4 tiers in my architecture: Presentation, Application Services, Business Logic, and Domain/Persistence. For the purposes of my question, we really only need to focus on the Presentation and Application Services.

The Application Services Layer will contain a Service that allows tracking of a certain event. The Presentation will have several views that should update dynamically as the tracking model of the events change. Inherently, it seems that I need a one-way change-propagation mechanism.

Since I'm trying to model these Layers as Tiers, I'd like to restrict communication between Facade objects for each Tier, and when necessary, allow a Tier to aggregate an object from one Tier lower, though only known by interface.

I'm programming this application in Java, so the obvious thing to use is Observable/Observer. However, I don't like that the update method for the Observer interface forces you to cast object arguments. I want to work around this by defining my own interface and class for this mechanism. The problem, then, is that the Application Logic will depend on an interface from the Presentation Tier, a certain no-no for this architecture. Is this a sign that I should try modeling with MVC fore-most and Layer the Model? Or would it be a better idea to model each of the views with an interface known in the Application Services Layer. It seems like a bad place to put it, and I'm stuck. Also, I'm using the View-Handler design pattern to handle the multiple views.

A: 

Observer/Observable is often not the best approach, especially in Java where you have to derive from Observable thus wasting your single inheritance. It also, as you discuss, causes coupling which is bad when it crosses tiers.

I would be more inclined to investigate a pure Event model, with the services providing a way to register EventListeners and firing, perhaps, a PropertyChangeEvent when the change occurs.

The Services layer could then be notifying other services, or notifying the Presentation layer -- it doesn't know and doesn't care, and only the presentation is coupled to the service by way of registering as a listener.

Stephen P
I agree - the Observer/Observable classes were added in the original java specs and were just as worthless then as they are today. I wish they would deprecate and get rid of them since all they do is confuse people and lead them down the wrong path.
rancidfishbreath
This sounds like a good solution. However, I find difficulty in understanding how the underlying model in the Services Layer propagates information to the Presentation Layer. If the Presentation contains an Action Listener, which is registers to the model, how does the view retrieve the data it needs when that PropertyChangeEvent is fired?
Mike
I assumed the services layer would provide ways to get data for the presentation layer to use, but in any case the [PropertyChangeEvent](http://java.sun.com/j2se/1.4.2/docs/api/java/beans/PropertyChangeEvent.html) has `getPropertyName()`, `getOldValue()`, and `getNewValue()` methods. Whoever fires the event should be filling in the relevant information. You could also base things on Actions and ActionListeners, or derive your own custom Events and EventListeners, depending on what makes the most sense given the (unknown to me) details of your system.
Stephen P
I ended up going with something similar to this. Took me a few days to digest, sorry for the last response. And thank you. :>
Mike
A: 

It seems to me that your question is less about Publish/Subscribe than it is how to get the layers to communicate.

Short answer:

Use MVC/MVP. Look up blog posts about them, download source code, and remember: if all you have is a hammer, everything looks like a nail. Meaning don't apply patterns because you have them, apply them because you need them.

Long answer:

If you're working in Java, I suggest Head First Design Patterns which will get you oriented to the way of thinking in patterns. After you have your head around design patterns, which I think you're on your way to now, you can look at Patterns of Enterprise Application Architecture. Feel free to skip Head First, but it is a very good book that I highly recommend if you're getting into architecture.

Once you've digested the Fowler book, or at least have a basic understanding of N-Tiered Enterprise Architecture, you should be well on your way.

Nate Noonen
You're right. My question is certainly more about Layer communication. My proposed solution is a change propagation mechanism, given the forces that the view must update as the model is changed, and that multiple views should have easy access to this model. Publish/Subscriber might not be the best way to go about it.So, with that said, are you suggesting that every time I run into this problem with a Layered architecture, that I resort to MVC? If that's true, Layers seems weak - which I'm sure it's not.
Mike
Continuing, I ran out of room. :(I read a bit of Head First Design Patterns. It was okay. I've read the Gang of Four book, Holub on Patterns, and Patterns of Software Architecture, Volume I. I was going to check out Fowler's book soon. It's hard to distinguish when to use certain patterns, because some of them are so potent.Are there any books specifically for N-Tiered? It seems like a great route to go a lot of the time (That might have been a super naive statement.)
Mike