用switch语句实现java中的类故障解耦

sshcrbum  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(341)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

27天前关门了。
改进这个问题
目前我正在做一个小类型的游戏,在我的player类中我有很多重要的和依赖的东西。
因此,我决定将switch语句分离到它自己的单独类中,以便在方法中调用。
播放器代码的上下文

import Accessories.BasicSight;
import Accessories.BasicSilencer;
import Accessories.BasicStock;
import Accessories.GoodSight;
import Accessories.GoodSilencer;
import Accessories.GoodStock;
import Accessories.GreatSight;
import Accessories.GreatSilencer;
import Accessories.GreatStock;
import PlayerState.AliveState;
import PlayerState.PlayerState;
import Guns.Weapon;
import Guns.*;

import java.util.Scanner;

public class PlayerSingleton {

    private static PlayerSingleton player;
    Scanner scanner = new Scanner(System.in);

    private String playerName;
    private int lives;
    private Integer health;
    public static Integer MAX_PLAYER_HEALTH = 500;
    public static Integer DEFAULT_PLAYER_LIVES = 2;

    private PlayerState playerState;

    private Weapon weapon;

    private PlayerSingleton(Weapon weapon, String pName) {
        this.weapon = weapon;
        this.playerName = pName;
        this.lives = DEFAULT_PLAYER_LIVES;
        setState(new AliveState(this));
    }

    public static PlayerSingleton getInstance(String choice, String name) {
        System.out.println("Choose Weapon to play the with: ");

        Weapon weapon = PlayerSingleton.chooseWeapon(choice);

        weapon.getDescription();

        if (player == null) {
            player = new PlayerSingleton(weapon, name);
        }
        return player;
    }

    public void sufferDamage(int damage) {
        playerState.takeDamage(damage);
    }

    public void respawn() {
        playerState.respawn();
    }

    public int getLives() {
        return lives;
    }

    public void setLives(int lives) {
        this.lives = DEFAULT_PLAYER_LIVES;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(Integer health) {
        this.health = health;
    }

    public void setState(PlayerState playerState) {
        this.playerState = playerState;
    }

    public void chosenWeapon() {
        System.out.println("Player Info: " + playerName + " " + "Has: " + health + " health and " + lives + " lives");
        System.out.println(weapon.getDescription() + ":" + " base damage: " + weapon.damage());

    }

    public static Weapon chooseWeapon(String choice) {
        switch (choice) {
            case "MP5":
                System.out.println("You have chosen MP5!");
                return new MP5Weapon();
            case "SNIPER":
                System.out.println("You have chosen Sniper!");
                return new SniperRifleWeapon();
            case "SHOTGUN":
                System.out.println("You have chosen Shotgun!");
                return new ShotgunWeapon();
            default:
                System.out.println("No gun by that name found!");
                return null;
        }

    }  
     public void addBasicAttachment(String attachment) {

        switch (attachment) {
            case "SIGHT":
                weapon = new BasicSight(weapon);
                break;
            case "SILENCER":
                weapon = new BasicSilencer(weapon);
                break;
            case "STOCK":
                weapon = new BasicStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
    }

    public void getBasicAttachment(String attachment) {

    }

    public void addGoodAttachment(String attachment) {
        switch (attachment) {
            case "SIGHT":
                weapon = new GoodSight(weapon);
                break;
            case "SILENCER":
                weapon = new GoodSilencer(weapon);
                break;
            case "STOCK":
                weapon = new GoodStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
    }

    public void addGreatAttachment(String attachment) {
        switch (attachment) {
            case "SIGHT":
                weapon = new GreatSight(weapon);
                break;
            case "SILENCER":
                weapon = new GreatSilencer(weapon);
                break;
            case "STOCK":
                weapon = new GreatStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
    }

}

所以上面的代码是我的player类,我想把addbasic/good/great-wealth附件放在另一个单独的类文件中,这样可以得到更好的解耦代码。我不知道如何用switch语句来实现这一点。
有什么办法吗?
编辑:
正在从main调用switch方法:

import Accessories.*;
import Player.PlayerSingleton;
import Guns.Weapon;
import Guns.MP5Weapon;
import java.util.Scanner;

public class FireingRange {
Weapon weapon;
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String name = scanner.nextLine();

        System.out.println(name + " to begin the game, first type a weapon you would like to play with:");
        System.out.println();
        System.out.println("Choose Weapon to play the with: ");
        System.out.println("'Shotgun' for Shotgun");
        System.out.println("'Sniper' for Sniper");
        System.out.println("'MP5' for MP5");
        String chooseWeapon = scanner.next().toUpperCase();

        PlayerSingleton player = PlayerSingleton.getInstance(chooseWeapon, name);

        System.out.println(player.getHealth());
        System.out.println(player.getLives());

        System.out.println();

        player.chosenWeapon();
        System.out.println("Are you ready to play the game?");

    }

抽象附属类:

import Guns.Weapon;

public abstract class AccessoryDecorator extends Weapon{
    public abstract String getDescription();
}

附件扩展附件装饰器:

public class BasicSight extends AccessoryDecorator {
    Weapon weapon;

    public BasicSight(Weapon weapon) {
        this.weapon = weapon;
    }
    @Override
    public String getDescription() {
        return weapon.getDescription() + ", Basic Sight";
    } 
    @Override
    public double weight() {
        return 7 + weapon.weight();
    }

    @Override
    public double damage() {
        return 7 + weapon.damage();
   }

因此,目标是:

public void addBasicAttachment(String attachment) {

        switch (attachment) {
            case "SIGHT":
                weapon = new BasicSight(weapon);
                break;
            case "SILENCER":
                weapon = new BasicSilencer(weapon);
                break;
            case "STOCK":
                weapon = new BasicStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
    }

    public void addGoodAttachment(String attachment) {
        switch (attachment) {
            case "SIGHT":
                weapon = new GoodSight(weapon);
                break;
            case "SILENCER":
                weapon = new GoodSilencer(weapon);
                break;
            case "STOCK":
                weapon = new GoodStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
    }
    public void addGreatAttachment(String attachment) {
        switch (attachment) {
            case "SIGHT":
                weapon = new GreatSight(weapon);
                break;
            case "SILENCER":
                weapon = new GreatSilencer(weapon);
                break;
            case "STOCK":
                weapon = new GreatStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
    }

在自己的一个类中,然后能够调用带有结果的开关,然后将其添加到所选枪的值中。
我似乎无法思考如何返回配件取决于什么配件是创建。

q35jwt9p

q35jwt9p1#

通常可以消除java等具有类继承的语言中的switch语句。尤其是java灵活的枚举。我碰巧有一个这样的例子:

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public enum CommandEnum
{
    AUTHENTICATE("authenticate") {
        public Command newCommand(final String newMessageName) {
            return new Authenticate(newMessageName);
        }
    },

    ADD("add") {
        public Command newCommand(final String newMessageName) {
            return new Add(newMessageName);
        }
    },
    // ...bunch deleted for brevity    

    public final String messageName;

    // Collect all enumerations into a map you
    // can look up by name.    
    private static final Map<String, CommandEnum> messageNameMap =
        Arrays.stream(CommandEnum.values())
            .collect(
                Collectors.toMap(
                    commandEnum -> commandEnum.messageName,
                    commandEnum -> commandEnum
                )
            );

    CommandEnum(final String newMessageName) {
        messageName = newMessageName;
    }

    public abstract Command newCommand(final String newMessageName);

    public Command newCommand() {
        return newCommand(messageName);
    }

    public static CommandEnum getEnum(String messageName) {
        return messageNameMap.get(messageName);
    }
}

在这种情况下,可以通过以下方式从字符串中获取枚举:

CommandEnum ce = CommandEnum.getEnum(...string...);

你把它转换成这样的命令:

if (null != ce) {
    Command c = ce.newCommand();
    ...
}
ivqmmu1c

ivqmmu1c2#

您可以创建一个具有某些功能的基类,然后通过扩展的方式添加代码。另一种方法是创建一个只包含静态函数的实用程序类,您可以调用这些函数来操作传递的对象,如下所示:

public class PlayerUtil {
    public static void addGreatAttachment(PlayerSingleton player, String attachment) {
        Weapon weapon;
        switch (attachment) {
            case "SIGHT":
                weapon = new GreatSight(weapon);
                break;
            case "SILENCER":
                weapon = new GreatSilencer(weapon);
                break;
            case "STOCK":
                weapon = new GreatStock(weapon);
                break;
            default:
                System.out.println("No Attachment found!");
        }
        player.setWeapon(weapon);
    }
}

但在你的情况下,我建议你创建另一个类:“装备武器”。这个类将有自己的武器场和可能的多个附件场。你可以把更改附件的代码放在里面,甚至可以添加一些方法来获取武器上附件的统计信息,比如伤害、子弹扩散或wtv,然后在playersingleton里面为武器创建一个getter。
我还建议您在游戏中创建一个attachmentmanager类,该类包含所有附件,并允许您使用各种方法选择它们。或者可以使所有附件扩展一个附件类,该类包含所有附件的静态列表。如果将名称作为字段添加到attachment类中,则可以创建一个静态方法,该方法迭代所有附件,找到一个具有正确名称的附件并返回它。或者使用静态hashmap<string,attachment>。

相关问题