Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Read Only class

Thread view: 
Gilbert Ostlethwaite - 21 Apr 2006 11:46 GMT
Hi

I have a class which is basically a data holder object with attributes
exposed via getter and setter methods. Obviously I need the class that
creates my data holder object to be able to access the setter methods,
but when I "expose" my data holder class to the rest of the application
I only want the getter methods to be available.

Now I can do this by creating a wrapper object with only getter methods
that delegate to the wrapped data holder, but is there another/better
way of doing this?

Regards
Gilbert
cklutz@gmail.com - 21 Apr 2006 11:54 GMT
> Hi
>
[quoted text clipped - 10 lines]
> Regards
> Gilbert

You could use interfaces. Something like this:

public interface Foo {
   // Only the get methods
}

public class FooImpl implements Foo {
  // Implement get methods of Foo
  // "Add" set methods
}

public class FooFactory {
 Foo createFoo() {
   return new FooImpl();
 }
}
chris brat - 21 Apr 2006 12:19 GMT
Idea 1 :
Place the two classes in their own package, make the setter methods
either protected or default - probably protected till you need to
extend the class then make them protected.

Make the getter methods public so that classes outside the package can
read them.

The problem with this is that you need all classes that will create the
data holder objects to be in the same package as the data holder class
- this is not always possible or suitable.

Idea 2:
Make the data holder class an inner class of the class that creates it.
Make the getter methods public, protected or default depending on who
you want to access it and make the setter methods private (only
accessible from the class that creates the data holder object).

This also has a drawback that only one class will ever create the data
holder object.

Idea 3 :
Have your data holder implement an interface
EditableDataHolderInterface which extends
NonEditableDataHolderInterface.

- NonEditableDataHolderInterface contains only the getter methods and
will be used by all your classes which should only do a lookup.

- EditableDataHolderInterface will contain only the setter methods and
will be used by only the classes that create the object and should be
able to use the setters.

This way you can pass an an EditableDataHolderInterface reference to
the DataHolder class through to a method that expects a
NonEditableDataHolderInterface reference (normal inheritance rules).

Chris
Chris Uppal - 21 Apr 2006 13:08 GMT
> I have a class which is basically a data holder object with attributes
> exposed via getter and setter methods. Obviously I need the class that
> creates my data holder object to be able to access the setter methods,
> but when I "expose" my data holder class to the rest of the application
> I only want the getter methods to be available.

The obvious way is to put the two classes into the same package, but not in the
package as the rest of your application.

If that's not suitable, then here's one more approach.  It has the advantage
that you can create "dummy" DataObjects using only the public methods if you
wish (for testing etc).  It's a bit repetitive, so you'd want to generate most
of the code automatically.

Arguably the RawData interface should be in the dataobjects package instead of
being packaged with the builders which use it.

   -- chris

=====================
package dataobjects;

public class
DataObject
{
   private int m_this, m_that;

   public int getThis() { return m_this; }
   public int getThat() { return m_that; }
   // etc

   public DataObject(databuilders.RawData raw)
   {
       m_this = raw.getThis();
       m_that = raw.getThat();
   }
}
=====================
package databuilders;

public interface
RawData
{
   int getThis();
   int getThat();
   // etc
}
=====================
package databuilders;
import dataobjects.*;

public class
Builder
{
   static class
   TmpDataHolder
   implements RawData
   {
       int m_this, m_that;

       public int getThis() { return m_this; }
       public int getThat() { return m_that; }
       // etc
   }

   public DataObject
   build()
   {
       TmpDataHolder data = new TmpDataHolder();
       data.m_this = 22;
       data.m_that = 33;
       // etc

       return new DataObject(data);
   }
}
=====================
mmazzotty@gmail.com - 21 Apr 2006 13:54 GMT
> I have a class which is basically a data holder object with attributes
> exposed via getter and setter methods. Obviously I need the class that
> creates my data holder object to be able to access the setter methods

i'd let the constructor take initial parameters...

> but when I "expose" my data holder class to the rest of the application
> I only want the getter methods to be available.

...and only implement getter methods


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.