Intention to Create Copy of Java Objects


java clone.

Java application created by group of objects. Every time we create object to represent the real time objects. “new” operation creates and builds new object for the class.  But real-time scenario,  we may require to create exact copy of java object in enterprise application.  Below list of use cases to create copy of objects in java.

  • To initiate the two different operation based on same object such as generateReport() and sendEmail().
  • To implement cache in our application. We required to take exact copy and store in cache.
  • To track the state of the object in application workflow at each activity.
  • To create a new object from already existing object to save the resource and to avoid building a object by invoking all super class constructors in the class hierarchy.

Now we will look each use cases in details before we implement creation of new objects.

Independent Operation on Same Object in Parallel:

We have require the same object in different operation to achieve multithreading in java.  Let us take an example. In an ecommerce application, after order confirmed by customer,

  1. Send email to customer with complete payment and order information.
  2. Send order information to shipping team to process the order
  3. Send payment for further processing to payment team
  4. Persist order information in database.

Let us assume each operation handled by different modules and each requires order object. Instead of passing same object to all the operations, we can take exact copy of single object and send it to each module . It would mitigate the issues if any module wrongly update or delete the object properties.   Because the changes would not be visible to other module as that object is being handled and visible only in that module.

Cache Implementation:

Cache implementation requires copy of object. After cache the object, retrieve from cache should return copy of the object. It should not return actual object. For instance, we can have Calendar object for every year in the cache to avoid creation of calendar object. Cache implementation would have,

{ 2014, calendar1
2015, calendar2
2016, calendar3
2017, calendar4
2018, calendar5 }

Each time request to get calendar for “2018”, should return copy of the calendar5.  Cache implementation internally requires copy of object.

Track the State of the Object:

Critical enterprise application, we may have to save the state of object whenever the state of the object changes. With state of the object, we may require to add one or two fields and persist the object for future references.

For example, Loan request is submitted by requester in Bank. Loan object created for the each loan. It has details of loan such as name of applicant, amount and proof of document.  Every time the state of the loan object changes from one state to another, it requires to record the change data time, changing authority, comments. Each time state of the object changes

  • Copy the loan object
  • Update changing authority, change date time and comments
  • Save/persist the record for future reference to track the flow of flow from application to final approval.

Copy the exact loan object is required to implement the track and trace of object states. The real time application requires track and tracing of critical business objects.

Building complex objects:

In an enterprise application, we have to build complex object by fetching data in database and by sending REST request to external systems. If the same object requires at different component, instead of recreating the same object by complex operation, just do simply copy of earlier created object.

Let take an example of state and country.

class Country {

String name;
PopulationReport populationReport; // will be generated by fetching data from database
FinancialReport financialReport; // will be generated by fetching data from database
WeatherReport weatherReport; // will be generated by REST from external system.

// Getter setter methods.

}

class State {

Sting name;
Country country; // Complex object

// other members and getter and setters…

}

Country countryUSA = new Country();
// Generate all the members of country, populationReport,financialReport and weatherReport
State texas = new State();
state.setCountry(countryUSA);

Country usa = new Country(); // new object
// Regenerate all the members of country, populationReport,financialReport and weatherReport
State ohio = new State();
state.setCountry(usa);

Avoid recreation of new complex object. Copy the earlier complex object and use it for better performance and to avoid unnessary queries to database and external systems.

Country usa =…………… // Copy the previous object countryUSA
State ohio = new State();
state.setCountry(usa);

Discussed on intention to create  copy of objects  in java with realtime examples.  Next section, we would discuss on how to implement the copy of object, Rules of object copying and design pattern of object copy. Stay Tuned.  No Theory.. Strictly No Theory.

You may also like