如何在初始化之前更改单例模式

z4iuyo4d  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(389)
public class PlayerSingleton {

private static PlayerSingleton player;   
private String playerName;

private Weapon weapon;

Weapon stockMP5 = new MP5Weapon();        //Making a new weapon called stockMP5 from MP5weapon Class
Weapon sniper = new SniperRifleWeapon();  //Making a new weapon called sniper from SniperRifleWeapon Class
Weapon shotgun = new ShotgunWeapon();  //Making a new weapon called shotgun from Shotgun Class

private PlayerSingleton(Weapon weapon, String pN) {
    this.weapon = weapon;
    playerName = pN;
}

public void chooseWeapon(String choice) {
    switch (choice) {
        case "MP5":
            weapon = new MP5Weapon();
            break;
        case "Sniper":
            weapon = new SniperRifleWeapon();
            break;
        case "Shotgun":
            weapon = new ShotgunWeapon();
            break;
        default:
            System.out.println("No Attachment found!");
    }
}

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

//在这里,我希望允许玩家选择武器之前,单例类初始化如下。让玩家玩他们的枪,而不是在整个游戏中改变它。

if (player == null) {
        player = new PlayerSingleton(choice, n);
    }
    return player;
}

public void chosenWeapon() {
    System.out.println(weapon.getDescription()
            + " Weight of Player: " + playerName + " gun: " + weapon.weight() + " base damage: " + 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!");
    }
}

在初始化playersingleton getinstance方法之前,如何允许玩家选择武器?
任何帮助都将不胜感激。
编辑时间:

public static Weapon chooseWeapon(String choice) {
        switch (choice) {
            case "MP5":
                return new MP5Weapon();
            case "Sniper":
                return new SniperRifleWeapon();
            case "Shotgun":
                return new ShotgunWeapon();
            default:
                return null;
        }
    }
public static PlayerSingleton getInstance(String choice, String n) {
        System.out.println("Choose Weapon to play the with: ");

        Weapon weapon = PlayerSingleton.chooseWeapon(choice);
        if (player == null) {
            player = new PlayerSingleton(weapon, n);
        }
        return player;
    }

主要内容:

Scanner scanner = new Scanner(System.in);
        System.out.println(" ------------------------------------ ");
        System.out.println("       Text based Shooting Game       ");
        System.out.println(" ------------------------------------ ");
        System.out.println("Please Enter your name before Beginning*");
        String name = scanner.next();
        System.out.println("Thank you for joining this Adventure " +name);

        Weapon weapon = PlayerSingleton.chooseWeapon(name);   
        PlayerSingleton player = PlayerSingleton.getInstance("MP5",name);
        player.chosenWeapon();

附加编辑:武器类

public abstract class Weapon {
    String description = "Unknown Weapon: ";

    public String getDescription(){
        return description;
    }
    //public abstract int bulletCount();
    public abstract double weight();
    public abstract double damage();
}
lsmepo6l

lsmepo6l1#

一个更干净的方法是 chooseWeapon 方法static,并返回武器:

public static Weapon chooseWeapon(String choice) {
    switch (choice) {
        case "MP5":
            return new MP5Weapon();
        case "Sniper":
            return new SniperRifleWeapon();
        case "Shotgun":
            return new ShotgunWeapon();
        default:
            System.out.println("No Attachment found!");
            return null;
    }
}

接着 getInstance 执行:

public static PlayerSingleton getInstance(String choice, String n) {
    Weapon weapon = PlayerSingleton.chooseWeapon(choice);        
    if (player == null) {
        player = new PlayerSingleton(weapon, n);
    }
    return player;
}

对getinstance的调用:

System.out.println("Choose Weapon to play the with: ");
 String choice = scanner.next();
 PlayerSingleton player = PlayerSingleton.getInstance(choice, name);
 player.chosenWeapon();
eaf3rand

eaf3rand2#

所以一个问题是为什么你想让你的球员成为单身汉?玩家对象应该用游戏对象初始化,这样你就不需要说“新”玩家了。
现在,如果您正在重新生成player并创建一个新示例,那么您应该考虑在player类中添加一个“reset”方法
如果你必须做“单件”:就选择武器的程序而言,将它委托给另一个类“武器工厂”会更干净。静态方法可能是一种短期的方法,如果你有自动测试,那么测试和存根是一件痛苦的事情(猜这些通常不是在编写游戏时编写的?):p
现在,当你有一个武器工厂,你可以有一个武器选择器类,提示用户,并调用所需的工厂(或方法)创建一个“武器”之前,试图获得一个球员的示例。
实际上:如果你不在player类中编写一个“setwearm”方法,你就已经达到了不允许玩家的武器被更改的目的,除非玩家对象被创建并与游戏再次关联。

相关问题