简体   繁体   中英

java: ensure that there is only one instance of the type

I am following the example from:
https://www.baeldung.com/java-composite-pattern

public class FinancialDepartment implements Department {

    private Integer id;
    private String name;

    public void printDepartmentName() {
        System.out.println(getClass().getSimpleName());
    }

    // standard constructor, getters, setters
}
public class SalesDepartment implements Department {

    private Integer id;
    private String name;

    public void printDepartmentName() {
        System.out.println(getClass().getSimpleName());
    }

    // standard constructor, getters, setters
}

public class HeadDepartment implements Department {
    private Integer id;
    private String name;

    private List<Department> childDepartments;

    public HeadDepartment(Integer id, String name) {
        this.id = id;
        this.name = name;
        this.childDepartments = new ArrayList<>();
    }

    public void printDepartmentName() {
        childDepartments.forEach(Department::printDepartmentName);
    }

    public void addDepartment(Department department) {
        childDepartments.add(department);
    }

    public void removeDepartment(Department department) {
        childDepartments.remove(department);
    }
}

I want to prevent my self from able to add two of the same types to HeadDepartment

for example if it call add addDepartment twice for the same type, there should be only one

public class CompositeDemo {
    public static void main(String args[]) {
        Department salesDepartment = new SalesDepartment(
          1, "Sales department");

        Department salesDepartment2 = new SalesDepartment(
          1, "Sales department");
        Department salesDepartment3 = new SalesDepartment(
          3, "Sales department");


        Department financialDepartment = new FinancialDepartment(
          2, "Financial department");

        HeadDepartment headDepartment = new HeadDepartment(
          3, "Head department");

        headDepartment.addDepartment(salesDepartment);
        headDepartment.addDepartment(financialDepartment);

        // only keep the latest of same instanceof ie replace
        headDepartment.addDepartment(salesDepartment2);
        headDepartment.addDepartment(salesDepartment3);

        // this should only print twice one for salesDepartment3 and financialDepartment
        headDepartment.printDepartmentName();

    }
}

i suppose do i just iterate the list and if instanceof, replace and put?

public void addDepartment(Department department) {
        childDepartments.add(department);
    }

i would like to keep the order as well if the instnaceof Department was the first, i would like it to keep it as 1st, meaning it should print salesDepartment3 before financialDepartment

Your addDepartment() needs to iterate over the list of children and compare each one's class to the class of the object you are adding. Pseudo code:

Class addClass = itemToAdd.getClass();
for each child
{
    if (child.getClass() == addClass)
    {
        //class is already in the list so replace it.
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM