为什么NeverSwitch类会覆盖我的AlwaysSwitch类?

时间:2019-02-18 00:54:16

标签: java template-method-pattern

我正在使用模板方法。我有一个提供给我的驱动程序,可以创建游戏并通过两个切换类(AlwaysSwitch或NeverSwitch)之一进行传递。然后,驱动程序将为Always Switch创建一个试用版,并为NeverSwitch创建一个单独的试用版。每次审判进行100次,并计算每个案例赢得游戏的次数。

这两个类都扩展了Game。游戏有一个称为switch()的抽象方法。两个交换类都继承此方法。我在两个类中都放了一个简单的print语句。 AlwaysSwitch带有“我想切换”,而NeverSwitch带有“我会留下”。

我的问题甚至是在创建new Game g = new AlwaysSwitch();的Always switch试用版中,输出始终是“我会留下来”。并且在new Game g = new NeverSwitch();中,输出再次是“我会留下来”。

我已尝试在驱动程序中注释掉NeverSwitch试用版,然后,AlwaysSwitch试用版才能正常工作。

我不明白为什么neverSwitch类会覆盖alwaysSwitch类?

public class Host {
    private Door prizeDoor;

    /**
     * Choose a random prize door and keep it secret.
     */
    public void choosePrizeDoor() {
        prizeDoor = Door.randomDoor();
    }

    /**
     * Reveal a door that does not contain the prize and does not
     * match the one the contestant chose.
     */
    public Door revealADoor(Door contestantChoice) {
        Door result = contestantChoice;
        // Randomly pick a door. Might iterate few times.
        while (result == contestantChoice || result == prizeDoor) {
            result = Door.randomDoor();
        }
        return result;
    }

    /**
     * Determine if the contestant's door wins or loses.
     */
    public boolean isAWinner(Door contestantChoice) {
        return prizeDoor == contestantChoice;
    }
}

/**
 * An enum representing a door. Left = 1, center = 2, right = 3.
 * Can also choose random doors here.
 * 
 * @author Todd Whittaker
 * @version 20180110
 */
public enum Door {
    LEFT(1),
    CENTER(2),
    RIGHT(3);

    private int value;
    private static final Random r = new Random();

    Door(int value) {
        this.value = value;
    }

    /**
     * Find the door that matches the number.
     */
    public static Door valueOf(int num) {
        switch (num) {
            case 1: return LEFT;
            case 2: return CENTER;
            default: return RIGHT;
        }
    }

    /**
     * return the number matching this door.
     */
    public int valueOf() {
        return value;
    }

    /**
     * Pick a random door (LEFT, CENTER, RIGHT).
     */
    public static Door randomDoor() {
        return Door.valueOf(r.nextInt(3)+1);
    }
}

public abstract class Game {

    private Host host;
    public Door contestantChoice;
    public Door revealedDoor;

    /**
     * Creates the game and its host.
     */
    public Game () {
        this.host = new Host();
    }

    /**
     * Implements the algorithm for the game:
     * 1. The host chooses which door has a prize at random.
     * 2. The contestant picks a door (left, center, or right) at random.
     * 3. The host reveals one of the two other doors that does not contain 
        the prize.
     * 4. The contestant can then switch to the other door or keep their 
        current door.
     * 5. The prize is revealed and the contestant wins or loses.
     */
    final boolean runGame() {
        host.choosePrizeDoor(); //1
        System.out.println("Let's Play");

        contestantChoice = contestantChoice.randomDoor();//2
        System.out.println("Contestant chooses " + contestantChoice);

        revealedDoor = host.revealADoor(contestantChoice); //3
        System.out.println("reveal door " + revealedDoor);

        System.out.println("would you like to switch?");
        switching();

        if(host.isAWinner(contestantChoice) == true)
        {
            System.out.println("Winner!! \n");
            return true;
        }

        System.out.println("Sorry you lose \n");

        return false;
    }

    abstract void switching();
}

public class AlwaysSwitch extends Game {
    void switching()
    {        
        System.out.println("I would like to switch" );
    }
}

public class NeverSwitch extends Game {
     void switching()
    {        
        System.out.println("I will stay." );
    }
}

public class Driver {
    private static final int TRIALS = 100;

    public static void main(String [] args) {
        int switchWins = 0;
        int stayWins = 0;
        for (int i = 0; i < TRIALS; ++i) {
           Game g = new AlwaysSwitch();
           if (g.runGame()) {
               ++switchWins;
           }
        }

        for (int i = 0; i < TRIALS; ++i) {
            Game g = new NeverSwitch();
            if (g.runGame()) {
                ++stayWins;
            }
        }

        System.out.println("Out of " + TRIALS + " trials:");
        System.out.println(" Switch won " + switchWins + " times.");
        System.out.println(" Stay won " + stayWins + " times.");
    }
}

所有结果都说“我会留下。”

1 个答案:

答案 0 :(得分:0)

要完成代码,您可以将switching()的签名更改为:

abstract Door switching(Door contestantsChoice, Door revealedDoor);

然后您将在Game.runGame()中这样称呼它:

contestantsChoice = switching(contestantsChoice, revealedDoor);

您的AlwaysSwitch实现将是:

Door switching(Door contestantChoice, Door revealedDoor)
{
    System.out.println("I would like to switch" );
    return selectRemainingDoor(contestantChoice, revealedDoor);
}

private Door selectRemainingDoor(Door contestantChoice, Door revealedDoor) {
    List<Door> doors = new ArrayList<>(asList(LEFT, CENTER, RIGHT));
    doors.remove(contestantChoice);
    doors.remove(revealedDoor);
    return doors.get(0);
}

您的NeverSwitch实现方式如下:

Door switching(Door contestantChoice, Door revealedDoor)
{
    System.out.println("I will stay." );
    return contestantChoice;
}