Puedo pensar en dos posibilidades.
Una es (es una especie de truco), si no tiene muchos frijoles que deben estar vinculados como los de su ejemplo, puede inyectar johnWork en el frijol johnHome, y en johnHome.setPhone se podría actualizar la propiedad de teléfono johnWork, algo así como:
public class Contact {
private Contact myWorkContact;
private String phone;
public void setPhone(String phone) {
this.phone = phone;
if (this.myWorkContact != null) {
this.myWorkContact.setPhone(phone);
}
}
public void setWorkContact(Contact c) {
this.myWorkContact = c;
}
}
o podría tener InicioContactenos y WorkContact tanto extender una clase de contacto y hacer lo mismo con la inyección de eso.
Si tiene toneladas y toneladas de frijoles que necesitarán esto (como si su aplicación realmente trata con información de contacto), con AOP (necesitará AspectJ para el ejemplo dado) Creo que podría hacer algo como esto (Será un poco intensivo en memoria si obtienes una tonelada de objetos, pero puedes ver cómo funciona algo así):
Advertencia: esto se volvió complicado rápidamente, pero estoy bastante seguro de que funcionaría después de haber trabajado unos pocos problemas
public class Contact {
...
private String phone;
private String name;
private Integer id;
public Contact(Integer id, String name, String phone) {
this.phone = phone;
this.name = name;
this.id = id;
}
public void setPhone(String phone) {
this.phone = phone.
}
//Other getters, setters, etc
...
}
@Aspect
public class ContactPhoneSynchronizer {
//there is probably a more efficient way to keep track of contact objects
//but right now i can't think of one, because for things like a tree, we need to
//be able to identify objects with the same name (John Smith), but that
//have different unique ids, since we only want one of each Contact object
//in this cache.
private List<Contact> contacts = Collections.synchronizedList(new ArrayList<Contact>());
/**
This method will execute every time someone makes a new Contact object.
If it already exists, return it from the cache in this.contacts. Otherwise,
proceed with the object construction and put that object in the cache.
**/
@Around("call(public Contact.new(Integer,String,String)) && args(id,name,phone)")
public Object cacheNewContact(ProceedingJoinPoint joinPoint, Integer id, String name, String phone) {
Contact contact = null;
for (Contact c : contacts) {
if (id.equals(c.getId()) {
contact = c;
break;
}
}
if (contact == null) {
contact = (Contact) joinPoint.proceed();
this.contacts.add(contact);
}
return contact;
}
/**This should execute every time a setPhone() method is executed on
a contact object. The method looks for all Contacts of the same
name in the cache and then sets their phone number to the one being passed
into the original target class.
Because objects are passed by reference until you do a reassociation,
calling c.setPhone on the object in the cache should update the actual
instance of the object in memory, so whoever has that reference will
get the updated information.
**/
@After("execution(example.Contact.setPhone(String) && args(phone)")
public void syncContact(JoinPoint joinPoint, String phone) {
Contact contact = joinPoint.getTarget();
for (Contact c : this.contacts) {
if (c.getName().equals(contact.getName()) {
c.setPhone(phone);
}
}
}
}
una vez más, es probable que haya 100 wa Si pudieras optimizar esto, ya que estoy tipeándolo de la parte superior de mi cabeza; es decir, si quería ir a esta ruta en primer lugar. En teoría, debería funcionar, pero no lo he probado en absoluto.
De todos modos, Happy Springing!