gpt4 book ai didi

java - 这是在 Java 中实现装饰器或多继承的新方法吗?

转载 作者:行者123 更新时间:2023-11-30 07:58:24 26 4
gpt4 key购买 nike

我想知道是否找到了在 Java 中实现装饰器设计模式的替代方法?这是否可能是一种类似于 Java 中多重继承的新方法?有没有更好的方法来实现下面的问题?

我针对以下问题提供了一个最小的示例(想象一个简单的游戏引擎):

  • Actor:是一个基本的 Actor,它有一个位置(在屏幕上)和一个可见性标志
  • 某些 Actor 是可损坏的,这意味着它们可以通过生命值、施加伤害和在必要时摧毁它们(使基础 Actor 隐形)的方法来扩展。
  • 某些 Actor 是物理,这意味着它们可以施加重力等力,从而有效地改 rebase 础 Actor 的位置。
  • 某些 Actor 是可损坏且物理的,因此它们应该执行某种多重继承或应用 2 个装饰器(使用装饰器模式)。 EnemyActor 就是一个例子,它当然还有自己的方法。

这是我的实现(最小化):

public class Actor {
int posX;
int posY;
boolean visible = true;
}

public class DamagableActorDecoration {
float health = 10;
private final Actor base;

public DamagableActorDecoration(Actor base) {
this.base = base;
}

void applyDamage (float dmg) {
health -= dmg;
if (health<=0)
base.visible = false;
}

boolean isDestroyed () {
return health<=0;
}
}

public class PhysicalActorDecoration {
private final Actor base;

public PhysicalActorDecoration(Actor base) {
this.base = base;
}

public void applyGravity () {
base.posY--;
}
}

public interface Damagable {
DamagableActorDecoration d();
}

public interface Physical {
PhysicalActorDecoration p();
}

public class EnemyActor extends Actor implements Damagable, Physical{
@Override
public DamagableActorDecoration d() {
return new DamagableActorDecoration(this);
}
@Override
public PhysicalActorDecoration p() {
return new PhysicalActorDecoration(this);
}

public void showExplodingParticleSystem (){}
}

现在我可以创建一个扩展 Actor 的敌人,它有自己的方法,而且也是可损坏的和物理的。例如,像这样使用它:

public class Runner {
public static void main(String[] args) {
EnemyActor enemy1 = new EnemyActor();

// basic actor behavior : appear
enemy1.visible=true;

// basic actor behavior : move forward
enemy1.posX++;

// damagable actor behavior : receive damage
enemy1.d().applyDamage(0.7f);

// physical actor behavior : fall down
enemy1.p().applyGravity();

// damagable actor behavior : be destroyed
if (enemy1.d().isDestroyed())
{
// enemy actor behavior : explode into pieces
enemy1.showExplodingParticleSystem();
}
}
}

最佳答案

你所做的事情介于委托(delegate)和组合之间。在我看来,“Damageable”和“Physical”应该独立运行,并将“EnemyActor”委托(delegate)委托(delegate)给它们,而不是将接口(interface)委托(delegate)委托(delegate)给装饰器。我通过删除不必要的间接级别对其进行了一些清理。

编辑:我忘了提及。当您玩视频游戏时,有时性能是最重要的。如果我们谈论的是由用户事件触发的操作,那么性能并不重要,但是如果我们谈论的是在循环中多次发生的操作 - 最好放弃并使用 Actor 来增强直接可访问的属性。

public class EnemyActor extends Actor implements Damageable, Physical {

private final DamageableActorDecoration damageDecorator;
private final PhysicalActorDecoration physicalDecorator;

public EnemyActor() {
damageDecorator = new DamageableActorDecoration(this);
physicalDecorator = new PhysicalActorDecoration(this);
}

@Override
public void applyDamage(float dmg) {
damageDecorator.applyDamage(dmg);
}

@Override
public boolean isDestroyed() {
return damageDecorator.isDestroyed();
}

@Override
public void applyGravity() {
physicalDecorator.applyGravity();
}
}

public class Actor {
int posX;
int posY;
boolean visible = true;
}

public interface Damageable {
void applyDamage(float dmg);

boolean isDestroyed();
}

public class DamageableActorDecoration implements Damageable {
float health = 10;
private final Actor base;

public DamageableActorDecoration(Actor base) {
this.base = base;
}

@Override
void applyDamage(float dmg) {
health -= dmg;
if (health <= 0)
base.visible = false;
}

@Override
boolean isDestroyed() {
return health <= 0;
}
}

public interface Physical {
void applyGravity();
}

public class PhysicalActorDecoration implements Physical {
private final Actor base;

public PhysicalActorDecoration(Actor base) {
this.base = base;
}

@Override
public void applyGravity() {
base.posY--;
}
}

关于java - 这是在 Java 中实现装饰器或多继承的新方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32307580/

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