Prototype Design Pattern
Aliases π: None
Description π: when the constructor is doing some extensive stuff, we may not want to call it often. To do so, we can use cloning to create subsequent objects from the first one.
Advantages β
- Simplify the creation of said objects
Disadvantages π«
- Cloning may be complex especially for a deep copy
Notes π
- None
Java implementation
public class BankAccount implements Cloneable {
public BankAccount(String accountHolder, double balance) {
// ...
}
@Override
public BankAccount clone() {
try {
return (BankAccount) super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
public class Main {
public static void main(String[] args) {
// Create a prototype BankAccount
BankAccount prototypeAccount = new BankAccount("John Doe", 1000.0);
// Create new accounts by cloning the prototype
BankAccount account1 = prototypeAccount.clone();
BankAccount account2 = prototypeAccount.clone();
// ...
}
}
If implementing Cloneable
isn't working, you can always fallback to the previous way of doing it.
public class BankAccount {
// ...
@Override
public BankAccount clone() {
BankAccount account = null;
try {
Object clone = super.clone();
if (clone instanceof BankAccount) {
account = (BankAccount) clone;
// Set BankAccount-specific parameters
// ...
} else {
throw new CloneNotSupportedException();
}
} catch (CloneNotSupportedException e) {
// fallback
account = new BankAccount(...);
}
return account;
}
}