Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8307915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:38:45+00:00 2026-06-08T18:38:45+00:00

While exercising your DDD skills, suppose you have object like this: class Person {

  • 0

While exercising your DDD skills, suppose you have object like this:

class Person {
    Set<Address> addresses = new HashSet<Address>();
}

Is it more appropriate to allow normal/full access to the collection:

Set<Address> getAddresses() {
    return addresses;
}

Which would allow the caller to add/remove addresses as they see fit, or, is it better to make callers go through the Person object in this case:

Set<Address> getAddresses() {
    return Collections.unmodifiableSet(addresses);
}

void addAddress(Address address) {
    addresses.add(address);
}

void removeAddress(Address address) {
    addresses.remove(address);
}

The first case saves us from creating extra methods; the second case allows the Person object to be aware of changes to its addresses (in case it cared for one reason or another).

Is there a best-practice here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:38:47+00:00Added an answer on June 8, 2026 at 6:38 pm

    I prefer the second approach, but I believe that the unmodifiable set buys you very little other than undesirable surprises in the client.

    Had you wished to convey to the client that this collection is immutable, then you should have used a collection type which withheld mutability in its API (rather than it its implementation). That is, invoking a method which is declared to exist and catching a UnsupportedOperationException is like finding a unit on the woman with whom you have had your first date.

    I love Josh Bloch, but I think he messed up on this one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While reading this class BitmapFactory I noticed that almost all methods inside are static.
While I'm developing Android-Apps I like to have a look at internal SDK implementations.
while working with linq query i'm receiving error System.NullReferenceException: Object reference not set to
While using boost::threads I have come across this interruption problem. When I do a
While refactoring my code base I found a piece of code which I'd like
I've made an iPhone app to be used while exercising. It plays a bell
While calling XslCompiledTransform.Transform() method I get this exception : The Writer is closed or
While installing Visual Studio 2010 Service Pack 1, I am getting this error: Installation
While executing this below Dojo code the call back mehod is calling the onFailure.
While looking at various options for making dropdowns more user-friendly I came across this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.