Copied from Maniero’s response, only in pseudo-Java, for better visualization (and take Boilerplate. Whoever has something to improve, just edit it).
Very simply:
public class Pessoa {
private String nome;
}
public interface Papel {
void umMetodo();
}
public class Cliente implements Papel {
private Pessoa pessoa;
private BigDecimal credito;
}
public class Faturamento {
public void venda(Cliente cliente) {
System.out.println(cliente.getPessoa().getNome());
}
}
If you wish people to be aware of the roles they play, you could do it in two ways:
public class Pessoa {
private String nome;
private Cliente cliente;
private Fornecedor fornecedor;
}
Each role you add needs to change the class, which violates some principles, but which is not always a problem.
Another way:
public enum TipoPapel { Cliente, Fornecedor }
public class Pessoa {
private String Nome;
private Map<TipoPapel, Papel> papeis = new HashMap<>();
public void adicionaPapel(TipoPapel tipo, Papel papel) {
papeis.put(tipo, papel);
}
}
Instead of Enum it could use String that can facilitate or hinder, depending on the case. You could use another structure, even more specialized, in place of the map. You could have control of the papers in a separate type that abstracts the map.
I know modeling forms, UML ñ sei ñ.
– Maniero
@All right, no UML then :)
– Piovezan