gpt4 book ai didi

java - 方法实现

转载 作者:行者123 更新时间:2023-12-02 03:33:55 25 4
gpt4 key购买 nike

我对 java 很陌生,需要帮助以更复杂的方式设置该方法的实现 - 我已经写了这个,但被告知我需要使用 GoF 模式之一来美化它。关于我需要做什么的一些帮助将是最合适的

protected void setWeapon() {
if (this.getClass().getSimpleName().equals("FighterJet")) {
this.weapon = new GuidedMissileSystem();
System.out.println("FighterJet " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("AttackHelicopter")) {
this.weapon = new GrenadeLauncher();
System.out.println("AttackHelicopter " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("Tank")) {
this.weapon = new Cannon();
System.out.println("Tank " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("InfantryMobilityVehicle")) {
this.weapon = new MachineGun();
System.out.println("InfantryMobilityVehicle " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("Warship")) {
this.weapon = new RocketLauncher();
System.out.println("Warship " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("Submarine")) {
this.weapon = new TorpedoTube();
System.out.println("Submarine " + id + " equipped with " + weapon.getClass().getSimpleName());
}
}

最佳答案

@AndyTurner的答案可以用下图来说明:

Solution Diagram

虽然这个解决方案有效,但我不同意它遵循依赖反转原则,因为具体类负责创建 Weapon 对象。此外,我认为它违反了开闭原则。想象一下您的武器在战斗中崩溃了。您不能只是将其替换为另一个。

我想提出另一种解决方案,结构相似,但功能不同:

Solution 2 Diagram

interface WarVehicle {
boolean atack(WarVehicle other);
boolean defend(WarVehicle from);
void setWeapon(WarVehicle from);
Weapon getWeapon();
boolean acceptsWeapon();
}

abstract class AbstractWarVehicle {
private Weapon weapon;
public AbstractWarVehicle(Weapon weapon) {
setWeapon(weapon);
}

public final void setWeapon(Weapon weapon) {
if (!acceptsWeapon(weapon)) {
throw new IllegalArgumentException("Weapon of type "
+ weapon.getClass().getName()
+ " cannot be added to WarVehicle of type "
+ this.getClass().getName());
}
this.weapon = weapon;
}

public final Weapon getWeapon() {
return this.weapon;
}

public boolean attack(WarVehicle other) {
if (other != this) {
return !other.defend(this);
}
}

public boolean defend(WarVehicle from) {
return this.getWeapon().getDefensePower() >=
from.getWeapon().getFirePower();
}
}

class FighterJet extends AbstractWarVehicle {
public boolean acceptsWeapon(Weapon weapon) {
return weapon instanceof GuidedMissilesystem;
}
}

class AttackHelicopter extends AbstractWarVehicle {
public boolean acceptsWeapon(Weapon weapon) {
return weapon instanceof GrenadeLauncher;
}
}

// And so on...

使用它:

Weapon jet = new FighterJet(new GuidedMissleSystem()); // OK
Weapon jet2 = new FighterJet(new Cannon()); // throws!

这种方法的优点:

  • 这是一个 DIP 实现。
  • 可以使用 setWeaponWeapon 替换为类似的武器。
  • 它(在某种程度上)尊重 OCP,因为可以子类化/组合 WeaponFighterJet 将接受任何 GuidedMissileSystem 实例,这意味着如果您的游戏扩展并且您获得了 SuperUltraMegaBlasterGeorgeForemanMissileSystem,您仍然会如果它继承自 GuidedMissileSystem,则可以使用它。

这种方法的缺点:

  • acceptsWeapon 中的规则是静态的,因此无法在运行时更改。

关于java - 方法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37694231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com