views:

186

answers:

9

For homework, I need to build a small java application.The main goal is to learn how to interact with XML. But a second goal is to learn how to build such an application.

I have quite some experience on programming, but little experience in designing programs. I've read about designpatterns and designprinciples like Solid and TDD. But I find it hard to know how to implement such ideas.

One problem I encounter is segregation between the GUI and the application. On the app, we want to be able to sort on different criteria. For each criteria, there would be a button (like the header of a list). This would then cause an event to be triggered. But how should I pass on what criteria should be sorted, and how would a send back the sorted data?

One could define a different event for each button, but then you would have several similar events with the only goal to differentiate between the sorting criteria.

Another approach would be to define one event, and then pass a string or int, which would specify on what criteria should be sorted, but then you would get a list of if else statements, and would not adhere to the open closed principle.

How would you design such a system.

Edit:

My biggest concern here is trying to get this 'open-closed'. As someone asked, the data is a collection of objects. Is there some way it's easy to add criteria to sort on? Or is this not really feasible to achieve?

+2  A: 

If I well understand your question, you need separate GUI and Application logic.

Look to MVC pattern. http://en.wikipedia.org/wiki/MVC

Maybe better for you is MVP pattern http://en.wikipedia.org/wiki/Model_View_Presenter

If you have experience with MVC pattern look to some Java MVC Framework for desktop applications (mostly are for web).

MicTech
It's not really about the sepparation. I've used MVC in PHP, but never in a normal app. But the problem is how to desgign the part where I have to chose where to sort on.
Ikke
+1  A: 

You also might want to check out the Observer Pattern.

For actual implementation, see this article.

Zack
By events, i'm refering to the observer pattern. That's not really the problem.
Ikke
A: 

Well if you passed an int, which corresponds to the column number you want to sort on, you wouldn't need a case statement (or a bunch of "if" statements), you could just index that column.

Vinnie
+2  A: 

It does sound a bit like your data will be displayed in a table and if you are using Swing JTable now supports sorting. Check out the JTable tutorial in the Swing trail from Sun.

I'd say as a general solution you would probably pass the criteria as an argument to whatever does the sorting though.

Another word of advice is not to worry too much about patterns, just keep your design simple and the code readable and you will be fine for a small application. It is sometimes easy to get bogged down in all the nice patterns and then try and squeeze them into your application.

willcodejavaforfood
+1 for Pattern misuse advice. Use a pattern if it solves your problem. Don't change the problem to fit the pattern.
codeelegance
Thanks for the advice. I know that if you have a brand new shiny hammer, you want everything to look like a nail. But I think principles like open-closed are allways a good goal to achieve.
Ikke
+1  A: 

It's good to see that you're thinking about the problem, which is more than can be said by an unfortunte proportion of developers.

You certainly should be looking at design patterns such as MVC. My personal favourite is the MVVM pattern. If you google it, you'll generally find it linked to WPF applications, but there is no reason that it can't be applied to a wider range of platforms (it is just a design pattern after all). I prefer to think of the 'View' as an external interface rather than a GUI.

AndrewS
A: 

Separate the presentation of the GUI from the logic of the GUI. Then you can use TDD to create the logic layer, and the presentation layer will be just simple glue code between the logic layer and the GUI framework.

Follow the links on this page for more information: http://martinfowler.com/eaaDev/ModelViewPresenter.html

Esko Luontola
+1  A: 

The best approach is to have "one event, and then pass a string or int".

Depending on the underlying data structure you use, you will not need to use a switch statement to sort the data. If you have a data table, you can use the string to specify which column to sort by. It's trickier if you have a collection of objects and the columns represent their properties. In .Net you can do neat things with LINQ that let you sort by a property represented as a string. But in java, you could always resort to reflection to get the field value to sort by.

If you have your data represented as a multidimensional array passing an int would represent the property-y index of the array to sort by. In any case there is always a technique available to avoid the switch statement for sorting.

Tion
It's a collection of objects indeed. But there is not a really standard way to solve such a problem thus?
Ikke
No, there is no standard prescribed way to solve such a problem. The best answer usually depends on the language and the type of collection you are using. Real world applications are littered with switch blocks and if statements to solve such problems. Reflection is more elegant but often has performance drawbacks.
Tion
A: 

Look at GlazedLists for filtering/sorting/etc. operations on lists, and JavaBuilders for an easy way of declaring GUI elements in a YAML file. (the first is almost necessary, the second is a convenience)

I used to shun GUI development, because it was either painful, or I needed to use some hokey graphical tool to design a GUI that worked in ways I didn't understand. But the above toolkits are pretty simple to use, and have let me concentrate my efforts on what my program is actually doing rather than how to display it.

Jason S
A: 

"One could define a different event for each button, but then you would have several similar events with the only goal to differentiate between the sorting criteria."

I actually would use the command pattern, using it an object (typically called Action) would encapsulate all the information needed to call the method (Receiver of Action).

Something along the lines of:

public enum CommandType{ 
    SORT_BY_CRITERIA_1, 
    SORT_BY_CRITERIA_2, 
    SORT_BY_CRITERIA_3; 
}

public interface Command{
     public CommandType getCommandType();
     // Any other information you need to pass to the receiver
}

public class CriteriaOneCommand implements Command{
       private final CommandType commandType;

       public CriteriaOneCommand(){
           commandType = SORT_BY_CRITERIA_1;
       }

       public CommandType getCommandType(){
           return commandType;
       }
}

Receiver

public void action(Command command){
       CommandType type = command.getCommandType();

       // If type is criteria 1, sort using that criteria
       // If type is criteria 2, sort using that criteria
}

Even if you decide to use one event, favor using enum over using int/String to differentiate between different types of events.

CaptainHastings