跳到主要内容

敌人生成与工厂模式

示例

public interface IEnemy
{
void Attack();
}

public class Zombie : IEnemy
{
public void Attack()
{
Debug.Log("Zombie attacks!");
}
}

public class Alien : IEnemy
{
public void Attack()
{
Debug.Log("Alien attacks!");
}
}

public class EnemyFactory
{
public IEnemy CreateEnemy(string enemyType)
{
switch (enemyType)
{
case "Zombie":
return new Zombie();
case "Alien":
return new Alien();
default:
throw new ArgumentException("Invalid enemy type");
}
}
}