You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This code refactors the addMigratingInVm method in the Host class by applying the Strategy design pattern.
The primary motivation for using the Strategy pattern is to improve code modularity and maintainability.
Initially, the method contained multiple resource checks (storage, RAM, bandwidth, MIPS) using conditional statements,
making the code difficult to read and maintain. By encapsulating each resource check into separate strategy classes,
we achieve a cleaner, more extensible design. This allows us to easily modify or add new resource checks without changing
the main method's logic, thus adhering to the Single Responsibility Principle and enhancing the code's flexibility and cohesion.
publicinterfaceResourceCheckStrategy {
booleancheck(Vmvm, Hosthost);
publicclassStorageCheckStrategyimplementsResourceCheckStrategy {
@Overridepublicbooleancheck(Vmvm, Hosthost) {
if (host.getStorage() < vm.getSize()) {
Log.printConcatLine("[VmScheduler.addMigratingInVm] Allocation of VM #", vm.getId(), " to Host #",
host.getId(), " failed by storage");
returnfalse;
}
returntrue;
}
}
publicclassRamCheckStrategyimplementsResourceCheckStrategy {
@Overridepublicbooleancheck(Vmvm, Hosthost) {
if (!host.getRamProvisioner().allocateRamForVm(vm, vm.getCurrentRequestedRam())) {
Log.printConcatLine("[VmScheduler.addMigratingInVm] Allocation of VM #", vm.getId(), " to Host #",
host.getId(), " failed by RAM");
returnfalse;
}
returntrue;
}
}
publicclassBwCheckStrategyimplementsResourceCheckStrategy {
@Overridepublicbooleancheck(Vmvm, Hosthost) {
if (!host.getBwProvisioner().allocateBwForVm(vm, vm.getCurrentRequestedBw())) {
Log.printLine("[VmScheduler.addMigratingInVm] Allocation of VM #" + vm.getId() + " to Host #"
+ host.getId() + " failed by BW");
returnfalse;
}
returntrue;
}
}
publicclassMipsCheckStrategyimplementsResourceCheckStrategy {
@Overridepublicbooleancheck(Vmvm, Hosthost) {
if (!host.getVmScheduler().allocatePesForVm(vm, vm.getCurrentRequestedMips())) {
Log.printLine("[VmScheduler.addMigratingInVm] Allocation of VM #" + vm.getId() + " to Host #"
+ host.getId() + " failed by MIPS");
returnfalse;
}
returntrue;
}
}
publicclassHost {
privateList<ResourceCheckStrategy> strategies;
publicHost() {
strategies = newArrayList<>();
strategies.add(newStorageCheckStrategy());
strategies.add(newRamCheckStrategy());
strategies.add(newBwCheckStrategy());
strategies.add(newMipsCheckStrategy());
}
publicvoidaddMigratingInVm(Vmvm) {
vm.setInMigration(true);
if (!getVmsMigratingIn().contains(vm)) {
for (ResourceCheckStrategystrategy : strategies) {
if (!strategy.check(vm, this)) {
System.exit(0);
}
}
getVmScheduler().getVmsMigratingIn().add(vm.getUid());
setStorage(getStorage() - vm.getSize());
getVmsMigratingIn().add(vm);
getVmList().add(vm);
updateVmsProcessing(CloudSim.clock());
vm.getHost().updateVmsProcessing(CloudSim.clock());
}
}
// Other methods and attributes of the Host class
}
publicclassVm {
privatebooleaninMigration;
privatedoublesize;
privatedoublecurrentRequestedRam;
privatedoublecurrentRequestedBw;
privatedoublecurrentRequestedMips;
privateHosthost;
privateStringuid;
privateintid;
// Getters and setterspublicvoidsetInMigration(booleaninMigration) {
this.inMigration = inMigration;
}
publicbooleanisInMigration() {
returninMigration;
}
publicdoublegetSize() {
returnsize;
}
publicdoublegetCurrentRequestedRam() {
returncurrentRequestedRam;
}
publicdoublegetCurrentRequestedBw() {
returncurrentRequestedBw;
}
publicdoublegetCurrentRequestedMips() {
returncurrentRequestedMips;
}
publicHostgetHost() {
returnhost;
}
publicStringgetUid() {
returnuid;
}
publicintgetId() {
returnid;
}
}
}
The text was updated successfully, but these errors were encountered:
making the code difficult to read and maintain. By encapsulating each resource check into separate strategy classes,
we achieve a cleaner, more extensible design. This allows us to easily modify or add new resource checks without changing
the main method's logic, thus adhering to the Single Responsibility Principle and enhancing the code's flexibility and cohesion.
The text was updated successfully, but these errors were encountered: