• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

To the author: What to do on refactoring

 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just read a book talking about the getter and setter methods.

I wonder, for those Collection Framework data structure, some books recommend:


while some books recommend:


Which is prefered?

When should I use these 2 ways? I believe some cases, 1st method is better, while some cases, 2nd method is better.

Thanks.

Nick
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for me, I do prefer the second method, where we can add, remove as well as get a collection of objects...

But the first method is like a traditional bean style... But in the signatures that you provided, the first method deals with the collection as a whole, while the second method deals with individual objects...

Well, as I mentioned before, I do prefer the second method...

I hope Joshua can shed some light on us about these methods...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
I just read a book talking about the getter and setter methods.

I wonder, for those Collection Framework data structure, some books recommend:



This is OK if you actually always want to exchange or access the whole collection, I'd say.

Problems arise when you use that interface to write code such as

foo.getXxx().add(myXxx);

The problem is that foo doesn't have control about it's innards - encapsulation is violently broken.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ko Ko,

I also prefer the 2nd way, however, just wonder why some books will recommend for the 1st one.

Nick
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The problem is that foo doesn't have control about it's innards - encapsulation is violently broken.


In such case, any methods to avoid the case?

We create our own *Collection* such that it does not provide any ways to get the *real* collection?

Nick
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:

We create our own *Collection* such that it does not provide any ways to get the *real* collection?



I think it doesn't matter, because what we actually care about is the objects in the collection, not the collection itself... If we got a way to get the objects inside the collection, it's alright for an application to handle other things, IMO...
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:

In such case, any methods to avoid the case?



Well, don't expose the collection to the outside. Instead, provide the desired behaviour.

To be more specific, I needed a concrete example...
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the 1st one is enough generic to be used.
addXXX is equivalent with getCollection().addXXX and so on.
The drawbacks are that you cannot impose data type checking. Also if getCollection implies important work on the background (for example loading the children from the database) than the 2nd option is better.

./pope
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good posts, gang! See: Don't Confuse Your Dog!
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:
Also if getCollection implies important work on the background (for example loading the children from the database) than the 2nd option is better.



Problem is, if you go with option 1 and *later* want to change your class to do some more work, you might find that there are already many clients depending on the interface that exposes the collection, and changing it to the 2nd option is a whole lot of work - or even impossible, because not all of the clients are under your control.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Problem is, if you go with option 1 and *later* want to change your class to do some more work



I see a separation of things here. Some more work (internal stuff) is not related to the exposed interface.
However the things are the same if you want to go the other way around :-). Hopefully you can expose all 4 methods.

./pope
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say you decide that you not only want to remember the objects in the collection, but you also want to register a listener to every that is added, and remove it when an object is removed. When objects are solely added through add and remove methods on the class, this is trivial. If you are exposing the whole collection to clients, this complicates matters significantly.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As both of the choices have a Collection getXXX() method, I'm not sure what we are comparing. Does one of them return a read only collection or am I missing something?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


As both of the choices have a Collection getXXX() method, I'm not sure what we are comparing. Does one of them return a read only collection or am I missing something?


I saw both choices from different refactoring books. Some books recommend that we should change choice 1 into choice 2, while other books recommend for changing from choice 2 into choice 1.

That's why I wonder, why different books take different approaches, and what should be the case, or *real* case that we need to perform either change.

Nick
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
As both of the choices have a Collection getXXX() method, I'm not sure what we are comparing. Does one of them return a read only collection or am I missing something?



Well, the first approach adds the collection as a whole, while the second one adds the objects in the collection one by one... That's the main difference, Jeane...

Just my 2 cents...
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
I saw both choices from different refactoring books. Some books recommend that we should change choice 1 into choice 2, while other books recommend for changing from choice 2 into choice 1.


Which books might these be? I can't figure out why a refactoring book would recommend doing something like that. In fact, Martin Fowler's Refactoring included "reverse" refactorings for many of the refactorings presented. There's rarely a single, universally best answer in software design.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which books might these be?


I cannot remember both books. But the 2nd apporaches, the add/remove methods are proposed in Fowler's book.

Nick
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


There's rarely a single, universally best answer in software design


True, that's why I wanna know what situation should use which method.

Nick
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:

I cannot remember both books. But the 2nd apporaches, the add/remove methods are proposed in Fowler's book.

Nick



Do you know the name of the refactoring? Didn't find it...
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Do you know the name of the refactoring? Didn't find it...


Are you perhaps referring to Fowler's "Encapsulate Collection" refactoring (page 208)?

It does have a point in that you often don't want the clients of a class modify the collection's contents without its consent. This is not always the case, however. Sometimes you *want* to let clients edit the collection or replace the existing collection with a single setter method.

Obviously, the mere existence of a refactoring like "Encapsulate Collection" should not be interpreted as a blanket statement along the lines of "one should always encapsulate collections".
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
Obviously, the mere existence of a refactoring like "Encapsulate Collection" should not be interpreted as a blanket statement along the lines of "one should always encapsulate collections".



True - you should only do that if your fear of the consequences I highlighted above are stronger than your desire for frugal code. It certainly depends on the case at hand...
 
author
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If clients ought not to be able to change a collection, it's good to return a read-only version of the collection. On the other hand, if a client obtains a collection only to ask a question of that collection (like how many elements it has) then the design could have an odor of primitive obsession (see Fowler and Beck's chapter on Code Smell in [Refactoring]). In that case, it's best to give clients methods that let them access information directly (e.g. numberOfElementsInCollectionX().

I tend to encapsulate collections when I don't want clients to have to deal with type-casting (I'm not using JDK 1.5 yet). Fowler's Encapsulate Collection shows how to do this.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joshua Kerievsky:
If clients ought not to be able to change a collection, it's good to return a read-only version of the collection.


Joshua, could you explain me a bit what you mean by read-only version of collection. Since we get a set of objects from the main collection. Then we can do whatever we want with that set i.e., we can even modify the objects inside it, isn't it?

On the other hand, if a client obtains a collection only to ask a question of that collection (like how many elements it has) then the design could have an odor of primitive obsession (see Fowler and Beck's chapter on Code Smell in [Refactoring]). In that case, it's best to give clients methods that let them access information directly (e.g. numberOfElementsInCollectionX().


That method would better to be used, if the requirement is just to know about the collection like you said...

BTW, does ur book discuss about the issue that was raised by Nick, the original poster? Thanks...
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ko Ko Naing:


Joshua, could you explain me a bit what you mean by read-only version of collection.



You could return a copy of the collection instead of the real one. So any changes made to it will not be reflected in the original one. This is tricky as the other dev can expect this to happen.

./pope
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:


You could return a copy of the collection instead of the real one. So any changes made to it will not be reflected in the original one. This is tricky as the other dev can expect this to happen.

./pope



Do u think Joshua mean it so? I think that he was talking about the ability to modify the objects that are in the extracted collection...

Thanks for your comment too...
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have also picked this approach for unmodifiable Map which disallow the system add more data to it after it has been created and initialized.

Nick
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
We have also picked this approach for unmodifiable Map which disallow the system add more data to it after it has been created and initialized.

Nick



Nick but how do you tell the API users that the returned Map will not affect the original map? (just wondering, as I have this problem too).

./pope
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use:



HTH.

Nick
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ko Ko Naing:
Joshua, could you explain me a bit what you mean by read-only version of collection. Since we get a set of objects from the main collection. Then we can do whatever we want with that set i.e., we can even modify the objects inside it, isn't it?


You can return a deep copy of the collection, i.e. create a new collection which contains copies of all the objects in the original collection. Of course, if those objects inside the collection contain references to other objects, you'll have to deep copy those as well, and so on.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:

You can return a deep copy of the collection, i.e. create a new collection which contains copies of all the objects in the original collection. Of course, if those objects inside the collection contain references to other objects, you'll have to deep copy those as well, and so on.



Thanks a lot, Lasse, for the technique... I think that Joshua is also talking about this issue in the previous posts above...
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ko Ko,

You should know the technique, as it is supported by Tiger.

Nick
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
You should know the technique, as it is supported by Tiger.


Which technique? Deep copying? Is it supported by J2SE 1.5 in some new way (other than manually implementing java.lang.Object#clone() to perform deep copying)?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The drawbacks are that you cannot impose data type checking.


Use java tiger.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:
Use java tiger.


Considering that some Fortune 500 companies are still using J2SE 1.3, I guess Tiger will be reality in those places around 2005-2006...
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tiger will soon or later be popular, so we should learn it now.

Nick
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The drawbacks are that you cannot impose data type checking


Tiger's Generics can handle this issue.

Nick
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which technique? Deep copying?


Exactly. I think this is one of the features that will be used frequently, as I dont need to check the objects layer by layer.


Is it supported by J2SE 1.5 in some new way (other than manually implementing java.lang.Object#clone() to perform deep copying)?


Yes, this is supported in Tiger.

Nick
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone please tell me exactly how has java.lang.Object#clone() changed between Java 1.4 and 1.5? As far as I know, the functionality is unchanged (i.e. if you want deep copying, you have to implement it yourself by implementing the clone() method in a particular way)
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+1 on Lasse's question :-). (I don't know 'bout a new solution).

./pope
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:

Yes, this is supported in Tiger.

Nick



Is deep copying really supported in Tiger without clone() method? I'm also eager to know that...
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic