Prototype Design Pattern

Run Settings
Language Version
Run Command
import java.util.List; class Main { public static void main(String[] args) { Employees emps = new Employees(); emps.loadData(); //Use the clone method to get the Employee object Employees empsNew = (Employees) emps.clone(); Employees empsNew1 = (Employees) emps.clone(); List<String> list = empsNew.getEmpList(); list.add("Hassan"); List<String> list1 = empsNew1.getEmpList(); list1.remove("Hicham"); System.out.println("emps List: "+emps.getEmpList()); System.out.println("empsNew List: "+list); System.out.println("empsNew1 List: "+list1); } }
import java.util.ArrayList; import java.util.List; public class Employees implements Cloneable{ private List<String> empList; public Employees(){ empList = new ArrayList<String>(); } public Employees(List<String> list){ this.empList=list; } public void loadData(){ //افترض ان هذه البيانات اتت من قاعدة البيانات empList.add("Ismail"); empList.add("Ahemd"); empList.add("Ibrahim"); empList.add("Hicham"); } public List<String> getEmpList() { return empList; } @Override public Object clone() { List<String> temp = new ArrayList<String>(); for(String s : this.getEmpList()){ temp.add(s); } return new Employees(temp); } }
Editor Settings
Key bindings
Full width