Skip to content

Commit 9c4c0ba

Browse files
committed
Prototype Pattern
1 parent 738f614 commit 9c4c0ba

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.design_pattern.prototype;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Employee implements Cloneable{
7+
8+
9+
private List<String> empList;
10+
public Employee(){
11+
empList = new ArrayList<String>();
12+
}
13+
14+
public Employee(List empList){
15+
this.empList = empList;
16+
}
17+
18+
public void loadData(){
19+
empList.add("Manoj");
20+
empList.add("Rajkumar");
21+
empList.add("Durgesh");
22+
empList.add("Roshan");
23+
}
24+
25+
26+
public List getEmp(){
27+
return empList;
28+
}
29+
30+
@Override
31+
public Object clone() throws CloneNotSupportedException{
32+
33+
List<String> temp = new ArrayList<String>();
34+
for (String name : this.empList) {
35+
temp.add(name);
36+
}
37+
38+
return new Employee(temp);
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.design_pattern.prototype;
2+
3+
import java.util.List;
4+
5+
public class PrototypePattern {
6+
7+
public static void main(String[] arg) throws CloneNotSupportedException{
8+
Employee emp = new Employee();
9+
emp.loadData();
10+
11+
Employee emp1 = (Employee)emp.clone();
12+
List<String> list1 = emp1.getEmp();
13+
System.out.println("====");
14+
System.out.println(list1);
15+
list1.add("Nitin");
16+
System.out.println(list1);
17+
18+
Employee emp2 = (Employee)emp.clone();
19+
List<String> list2 = emp2.getEmp();
20+
System.out.println("====");
21+
System.out.println(list2);
22+
list2.remove("Rajkumar");
23+
System.out.println(list2);
24+
System.out.println(emp.getEmp());
25+
}
26+
27+
}

0 commit comments

Comments
 (0)