September 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Object Integrity & Security: Duplicating Objects, Part 3

  • July 6, 2007
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Multiple Heads

To take care of this problem, you must physically create a second head object and assign it to the new Dog object, spot. As usual, there are several ways that you could do this. In this case, you will explicitly create a new head when you copy the Dog object, inside the actual copy( ) method.

ref = (Dog)clone();

Head h1 = new Head(no);

ref.head = h1;

This is a 3-step process. First, the Dog object is cloned. Second, the new Head object is created. Third, you assign the newly created head object to the reference of the cloned Dog object. The Head class is presented in Listing 6 and the completely redesigned Dog class is seen in Listing 7. The Duplicate class is contained in Listing 8.

Listing 6: The new Head class

// Class Head
class Head {

   String nose;

   public Head(String no) {

      nose = no;

   }

   public String getNose() {

      return nose;

   }
   public void setNose(String no) {

      nose = no;

   }


}

Listing 7: The new Dog class

// Class Dog
class Dog implements Cloneable {

   String name;
   String breed;

   Dog ref;

   Head head;

   public Dog(String n, String b, String s) {

      name = n;
      breed = b;
      head = new Head(s);

   }

   public Dog copy(String n, String b, String no) {

      ref = (Dog)clone();

      Head h1 = new Head(no);

      ref.setName(n);
      ref.setBreed(b);
      ref.head = h1;

      return ref;

   }

   public Object clone() {
      try {
         System.out.println("Inside Dog 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 8: The new Duplicate class

// Class Duplicate
public class Duplicate {

   public static void main(String[] args) {

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

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


      Dog spot;

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

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

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

   }

}

When the redesigned application is executed, you can see by the results in Figure 3 that you now retain the proper value of the nose attributes for the fido object. The spot object also has the proper value for its nose attribute.

Figure 3: Separate Head objects

Finally, Diagram 3 provides a graphical representation of what the final version of the code looks like in memory. You have two Dog objects, fido and spot. And each of these objects has its own, separate Head object; fido has a black nose and spot has a pink nose.

Diagram 3: Separate Head Objects

Conclusion

Attempting to keep track of complicated object relationships is a daunting task. Consider the responsibility of modeling complex objects. Think about one of the more common objects, an automobile. This provides a real-world example of how object relationships are modeled. Each car contains so many separate parts that it is hard to even list them. On top of that, take into account how many different parts are manufactured at different sites.

The obvious example is that of an engine. Each car (an object) contains an engine (another object); however, the engine is made up of other parts, like pistons (an object) and camshafts (another object). Pistons are made up of even more objects. And I could go on and on.

In a world of hardware, there are physical objects to help with the manufacturing process; for example, you can hold a piston in your hands. Yet, when modeling an engine in software, you have to be aware of all these relationships and how you will duplicate them. It is important to pay attention to this process; otherwise, it can lead to major confusion.

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 5 of 5



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel