The document outlines a lab record for a Prototype Design Pattern practical at Pandit Deendayal Energy University, focusing on object cloning in Java. It explains the Prototype Design Pattern, including shallow and deep copy concepts, and provides a Java code example for a banking-related class that implements cloning. The conclusion highlights issues in the code, such as improper cloning implementation and undeclared variables, suggesting the need for error handling and input validation improvements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views5 pages
Design Pattern Lab - Exp-4
The document outlines a lab record for a Prototype Design Pattern practical at Pandit Deendayal Energy University, focusing on object cloning in Java. It explains the Prototype Design Pattern, including shallow and deep copy concepts, and provides a Java code example for a banking-related class that implements cloning. The conclusion highlights issues in the code, such as improper cloning implementation and undeclared variables, suggesting the need for error handling and input validation improvements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5
PANDIT DEENDAYAL ENERGY UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
SCHOOL OF TECHNOLOGY SESSION 2023-24
DESIGN PATTERN LAB RECORD
NAME & ROLL NO : SUHANI PATEL (22BCP399)
MAHI SHAH (22BCP408) KRISH MODI (22BCP426) SEMESTER : 4
DIVISION : D6 (G-11)
DOMAIN : FINANCE SECTOR
COURSE CODE : 20CP210P
PROGRAM : B-TECH 2ND YEAR
LAB INSTRUCTOR : DR. NISHANT DOSHI
PRACTICAL – 4 PROTOTYPE DESIGN PATTERN
THEORY AND SOLUTION:
The Prototype Design Pattern is a creational design pattern that enables the creation of new objects by copying an existing object. The concept is to copy an existing object rather than create a new instance from scratch, something that may include costly operations. The existing object acts as a prototype and contains the state of the object. The newly copied object may change the same properties only if required. Prototype patterns are required when object creation is a time-consuming, and costly operation, so we create objects with the existing object itself. For Prototype Design Pattern, it is mandatory to have a copy feature in existing objects. If we want to create prototypes of our object, we need to implement a clone () method for that object/class. Although, it depends on our design and requirement whether to use a shallow or deep copy of the existing objects. Shallow Copy - In shallow copy, we only clone the parent object and not its containing objects. In shallow copy, we copy references of the original object, and hence if we make any changes in one of the objects, those changes get reflected in other objects as well. Deep Copy - In a deep copy, we clone the parent object as well as its containing objects. In a deep copy, we copy the original object with its values so after copying, both the objects are independent of each other, and hence, if we make any changes in one object, that change does not get reflected in the other object.
SOURCE CODE:
package factorypattern; import java.util.*;
class clone implements Cloneable {
int bal=0; int accountID; String name; int deposit;
Scanner sc = new Scanner(System.in); System.out.println("Enter your name: "); String nm = sc.nextLine(); System.out.println("\nEnter account ID: "); int id = sc.nextInt(); System.out.println("\nEnter the amount to be deposited:"); int dep = sc.nextInt(); sc.close();
c2.balance(); } catch (CloneNotSupportedException c) { } } } Output: Enter your name: Krish Enter account ID: 6780 Enter the amount to be deposited: 455 6780 Krish Account ID : 6780 Name : Krish Deposit : 455 Balance : 455 Conclusion The provided Java code defines a class called `clone`, seemingly representing an entity related to banking or accounts. It includes attributes to store account information such as account ID, name, and deposit amount. The class implements the `Cloneable` interface to enable object cloning, although the cloning method is not implemented properly. Additionally, the class contains a method `balance()` to calculate and print account details, but there is an issue with the balance calculation as it references an undeclared variable `bal`. The `main()` method serves as the program entry point, prompting the user for account information and creating instances of the `clone` class. However, the code lacks proper error handling for potential exceptions and input validation. Overall, while the code demonstrates basic object creation and method invocation, it requires improvements to handle errors and ensure correct functionality.