Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Builder Pattern for Host Class #180

Open
dquishpe opened this issue Jul 30, 2024 · 0 comments
Open

Implement Builder Pattern for Host Class #180

dquishpe opened this issue Jul 30, 2024 · 0 comments

Comments

@dquishpe
Copy link

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);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant