OOP Design Theory

Hi,

When designing a GUI Application, what is the best way to structure the objects.

Would It be best to have a functional class (the class that does all the processing work) and then have another class that is linked via composition, that handles all the GUI functionaility for that functionility (which is normally encapsulated within a window or dialog.

If so is it best for the functionility class to create and hold an instance of the GUI class or the other way round.

Many Thanks

Cheers

Alex

[540 byte] By [alexcurtisa] at [2007-11-26 18:09:20]
# 1

It's up to you and requirements of the job... there is no hard and fast rule.

Personally, I prefer to divorce my MVC components from each other along "clear" lines (drawn with interfaces), and I certainly do NOT want to deal with disentangling inheritance between them should requirements change, and I need to, for example, provide a .Net GUI "proxy interface", which does work properly on a Citrix remote desktop.

But it's Your choice.

keith.

corlettka at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 2
both ways work, it all depends how you like to structure your program.
sjs1985a at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 3
Are you familiar with MVC? http://en.wikipedia.org/wiki/Model-view-controllerAnd I usually have a builder create the M/V/C components and introduce them to each other (inversion of control and all that).
DrLaszloJamfa at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 4
I would prefer u to go with the suggestion given by other in the forum...Go with MVC design pattern ..Its efficient and u will learn more as it wil be req in future...cheers
ManujSabharwala at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 5

Hi

Yes the mvc looks like just the ticket.

Would i need to write these from scratch, making the model, view and controller seperate classes, or does java have something built in.

Sorry, im new with this MVC Concept

Cheers

Alex

Edit: The thing is doesnt swing and netbeans handle all the events, meaning all i need is a link from the ui class to the data processing class (a reference to the object) so i can associate an event with a process

Message was edited by:

alexcurtis

alexcurtisa at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 6
You must do it from scratch. MVC is just a pattern.regards,Manuel Leiria
manuel.leiriaa at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 7

You can implement the views by giving your model classes a method that returns the object data in a gui representation. If it has to be represented in different ways, supply different methods. The controller just has to decide what method to call based on what view it needs. This way you avoid having to write tons of getters and setters.

Peetzorea at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 8
Just starting to research MVC. However one thing isnt clear.Is this model designed for an individual component or can it be used for an entire window / frame interface.Many ThanksAlex
alexcurtisa at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 9

The model represents the data. This can support your entire application. It can consist of a structure of different objects.

The view is how you display your data to whoever is using your data. It's possible that you display your data in several different ways. The same model object could be displayed as a BarChart or as a list of numbers...

Your view can ask your controller for the required data from the model. The data can be used throughout your entire window. However it is also possible that your controller doesn't pass entire objects to the view, but representations that are just fit into the global picture of your window.

Hope that makes some sense...

Peetzorea at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 10

The MVC pattern is used in so many areas nowadays, I use it often when developping java web applications. Just like what the name states (Model-view-controller), the class which is acting as a controller, is responsable for receiving the requests... in a J2EE application a controller is normally a servlet to which the request is made, then this servlet gets the data from whatever you have(POJO,EJB), and forwads your request to a jsp page. It is a good practice if you start implementiing this pattern, simply because it's one of the best patterns in case you want to trully separete presentation from business logic.

MeTitus

Me_Titusa at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 11

So my controll class processes and responds to events.

Does this include updating the view such as the following code:

addDisplayListener(new DisplayListener());

class DisplayListener implements ActionListener

{public void actionPerformed(ActionEvent e)

//Sets the model values to the GUI Value

{String value = getDisplay();

modelSession.set_x(value);

}

}

would this go into my control class or the view class.

My problem is that ive got a big interface (on one frame, lots of buttons)

That means ive got to implement lots of listener classes such as DisplayListner and ButtonListener. Would I put all the listner classes in one big class of use a generic?

Also ive read up and I have to impliment my update method from the observer class:

public void update(Observable t, Object o)

Now the examples ive been given the update method seems to update the entire screen, i.e refreshes every text field etc to the model. This seems abit inefficent and clunky is there any way of getting rounf this, or have they done this is the examples for a reason.

To help the example im using is here:http://csis.pace.edu/~bergin/mvc/mvcgui.html

Thanks For your help

Cheers

Alex

alexcurtisa at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 12

> Also ive read up and I have to impliment my update

> method from the observer class:

>public void update(Observable t, Object o)

There's nothing special about java.util.Obverver/Obverable. Don't feel forced to used them -- you can whip your own code up. I think Observable consists of less than 50 lines of code!

As an example, look at JTable: TableModel, TableModelListener and TableModelEvent. The TableModelEvent identifies the type of event (INSERT, UPDATE, DELETE) as well as the range of rows involved.

One of the basic options in the observer pattern is whether you push specific details of the model change to the listener or you simply tell the listener that the model has changed and you let the listener pull the details from the model: http://en.wikipedia.org/wiki/Observer_pattern

DrLaszloJamfa at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 13
If I was to do it from scratch what implimentation key points would you suggest.Thanks for your help-Alex
alexcurtisa at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 14

> If I was to do it from scratch what implimentation

> key points would you suggest.

The key point is to use existing frameworks and focus on designing/building/coding the Model. The Model represents the core functionality of the application (what is does/the verbs of the application) which is based on domain/business requirements.

Before starting on anything related to implementation. The first step is to gather/derive/discover the domain/business requirements. Once you have the requirements, you can then start on creating a technical design, once you have a technical design, you can start on create a prototype of the application which implements a few of the highest.most important domain/business requirements.

Once you have an executable prototype, you can revisit the technical design and if everything is ok, you can work on the second phase of the prototype and implement a few more domain/business requirments.

Once you have the second iteration of an executable prototype, you can revisit the technical design and if everything is ok, you can work on the third phase of the prototype and implement a few more domain/business requirements.

When everything is implemented in the prototype, you can put the application into test mode and then when done with that put it in QA cycle....and then poof, you have a working software application. Sounds easy....good luck!

Check out the Spring Framework

GhostRadioTwoa at 2007-7-9 5:41:15 > top of Java-index,Java Essentials,New To Java...
# 15

> It's up to you and requirements of the job... there

> is no hard and fast rule.

>

> Personally, I prefer to divorce my MVC components

Design is a lot more than MVC.

Start with the objects that model the problem you're trying to solve. Worry about UI and persistence stuff later.

%

duffymoa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 16
> Check out the Spring FrameworkI second and third this: http://www.springframework.org%
duffymoa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 17

Ok guys,

ive started to whip up my own implementation of the mvc pattern.

Just a quick question (which i really whould know the answer to)

ive got a method:

updateSubscriber(JComponent comp, String value)

Now what im trying to do is provide a clean way for any gui element derived from JComponent such as JButton to update its value display when the model's specific linked attribute has been changed.

Above code is where my questions comes from. Can i state my parameter as a JComponent then pass the method a derived type such as JButton and everything still works?

Cheers

Alex

Cheers

Alex

alexcurtisa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 18
Why assume the subscriber is a JComponent? And if it is, aren't you just passing itself?
DrLaszloJamfa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 19

Hi,

Ok then, I need a update method to have a unique tag passed as an parameter

I suppose I could design the tag to be an enum.

Is it possible for java to pass an enum value as a parameter? I know enums are their own type in java...

Also is there a way to check if the enum value I present is in an enumeration. such as if (val == enumid.val)

Cheers

Alex

Message was edited by:

alexcurtis

alexcurtisa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 20

> Ok guys,

>

> ive started to whip up my own implementation of the mvc pattern.

The world holds its breath.

>

> ive got a method:

> updateSubscriber(JComponent comp, String

> value)

So it's a Swing-only UI?

> Now what im trying to do is provide a clean way for

> any gui element derived from JComponent such as

> JButton to update its value display when the model's

> specific linked attribute has been changed.

What if the model behind that component isn't a String? It's a naive model, IMO.

> Above code is where my questions comes from. Can i

> state my parameter as a JComponent then pass the

> method a derived type such as JButton and everything

> still works?

If JButton IS-A JComponent you can. But you can only call methods from the JComponent API. It's the slicing problem.

%

duffymoa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 21
OK ignore my enum question...I need a way to map in java that allows me to map a keys to a object and a specific method of that object.How would I go about this in java?Thanks For Your HelpCheersAlex
alexcurtisa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 22
You can always store a Method instance in the Map. Use the reflection API.Not very efficient. I think this is the wrong way to go, but you're free to follow it all the way to its end.Ever thought about doing some research into other Swing frameworks? Spring has one.%
duffymoa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 23
Google brings back a few: http://www.google.com/search?q=Java+Swing+frameworks&ie=utf-8&oe=utf-8&rls=org.mozilla:en-US:official&client=firefox-aEven if you feel like you must do this, you can get some ideas.%
duffymoa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 24
OK,If you dont see it as very efficent how would you do it?I dont really want to use an 3rd party library at the moment.-Alex
alexcurtisa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 25

I'd have to think about how I'd manage a general purpose Swing library. It's a bigger design challenge than I'm able to tackle on this forum.

I'm just saying that doing reflection for everything doesn't seem like the best idea to me. I'd try to make it polymorphic.

"I dont really want to use an 3rd party library at the moment." - why not? NIH syndrome isn't always healthy. If you're trying to learn something that's fine. Will you intend this for production code? I'd think twice.

%

duffymoa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...
# 26
Hi,so if you say you would do it polymorphic....Would you define an update method and let ui elements define their own versions of it.-Alex
alexcurtisa at 2007-7-21 17:16:25 > top of Java-index,Java Essentials,New To Java...