Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Make Array Unmodifiable?

Reply
Thread Tools

Re: Make Array Unmodifiable?

 
 
Ryan Stewart
Guest
Posts: n/a
 
      06-02-2004
> "kk_oop<no spam> @yahoo.com>" <"kk_oop<no spam> wrote in message
news:40bd2807$0$2974$(E-Mail Removed)...
> See responses to Ryan below.
>
> > Ryan Stewart wrote:
> > Of course not. All the user has to do is cast it.

> Agreed. But I figured the idea was to let the client know that he

shouldn't change the class. I was thinking that if the returning interface
does not include the setter methods, the client would get the idea that >
setters should not be used. He can still cast, but at that point, I'd think
he was intentionally trying to break the encapsulation.
>
> > Design your objects to be mutable or immutable as needed.

> If the approach I mentioned above is not the way to go, how would you

recommend making the contained object mutable to the class that contains it
but immutable to everyone else?
>
> Once again, thanks for the input!
>

Please post in plain text instead of HTML. Casting is not intentionally
trying to break something. If a user wants to do something, can't figure out
how, then discovers that the class hiding under your interface does what he
wants, why wouldn't he use it? It's there, right? "Why would it be there if
I'm not supposed to use it?" might cross his mind, if he even thinks about
it. In response to your second question, ask yourself why you need an object
that's mutable in one scope and immutable in another. There may or may not
be a good reason. If there is, you might have a look at the transfer object
design pattern linked below. I don't think it's meant for this type of
problem, but it might be helpful. I've never read about it (hopefully will
do so today). If I'm way off base here, someone correct me.

http://java.sun.com/blueprints/corej...ferObject.html


 
Reply With Quote
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      06-02-2004
"Michael Borgwardt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ryan Stewart wrote:
>
> > Of course not. All the user has to do is cast it.

>
> Of course not. Users can't cast anything, only programmers. And if
> somebody is getting your system to execute his code, he can do
> *anything* he likes anyway.
>
> protected and private aren't about *preventing* people to screw
> with your code, they're about giving them hints about how your code
> should and should not be used in order to run correctly.
>

The term "user" is commonly used for someone who uses a class you've
written. Protected and private are very much about preventing people from
using your classes in unsafe ways.


 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      06-02-2004
Liz wrote:
> If the approach I mentioned above is not the way to go, how would you
> recommend making the contained object mutable to the class that contains it
> but immutable to everyone else?


Try this:

public interface MyData
{
public Object getX();
}

public interface MutableMyData extends MyData
{
public void setX(Object obj);
}

public class ImmutableMyDataAdapter implements MyData
{
private MyData base;

public ImmutableMyDataAdapter(MyData base)
{
this.base = base;
}

public Object getX()
{
return base.getX();
}
}

Then you simply say:

MutableMyData data = ...;
return new ImmutableMyDataAdapter(data);

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Pedro
Guest
Posts: n/a
 
      06-03-2004
On 2-6-2004 1:57, Ryan Stewart wrote:

> "Ken" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
>
>>Joona I Palaste <(E-Mail Removed)> wrote in message

>
> news:<c9huov$5ms$(E-Mail Removed)>...
>
>>>"kk_oop<no spam>" <"kk_oop <no spam>"@yahoo.com> scribbled the

>
> following:
>
>>So would a typical approach be to define an interface for the class
>>that contained only the readonly methods, then use this as the return
>>type for the getter? This way I'd be returning the encapsulated
>>instance, but the client would only be able to access it via the
>>readonly interface.
>>

>
> Of course not. All the user has to do is cast it.


Or use reflection. By using the reflection API it is still possible to invoke the "setter" methods
(provided they are public).
This is in particular a concern if your object is going to be used in an environment that uses
reflection to access objects, e.g.
- JavaBeans
- scripting languages (Beanshell, groovy)
- JSP expression language.
For instance, in Beanshell you can write
button = new java.awt.Button("my button");
print( button.label ); // Invoking button.getLabel()
button.label = "no, my button"; // Invoking button.setLabel("no, my button")

In such reflective enviroments, I'd create a wrapper object implementing the read-only interface,
delegating the "getter" methods to the mutable instance.

Regards,
Pedro

 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      06-03-2004
> Or use reflection. By using the reflection API it is still possible to
invoke the "setter" methods
> (provided they are public).


You can invoke a method, regardless of access modifier, using reflection.
But if were to go down that path, there are many horrible things that you
can do with reflection.
You can even mute a String!
http://www.xdweb.net/~dibblego/java/...nswers.html#q1

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.9 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? PerlFAQ Server Perl Misc 0 01-12-2011 11:00 PM
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
Uniform vector class, inheriting from Array: How to make sure thatmethods return a Vector and not an Array? Thomas Ruby 7 05-23-2005 04:21 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments