TL DR
Is it possible to use Java serialization / deserialization using the Serializable , ObjectOutputStream and ObjectInputStream classes, and possibly adding readObject and writeObject to classes that implement Serializable , as a valid implementation for the Prototype template or not?
Note
This question is not to discuss whether copy constructor is better than serialization / deserialization or not.
I know the concept of Prototype Pattern (from Wikipedia, my emphasis):
A prototype template is a design creation template for software development. It is used when the type of objects being created is determined by the prototype instance that is cloned to create new objects. This template is used for:
Avoid subclassing the object creator in the client application, as the abstract factory template does.
avoid the inherent costs of creating a new object in a standard way (for example, using the keyword "new") when it is prohibitively expensive for a given application.
And from this Q / A: Examples of GoF design patterns in major Java libraries , BalusC explains that the prototype pattern in Java is implemented by Object#clone only if the class implements the Cloneable interface (the marker interface is similar to Serializable for serializing / deserializing objects). A problem using this approach is noted in the blog posts / related Q / As questions:
So, another alternative is to use the copy constructor to clone your objects (DIY method), but this does not allow to implement a prototype template for the text, which I emphasized above:
avoid the inherent costs of creating a new object in a standard way (for example, using the keyword "new")
AFAIK, the only way to create an object without calling its constructor is to deserialize, as indicated in the example of the accepted answer to this question: How are constructors called during serialization and deserialization?
So, I just ask if deserialization of objects is used via ObjectOutputStream (and knowing what you are doing, marking the necessary fields as transient and understanding all the consequences of this process), or a similar approach would be the correct implementation of the prototype template.
Note. I do not think that unmarshalling XML documents are the correct implementation of this template, because it calls the class constructor. This probably also happens when unmarshalling the contents of JSON as well.
People will advise using the object constructor, and I would like to keep this option in mind when working with simple objects. This question is more focused on deep copying of complex objects, where I can have 5 levels of objects for cloning. For instance:
//fields is an abbreviation for primitive type and String type fields //that can vary between 1 and 20 (or more) declared fields in the class //and all of them will be filled during application execution class CustomerType { //fields... } class Customer { CustomerType customerType; //fields } class Product { //fields } class Order { List<Product> productList; Customer customer; //fields } class InvoiceStatus { //fields } class Invoice { List<Order> orderList; InvoiceStatus invoiceStatus; //fields } //class to communicate invoice data for external systems class InvoiceOutboundMessage { List<Invoice> invoice; //fields }
Let's say I need / need to copy an instance of InvoiceOutboundMessage . I do not think that in this case the copy constructor will be used. In this case, an IMO with a lot of copy constructors doesn't look like a good design.