Cloudslab / cloudsim

CloudSim: A Framework For Modeling And Simulation Of Cloud Computing Infrastructures And Services
http://www.cloudbus.org/cloudsim/
812 stars 491 forks source link

Implement Builder Pattern for Host Class #180

Open dquishpe opened 1 month ago

dquishpe commented 1 month ago

The Host class in our project contains multiple attributes and a complex constructor. Currently, the class initialization is cumbersome and lacks flexibility. Implementing the Builder pattern will improve code readability, maintainability, and allow more flexible object creation.

The Host class has multiple attributes, such as id, storage, ramProvisioner, bwProvisioner, vmScheduler, vmList, peList, failed, vmsMigratingIn, and datacenter. The current approach to instantiate this class involves setting all these attributes directly through a constructor or via multiple setter methods, which can be error-prone and hard to manage.

Proposed Solution: Implement the Builder pattern to simplify and streamline the creation of Host objects. The Builder pattern will allow us to create instances of Host in a more readable and manageable way by chaining method calls. The following changes are proposed:

Create an Interface Builder:

interface Builder {
    Host build();
}

Modify the Host Class to Use the Builder:

public class Host {
    private int id;
    private long storage;
    private RamProvisioner ramProvisioner;
    private BwProvisioner bwProvisioner;
    private VmScheduler vmScheduler;
    private List<Vm> vmList;
    private List<Pe> peList;
    private boolean failed;
    private List<Vm> vmsMigratingIn;
    private Datacenter datacenter;

    private Host(HostBuilder builder) {
        this.id = builder.id;
        this.storage = builder.storage;
        this.ramProvisioner = builder.ramProvisioner;
        this.bwProvisioner = builder.bwProvisioner;
        this.vmScheduler = builder.vmScheduler;
        this.vmList = builder.vmList;
        this.peList = builder.peList;
        this.failed = builder.failed;
        this.vmsMigratingIn = builder.vmsMigratingIn;
        this.datacenter = builder.datacenter;
    }

    public static class HostBuilder implements Builder {
        private int id;
        private long storage;
        private RamProvisioner ramProvisioner;
        private BwProvisioner bwProvisioner;
        private VmScheduler vmScheduler;
        private List<Vm> vmList;
        private List<Pe> peList;
        private boolean failed;
        private List<Vm> vmsMigratingIn;
        private Datacenter datacenter;

        public HostBuilder setId(int id) {
            this.id = id;
            return this;
        }

        public HostBuilder setStorage(long storage) {
            this.storage = storage;
            return this;
        }

        public HostBuilder setRamProvisioner(RamProvisioner ramProvisioner) {
            this.ramProvisioner = ramProvisioner;
            return this;
        }

        public HostBuilder setBwProvisioner(BwProvisioner bwProvisioner) {
            this.bwProvisioner = bwProvisioner;
            return this;
        }

        public HostBuilder setVmScheduler(VmScheduler vmScheduler) {
            this.vmScheduler = vmScheduler;
            return this;
        }

        public HostBuilder setVmList(List<Vm> vmList) {
            this.vmList = vmList;
            return this;
        }

        public HostBuilder setPeList(List<Pe> peList) {
            this.peList = peList;
            return this;
        }

        public HostBuilder setFailed(boolean failed) {
            this.failed = failed;
            return this;
        }

        public HostBuilder setVmsMigratingIn(List<Vm> vmsMigratingIn) {
            this.vmsMigratingIn = vmsMigratingIn;
            return this;
        }

        public HostBuilder setDatacenter(Datacenter datacenter) {
            this.datacenter = datacenter;
            return this;
        }

        @Override
        public Host build() {
            return new Host(this);
        }
    }
}