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 / May 2006

Tip: Looking for answers? Try searching our database.

"friend" behaviour in Java

Thread view: 
andrew.fray@gmail.com - 17 May 2006 07:55 GMT
Hi guys,

Say I have a class "Owner," which is to own another class "Database."
Database owns an aggregation of another class "Record."  Owner is to
live for the length of the program, so the others will as well.

When programmers use my code I want to force them to go through Owner
and Database to get their Record instances.  In C++ I could do this by
making Database's constructor private and making Owner a friend.  I can
do the same to ensure only Database can construct an instance of
Record.

I can almost replicate this behaviour in Java using packages and by
making the relevent constructors package-visible only.

However this does not allow Record's constructor to be invisible to
Owner, and if I have many Database and Record classes all owned by
Owner then the problem increases.

There must be some natty trick to do this, but I can't think of it:
Any pointers?

Cheers,
Andrew F.
andrew.fray@gmail.com - 17 May 2006 10:18 GMT
Suppose I could make Record a subclass of Database, and Database a
subclass of Owner, and make their constructors private.  But with many
database classes that gets unweildy.  Hiding the code away in another
class isn't really an issue because programmers will be able to see it
easily in Javadoc.

Any other suggestions?
VisionSet - 17 May 2006 10:24 GMT
...
> When programmers use my code I want to force them to go through Owner
> and Database to get their Record instances.  In C++ I could do this by
> making Database's constructor private and making Owner a friend.  I can
> do the same to ensure only Database can construct an instance of
> Record.
...

> There must be some natty trick to do this, but I can't think of it:
> Any pointers?

probably want an interface above Record too but this any good?

public abstract class Record {
}

public class Database {

   private Record myRecord = new PrivRecord();

   private static class PrivRecord extends Record {

   }
}

--
Mike W
andrew.fray@gmail.com - 17 May 2006 10:28 GMT
Don't need to pull the interface out:  I used generics (there's not
much shared across the different Databases).

So the inner class seems like the way to go!

Would anyone say this was a weakness of Java compared to how other
languages can handle this problem?  Or as the inner class solution is
kind of self-documenting is this a benefit?
VisionSet - 17 May 2006 10:34 GMT
> So the inner class seems like the way to go!

nested class

--
Mike W
andrew.fray@gmail.com - 17 May 2006 10:38 GMT
lol.  WhatEVAH, man. :-)

Thanks for the reply, btw.


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



©2008 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.