Categories
Design Patterns Using Java

Prototype Design Pattern Using Java

What We will be Learn Today?

  • Implementing Prototype Design Pattern Using Shallow Cloning in JAVA
  • Implementing Prototype Design Pattern Using Deep Cloning in JAVA

Using Shallow Cloning

In Shallow Cloning we won’t get the exact data of the object that we clone, Lets see practically

Step1: Create a Book class

package com.Prototypedesignpattern;
public class book
{
	private int bid;
	private String bname;
	public int getBid() 
	{
		return bid;
	}
	public void setBid(int bid) 
	{
		this.bid = bid;
	}
	public String getBname() 
	{
		return bname;
	}
	public void setBname(String bname) 
	{
		this.bname = bname;
	}
	@Override
	public String toString()
	{
		return "book [bid=" + bid + ", bname=" + bname + "]";
	}

}

Step2: Create class for bookshop

package com.Prototypedesignpattern;

import java.util.List;
import java.util.ArrayList;

public class bookshop implements Cloneable
{
	private String shopname;
	List<book> books = new ArrayList<>();
	public String getShopname() {
		return shopname;
	}
	public void setShopname(String shopname) {
		this.shopname = shopname;
	}
	public List<book> getBooks() {
		return books;
	}
	public void setBooks(List<book> books) {
		this.books = books;
	}
	public  void loadData()
	{
		for(int i=1;i<=10;i++)
		{
			book b = new book();
			b.setBid(i);
			b.setBname("Book" + i);
			getBooks().add(b);
			
		}
	}
	@Override
	public String toString() {
		return "bookshop [shopname=" + shopname + ", books=" + books + "]";
	}
	
/* This is for Shallow Clonning

protected Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	  
	

}

Step 3: Main Class

package com.Prototypedesignpattern;
/*
 * This is a Prototype Design Pattern
 * Here In this type of pattern we are going to clone the object and it can be done in two ways
 * Shallow cloning and Deep Clonning
 *
 */

public class Main
{

public static void main(String[] args) throws CloneNotSupportedException
	{
		bookshop bs=new bookshop();
		bs.setShopname("BTAO");
		bs.loadData();
		/*In Shallow Cloaning we wont get the exact data of the object that we clone 
 */
		bookshop bs1=(bookshop)bs.clone();
		bs.getBooks().remove(2);
		bs1.setShopname("BTAO2");

		System.out.println(bs);
		 System.out.println(bs1);
		
	}

}

Output:

bookshop [shopname=BTAO, books=[book [bid=1, bname=Book1], book [bid=2, bname=Book2], book [bid=4, bname=Book4], book [bid=5, bname=Book5], book [bid=6, bname=Book6], book [bid=7, bname=Book7], book [bid=8, bname=Book8], book [bid=9, bname=Book9], book [bid=10, bname=Book10]]]


bookshop [shopname=BTAO2, books=[book [bid=1, bname=Book1], book [bid=2, bname=Book2], book [bid=4, bname=Book4], book [bid=5, bname=Book5], book [bid=6, bname=Book6], book [bid=7, bname=Book7], book [bid=8, bname=Book8], book [bid=9, bname=Book9], book [bid=10, bname=Book10]]]

Using Deep Cloning

In Deep Cloning we get the exact data of the object that we clone, Lets see practically

We Just replace the following method in the bookshop class

protected bookshop clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		bookshop shop =new bookshop();
		for(book b: this.getBooks())
		{
			shop.getBooks().add(b);
		}
		return shop;
	}

Leave a Reply

Your email address will not be published. Required fields are marked *