views:

89

answers:

3

I'm building a Java application that retrieves information from differing information sources. I'm using a sensor, a set of RecordStores and a few online web services.

In the basic application all these resources are loaded by default. Some of these sources are directly read in a separate thread (load database, read sensor etc.). From this basic application some functions are started that use these sources.

The classes that contain these functions (usually combining multiple sources information) are currently paramterized with a set of resources like this:

AppPart a1 = new AppPart(source1, source2, source3);

I could also do it like this

AppPart a1 = new AppPart(this);

..where I need to make my sources public in order to read them.

I'm also thinking about a kind of stage/collection to publish all sources on, e.g:

public SourceCollectionStage sStage = new SourceCollectionStage();

and later:

 sStage.getSensor();

.. for example.

What do you guys think is the best or most often used way to do this?

Btw: the user interface is often implemented by the specific funtion classes, not by the main class. Should I do this in another way?

A: 

Have you considered using the Strategy Pattern?

Summary of Strategy pattern (from Wikipedia)

The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.

From your statement,

I'm also thinking about a kind of stage/collection to publish all sources

The Observer Pattern can be used as a publish/subscribe fashion.

The Elite Gentleman
Why do you think this is the right solution for me? - I'm not familiar with the pattern unfortunately... in the mean time I've read about the pattern and I don't think this is the right solution.
hsmit
You can structure the way you retrieve your information (sensor, web service, recordstore, etc.) by using the strategy pattern. That way, you encapsulate the way you retrieve information, and make it interchangeable.
The Elite Gentleman
+1  A: 

I think Observer pattern might be of help to you here.

You can have a hierarchy of classes that wraps the sources and which implement the Subject interface. The Appstore can be a registerd as Observer with the sources which it is interested to get details of.

sateesh
maybe I can use such an approach
hsmit
A: 

I needed to solve it in some other way due to the fact that my application needed restructuring only (not designing the structure from scratch). I used a very simple setup: a controller class that loads all sources. From the application I can call this controller class and load the specific resource. I think it comes close to a MVC pattern.

hsmit