October 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Object Integrity & Security: Duplicating Objects: Part 2

  • June 11, 2007
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Designing a Copy Mechanism

One design solution is to create a method in the Dog class called copy(). This makes more intuitive sense anyway, because you initially wanted to copy an object. Despite the fact that the term clone also makes sense, I will avoid using this term because the Object class actually has a clone() method. Create an entirely new method that is inherently part of your Dog class. In this way, there will be no confusion with the clone() method of the Object class.

In short, you will wrap the cloning process into a method that you create and call copy(). To accomplish this task, we need add only two components, an attribute to reference a Dog object and the copy() method itself.

   Dog ref;

...

      ref = (Dog) clone();

      ref.setName(n);

      return ref;
   }

Note that in this strategy, the application will now call the copy() method. The clone() method is not called directly by the application; it is wrapped inside the copy() method. This approach allows you to leave the signature of the clone() method intact while allowing you to alter the attributes of the cloned object.

Note: Adding parameters to the clone() method creates a totally new method signature and does not override the original clone() method in the Object class.

The complete revision to the Dog class is presented in Listing 4.

// Class Dog
class Dog implements Cloneable {

   String name;
   String breed;

   Dog ref;

   public Dog(String n, String b) {

      name  = n;
      breed = b;
   }

   public Dog copy(String n, String b) {

      ref = (Dog)clone();

      ref.setName(n);
      ref.setBreed(b);

      return ref;

   }

   public Object clone() {
      try {
         System.out.println("Inside clone");
         return super.clone();
      } catch (CloneNotSupportedException e) {
         throw new InternalError(e.toString());
      }
   }

   public String getName() {

      return name;

   }
   public void setName(String n) {

      name = n;

   }

   public String getBreed() {

      return breed;

   }
   public void setBreed(String b) {

      breed = b;

   }

}

Listing 4: Adding the copy() method to the Dog class

The change to the application itself is relatively minor. Instead of directly calling the clone() method, you now call the wrapper method copy().

spot = (Dog)fido.copy("spot", "Mutt");

The complete code listing for the application is presented in Listing 5. Note that you are passing parameters directly to the copy() method.

// Class Duplicate
public class Duplicate {

   public static void main(String[] args) {

      Dog fido = new Dog("fido", "retriever");

      Dog spot;

      spot = (Dog)fido.copy("spot", "mutt");

      System.out.print("fido :    = ");
      System.out.print("name      = " + fido.getName());
      System.out.println(", breed = " + fido.getBreed());

      System.out.print("spot :    = ");
      System.out.print("name      = " + spot.getName());
      System.out.println(", breed = " + spot.getBreed());

   }
}

Listing 5: Adding the copy() method to the application

As you can see in Figure 7, you now have two completely separate objects, and they have their own unique attributes.

Figure 7: Using the copy() method

Conclusion

In this article, you delved further into the concepts of cloning objects. Cloning implies that when an object is cloned, it is identical to the original object. Although this functionality is necessary, it is rare that you will actually need a second, identical object. In fact, in most cases, when you want to clone an object, you will most likely want to make specific changes to the new object.

In the examples presented here, you explore a technique to provide a copy mechanism for an object. One of the issues that you always need to be cognizant of is that objects are complex; they are not primitives like ints, doubles, and so forth. Thus, the copy process is not trivial and a certain amount of attention must be paid to the design of your methods.

Even though the objects you used in these examples are not complex objects (they did not contain other objects), you still needed to be careful about how you cloned and constructed them. In upcoming articles, you will explore how you deal with cloning objects that are much more complex.

References

About the Author

Matt Weisfeld is a faculty member at Cuyahoga Community College (Tri-C) in Cleveland, Ohio. Matt is a member of the Information Technology department, teaching programming languages such as C++, Java, C#, and .NET as well as various web technologies. Prior to joining Tri-C, Matt spent 20 years in the information technology industry, gaining experience in software development, project management, business development, corporate training, and part-time teaching. Matt holds an MS in computer science and an MBA in project management. Besides The Object-Oriented Thought Process, which is now in its second edition, Matt has published two other computer books, and more than a dozen articles in magazines and journals such as Dr. Dobb's Journal, The C/C++ Users Journal, Software Development Magazine, Java Report, and the international journal Project Management. Matt has presented at conferences throughout the United States and Canada.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel