这里为什么要用clone?
Why is clone used here?
我正在阅读 Vaadin 教程并遇到了这段代码。但是我无法理解 clone
在这种情况下的目的。谁能解释一下?
/**
* Persists or updates customer in the system. Also assigns an identifier for
* new Customer instances.
*
* @param entry
*/
public synchronized void save(Customer entry) {
if (entry == null) {
LOGGER.log(Level.SEVERE,
"Customer is null. Are you sure you have connected your form to the application as described in tutorial chapter 7?");
return;
}
if (entry.getId() == null) {
entry.setId(nextId++);
}
try {
entry = (Customer) entry.clone();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
contacts.put(entry.getId(), entry);
}
/**
* Sample data generation
*/
public void ensureTestData() {
if (findAll().isEmpty()) {
final String[] names = new String[] { "Gabrielle Patel", "Brian Robinson", "Eduardo Haugen",
"Koen Johansen", "Alejandro Macdonald", "Angel Karlsson", "Yahir Gustavsson", "Haiden Svensson",
"Emily Stewart", "Corinne Davis", "Ryann Davis", "Yurem Jackson", "Kelly Gustavsson",
"Eileen Walker", "Katelyn Martin", "Israel Carlsson", "Quinn Hansson", "Makena Smith",
"Danielle Watson", "Leland Harris", "Gunner Karlsen", "Jamar Olsson", "Lara Martin",
"Ann Andersson", "Remington Andersson", "Rene Carlsson", "Elvis Olsen", "Solomon Olsen",
"Jaydan Jackson", "Bernard Nilsen" };
Random r = new Random(0);
for (String name : names) {
String[] split = name.split(" ");
Customer c = new Customer();
c.setFirstName(split[0]);
c.setLastName(split[1]);
c.setStatus(CustomerStatus.values()[r.nextInt(CustomerStatus.values().length)]);
c.setBirthDate(LocalDate.now().minusDays(r.nextInt(365 * 100)));
save(c);
}
}
}
注:Customer
有以下数据成员:private Long id, private String firstName = "", private String lastName = "", private LocalDate birthDate, private CustomerStatus status, private String email = ""
您的 save
方法是 public,因此可以从任何地方调用。如果有人保存了一个客户,然后又更改了他们保存的客户对象,您不希望新的更改反映在您在 contacts
中得到的内容中。在那里保存一个克隆确保你不会受到他们正在做的事情的影响。
But assuming save method isn't called from anywhere else other than ensureTestData()
, do we still need to clone it?
从技术上讲,在短视上没有。您的 ensureTestData
方法是保存它创建的客户还是它的克隆都没有区别。不过,在那种情况下,您需要将 save
方法声明为私有,否则您将不知道何时有人从某处调用它。在许多情况下,人们仍然会克隆以防止将来对代码的更改导致不必要的影响。
我正在阅读 Vaadin 教程并遇到了这段代码。但是我无法理解 clone
在这种情况下的目的。谁能解释一下?
/**
* Persists or updates customer in the system. Also assigns an identifier for
* new Customer instances.
*
* @param entry
*/
public synchronized void save(Customer entry) {
if (entry == null) {
LOGGER.log(Level.SEVERE,
"Customer is null. Are you sure you have connected your form to the application as described in tutorial chapter 7?");
return;
}
if (entry.getId() == null) {
entry.setId(nextId++);
}
try {
entry = (Customer) entry.clone();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
contacts.put(entry.getId(), entry);
}
/**
* Sample data generation
*/
public void ensureTestData() {
if (findAll().isEmpty()) {
final String[] names = new String[] { "Gabrielle Patel", "Brian Robinson", "Eduardo Haugen",
"Koen Johansen", "Alejandro Macdonald", "Angel Karlsson", "Yahir Gustavsson", "Haiden Svensson",
"Emily Stewart", "Corinne Davis", "Ryann Davis", "Yurem Jackson", "Kelly Gustavsson",
"Eileen Walker", "Katelyn Martin", "Israel Carlsson", "Quinn Hansson", "Makena Smith",
"Danielle Watson", "Leland Harris", "Gunner Karlsen", "Jamar Olsson", "Lara Martin",
"Ann Andersson", "Remington Andersson", "Rene Carlsson", "Elvis Olsen", "Solomon Olsen",
"Jaydan Jackson", "Bernard Nilsen" };
Random r = new Random(0);
for (String name : names) {
String[] split = name.split(" ");
Customer c = new Customer();
c.setFirstName(split[0]);
c.setLastName(split[1]);
c.setStatus(CustomerStatus.values()[r.nextInt(CustomerStatus.values().length)]);
c.setBirthDate(LocalDate.now().minusDays(r.nextInt(365 * 100)));
save(c);
}
}
}
注:Customer
有以下数据成员:private Long id, private String firstName = "", private String lastName = "", private LocalDate birthDate, private CustomerStatus status, private String email = ""
您的 save
方法是 public,因此可以从任何地方调用。如果有人保存了一个客户,然后又更改了他们保存的客户对象,您不希望新的更改反映在您在 contacts
中得到的内容中。在那里保存一个克隆确保你不会受到他们正在做的事情的影响。
But assuming save method isn't called from anywhere else other than
ensureTestData()
, do we still need to clone it?
从技术上讲,在短视上没有。您的 ensureTestData
方法是保存它创建的客户还是它的克隆都没有区别。不过,在那种情况下,您需要将 save
方法声明为私有,否则您将不知道何时有人从某处调用它。在许多情况下,人们仍然会克隆以防止将来对代码的更改导致不必要的影响。