Tuesday, December 1, 2015

[Java] Clone Object trong Java

Trong nhiều trường hợp ta muốn clone object ra một object tương ứng để giúp chúng ta thao tác trên object mà không làm ảnh hưởng đến object gốc.

Trong java việc này ra đơn giản. Để clone một object ta implement Cloneable như trong ví dụ sau.
public class Employee implements Cloneable {
private String EmployeeID;
private String FullName;
public Employee() {
}
public String getEmployeeID() {
return EmployeeID;
}
public void setEmployeeID(String employeeID) {
EmployeeID = employeeID;
}
public String getFullName() {
return FullName;
}
public void setFullName(String fullName) {
FullName = fullName;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Sử dụng như sau:
Employee employee = new Employee();
employee.setFullName("Đức");
try {
Employee employeeClone = (Employee) employee.clone();
employeeClone.setFullName("Đức cloned");
employee.getFullName(); // trả về Đức
employeeClone.getFullName(); // trả về Đức cloned
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
OK vậy ta đã tạo được object clone. Nhưng với các object phức tạo như chứa các list con, trong list con lại còn list con bé hơn nữa. Với các object phức tạp như thế này thì đòi hỏi các object con phải implements Cloneable và for để tạo ra các object clone. Sử dụng cách này rất mất công và tốn time (chưa xét hiệu năng).
Và cách mà em đưa ra là tận dụng thư viện GSon để clone object. Như ví dụ trên để tạo Object Clone em làm như sau:
Employee employee = new Employee();
employee.setFullNam
c");
Gson gson = ne
n();
String jsonObject = gson.toJson(e
ee);
Employee employeeCloned = gson.fromJson(jsonObject, Employe
ss);
employeeCloned.getFullName();
Với cách này ta sẽ được một Object cloned với toàn bộ các property một cách đơn giản và nhanh chóng.
Download thư viện gson tại https://github.com/google/gson

7 comments:

  1. Your posts are the flower of the flock.Thanks for your sharing.Please update more posts.Your writings supply information and knowledge I need.
    currency converter box by autoketing currency-converter-app

    ReplyDelete
  2. I really want to express my gratitude to you.I have a fancy for your posts.Learn captivating things. i'll be actual to check verify inside the works anew particular
    https://bit.ly/2QslLr4
    https://autoketing.com/project/sale-pop-master, autoketing.com

    ReplyDelete
  3. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
    gaigoionline, clip gai goi, gai goi hn, gai bao, gai goi hcm

    ReplyDelete
  4. I am crazy about this blog. I have received a bunch of information.I don't know what to say except that I have enjoyed reading. I really appreciate to meet to it and i emphasize to this blog. My curiosity to learn more and more on this blog.
    Games of abcya3 motox3m3 games online abcya3 2 player

    ReplyDelete
  5. here is such a great amount in this article I would never have considered all alone.The site is very interesting, you made some compelling remarks and the matter is on point. Much thanks again.
    Ride The Bus Simulator best ride game Mommy Elsa Baby Caring play for free game online Tomb Runner

    ReplyDelete
  6. I hope I can see more posts.When I have spare time, I often read your posts.I love putting together useful information, this particular publish has got me personally a lot more info
    Real Flight Simulator best game for free online game Def Island online game Papa's Scooperia

    ReplyDelete
  7. this is truly decent to read..informative post is great to read..thanks a considerable measure! I haven't any word to welcome this post.....Really i am inspired from this post....the individual who make this post it was an awesome human..thanks for imparted this to us
    io jogos for school
    free online juegos games
    play 2 player games

    ReplyDelete