vendredi 5 juin 2020

What is ur suggestion for this Java class designe

I have some concussion to make proper design for the following interface and class. The requirement is the following:-

public interface Entity {
    // Returns entity name
    String getName();
    // Returns a unique identifier
    String getID();
    // Returns the sub-entities of this entity
    Set<Entity> getSubEntities();
    // Returns a set of key-value data belonging to this entity
    Map<String,String> getData();
}

public class SubEntity implements Entity {

private String ID;
private String name;
private int price;
private Set<Entity> sbuEntities;

public SubEntity(){
    ID = UUID.randomUUID().toString();
}

public SubEntity(String name, int price) {
    this.name = name;
    this.price = price;
    this.ID = UUID.randomUUID().toString();
}

public void setId(String id) {
    this.ID = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

public String getID() {
    return ID;
}

public void addSubEntity(Entity entity){
    if(sbuEntities == null){
        sbuEntities = new HashSet<>();
    }
    sbuEntities.add(entity);
};

public Set<Entity> getSubEntities(){
    return sbuEntities;
}

public Map<String, String> getData() {
    if(sbuEntities == null){
        sbuEntities = new HashSet<>();
    }
    return sbuEntities.stream()
            .collect(Collectors.toMap(Entity::getID,
                    Object::toString));
}

public String toString() {
    return "SubEntity{" +
            "id='" + ID + '\'' +
            ", name='" + name + '\'' +
            ", price=" + price +
            '}';
}
}

There could be many types of SubEnitity that implement this interface. Also, every SubEnitity should have a Set as Set subentities or Set sbuEntities. I have confusion about the Set. Should I keep inside the SubEntity or outside. I would like to know your opinion with possible pros an cons.

Aucun commentaire:

Enregistrer un commentaire