Re: Design question

From: dar7yl (no_reply_at_accepted.org)
Date: 02/26/05


Date: Sat, 26 Feb 2005 03:27:10 GMT


"jonck" <jonck@vanderkogel.net> wrote in message
news:1109357343.516807.318900@g14g2000cwa.googlegroups.com...
> Hi,
> I have been struggling with this for days now, but it does not look
> like I'm going to figure it out. Here is the situation:
>
> I have a class that stores values that it read in from a database, lets
> call this class Storage. I display this data using a JPanel (which I
> will call DisplayPanel) with several JTextFields, JComboBoxes, JPanel's
> with pictures drawn on them, etc... So in other words, when the user
> selects a new record, the DisplayPanel loads in a new Storage class,
> and calls all the getters of Storage, something like this:
> getFirstNameTextField().setText(storage.getFirstName());
> getLastNameTextField().setText(storage.getLastName());
> getPicturePanel().drawPicture(storage.getPicture());
>
> etc...
>
> Now this is all fine, but now comes the time when the user has made
> some changes and I want to store those back into the Storage class.
> I now have to do the same thing again, but this time in reverse:
> getStorage().setFirstName(getFirstNameTextField().getText());
> getStorage().setLastName(getLastNameTextField().getText());
> getStorage().setPicture(getPicturePanel().getPicture());
>
> etc...
>
> Now there are about 20 fields per Storage class and I have about 10
> different types of Storage classes, so it seems kind of wasteful, very
> non-OOP and error-prone to be doing it as I described above. It seems
> like a pointer would be really useful here, so that I could just pass
> the pointer as the model value to the view, and then when the view is
> updated, the view can update the model, of which it has the pointer.
> But since Java is a pass-by-value language this won't work for me.
>
> Does anyone have any suggestions what a better way of doing things
> might be in the above described situation?
>
> Thanks, Jonck
>

Whatever gave you the idea that Java is pass-by-value?
As far back as I can remember, it has been pass-by-reference
for all object parameters.
You can even consider all object variables to be pointers.
(although you can't perform pointer arithmetic like C/C++)
<code>
    Object obj = new Object();
    otherobj.method(obj); // obj is not copied, but passed by reference
</code>

so you could try:
<code>
    class DisplayPanel extends Panel
    {
        String getFirstName()
        {
            return getFirstNameTextField().getText();
        }
        // ... more accessors

        void storeBack( Storage store )
        {
            store.setFirstName( getFirstName() );
            // ...
        }
    }
</code>
or



Relevant Pages

  • Re: Threading problem
    ... Your analysis about the storage being freed is the ... but if this is a pointer to free storage on the ... now you can set it up so that it disables all controls (e.g., ... LRESULT CMyForm::OnThreadDone ...
    (microsoft.public.vc.mfc)
  • Re: Difference between Char* ptr and char arrCh []
    ... Is there a difference in storage of global char* and char* inside a ... The pointer is stored in automatic storage. ... and the pointer globalCh must be writable. ...
    (comp.lang.c)
  • Re: Fragmented memory compaction ???
    ... pointer" class where every access to the object is dereferenced via an indirect pointer ... Compaction and garbage collection are not at all the same, ... a contiguous block of allocated storage, and a contiguous block of free ... even a simple, single-threaded storage allocator (I've done it, and written a book chapter ...
    (microsoft.public.vc.mfc)
  • Re: Fragmented memory compaction ???
    ... pointer" class where every access to the object is dereferenced via an indirect pointer ... Compaction and garbage collection are not at all the same, ... a contiguous block of allocated storage, and a contiguous block of free ... even a simple, single-threaded storage allocator (I've done it, and written a book chapter ...
    (microsoft.public.vc.mfc)