0
Strategy Design Pattern ?
check it now š¤ https://code.sololearn.com/cG5V71KWWRFo/?ref=app
3 Answers
0
or
public interface Fireable {
void fire();
}
public class Gun {
Fireable ammo = new NoAmmo();
public void performUpgrade( Fireable ammoType) {
ammo = ammoType;
}
public void fire() { ammo.fire(); }
}
class NoAmmo implements Fireable {
public void fire() {
System.out.println("click");
}
}
public class Rockets implements Fireable {
@Override
public void fire() {
System.out.println("Rocket fired");
}
}
...
// input 500
myGun.fire(); //click
myGun.performUpgrade(upgrades);
myGun.fire(); //Rocket fired
0
public class BaseGun extends Gun {
Upgradeable upgradeable;
@Override
public void performUpgrade(Upgradeable upgradeable) {
this.upgradeable = upgradeable;
upgradeable.upgradeNow(); // added
}
}
...
if (score > 0 && score < 500) {
upgrades = new Bullets();
//upgrades.upgradeNow();
} else
if (score >= 500 && score < 1000) {
upgrades = new Rockets();
//upgrades.upgradeNow();
} else
if (score >= 1000) {
upgrades = new Nuclear();
//upgrades.upgradeNow();
}
ps don't forget, in case score=500 or score=1000
you need >= operator
if (score >= 500
instead of
if (score > 500
0
zemiak
rate it šš