Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

immutable object concept

 // example immutable object
  public class Dog {
      private String name;
      private Date birthDate;
 
      public Dog(String name, Date birthDate) {
          this.name = name;
          this.birthDate = birthDate;
      }
 
      public String getName() {
          return name;
      }
 
      public Date getBirthDate() {
          // see note below!
          return (Date) birthDate.clone();
      }
  }
Creating immutable objects can be tricky. In the above example, the birthDate field can be modified, so we don't want to return a reference to it. Instead, clone the object and return a copy. This ensures that nobody can modify the Dog instance.

Immutable objects do not prevent you from changing data. You just have to replace the existing object with a new one that has your updated data. One simple approach is to use a manager object (which is thread safe) to create and replace immutable objects. For example:

// This class provides thread-safe methods to add/remove/update dogs.
// An actual implementation may use a relational database as its
// underlying storage mechanism.
public class Kennel {
    public synchronized void create(Dog dog) { ... }
    public synchronized void update(Dog orig, Dog modified) { ... }

can you please advise on immutable objects concept as explained in below link.
http://www.ociweb.com/resources/publications/sett/april-2000tips-for-creating-thread-safe-code-avoiding-race-conditions/
I have not undersood why it is tricky and why
the birthDate field can be modified, so we don't want to return a reference to it. Instead, clone the object and return a copy. This ensures that nobody can modify the Dog instance.
Please advise
SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

how to add clone method?I am still not clear. please advise
Avatar of gudii9

ASKER

package ee;

import java.util.Date;

public class Dog {
	private String name;
	private Date birthDate;

	public Dog(String name, Date birthDate) {
		this.name = name;
		this.birthDate = birthDate;
	}

	public String getName() {
		return name;
	}

	public Date getBirthDate() {
		return birthDate ;
	}

	@Override
	public String toString() {
		return "Dog{" +
				"name='" + name + '\'' +
				", birthDate=" + birthDate +
				'}';
	}
	
	@Override
	public String clone() {
	return null;
	}

	public static void main(String[] args) {
		// Let's create a dog with a birthdate of today
		Date today = new Date() ;
		Dog dog = new Dog("Fido", today) ;

		System.out.println("Before: " + dog) ;

		// But now we can get the Date object from the dog
		// and change it - which changes the dog object
		// So 'dog' is not immutable
		Date birthday = dog.getBirthDate() ;
		birthday.setTime(0);

		System.out.println("After:  " + dog) ;

	}
}

Open in new window


above gave below output
Before: Dog{name='Fido', birthDate=Wed Oct 28 14:49:25 EDT 2015}
After:  Dog{name='Fido', birthDate=Wed Dec 31 19:00:00 EST 1969}


How do i test using the clone method to see how it does not change?
Avatar of gudii9

ASKER

http://www.tutorialspoint.com/java/lang/object_clone.htm

I looked at above link but does not explain relating to this scenario of question. please advise.
import java.util.GregorianCalendar;

public class ObjectDemo {

   public static void main(String[] args) {

   // create a gregorian calendar, which is an object
   GregorianCalendar cal = new GregorianCalendar();

   // clone object cal into object y
   GregorianCalendar y = (GregorianCalendar) cal.clone();

   // print both cal and y
   System.out.println("" + cal.getTime());
   System.out.println("" + y.getTime());
   }
}

Open in new window


i do not see any clone method also in above code.

I got below output though which is unchanged

Thu Oct 29 11:17:54 EDT 2015
Thu Oct 29 11:17:54 EDT 2015