多路分发_demo_me

参考ThinkJava中的例子
整理如下

OutCome

package com.enumDistribute;

public enum OutCome
{
    WIN, LOSE, DRAW
}

Competitor

package com.enumDistribute;

public interface Competitor<T extends Competitor<T>>
{
    OutCome compete(T competitor);
}

RoShamBo

package com.enumDistribute;

import java.util.Random;

public class RoShamBo
{
    public static <T extends Competitor<T>> void match(T a, T b)
    {
        System.out.println(a + " vs " + b + " : " + a.compete(b));
    }

    public static <T extends Enum<T> & Competitor<T>> void play(Class<T> rsbClass, int size)
    {
        for (int i = 0; i < size; i++)
        {
            match(random(rsbClass), random(rsbClass));
        }
    }

    public static <T extends Enum<T>> T random(Class<T> cls)
    {
        T[] array = cls.getEnumConstants();
        // System.out.println("length : "+array.length);
        Random random = new Random();
        T t = array[random.nextInt(array.length)];

        System.out.println("t : " + t);
        return t;
    }
}

RoShamBo2

package com.enumDistribute;

import static com.enumDistribute.OutCome.DRAW;
import static com.enumDistribute.OutCome.LOSE;
import static com.enumDistribute.OutCome.WIN;

public enum RoShamBo2 implements Competitor<RoShamBo2>
{
    /**
     * DRAW, LOSE, WIN参考的是OutCome paper, OutCome sham, OutCome rock
     */
    PAPER(DRAW, LOSE, WIN),

    SHAM(WIN, DRAW, LOSE),

    ROCK(LOSE, WIN, DRAW);

    OutCome _paper, _sham, _rock;

    RoShamBo2(OutCome paper, OutCome sham, OutCome rock)
    {
        this._paper = paper;
        this._sham = sham;
        this._rock = rock;
    }

    @Override
    public OutCome compete(RoShamBo2 it)
    {
        switch (it)
        {
        /*
         * default 放在上面/枚举中所有情况都可以被列举到。
         * 
         * PAPER、SHAM、ROCK是it的
         * _paper、_sham、_rock是this的
         * 
         */
            default:
            case PAPER:return _paper;
            case SHAM: return _sham;
            case ROCK:return _rock;
        }
    }

    public static void main(String[] args)
    {
        RoShamBo.play(RoShamBo2.class, 3);
    }
}

RoShamBo3

package com.enumDistribute;

public enum RoShamBo3 implements Competitor<RoShamBo3>
{
    PAPER
    {
        @Override
        public OutCome compete(RoShamBo3 it)
        {
            switch (it)
            {
                default :
                case PAPER:return OutCome.DRAW;
                case SCISSORS :return OutCome.LOSE;
                case ROCK :return OutCome.WIN;
            }
        }
    },
    SCISSORS
    {
        @Override
        public OutCome compete(RoShamBo3 it)
        {
            switch (it)
            {
                default :
                case PAPER:return OutCome.WIN;
                case SCISSORS :return OutCome.DRAW;
                case ROCK :return OutCome.LOSE;
            }
        }
    },
    ROCK
    {
        @Override
        public OutCome compete(RoShamBo3 it)
        {
            switch (it)
            {
                default :
                case PAPER:return OutCome.LOSE;
                case SCISSORS :return OutCome.WIN;
                case ROCK :return OutCome.DRAW;
            }
        }
    };

    public abstract OutCome compete(RoShamBo3 it);

    public static void main(String[] args)
    {
        RoShamBo.play(RoShamBo3.class, 10);
    }
}

RoShamBo5

package com.enumDistribute;

import java.util.EnumMap;

public enum RoShamBo5 implements Competitor<RoShamBo5>
{
    PAPER, SCISSORS, ROCK;

    static EnumMap<RoShamBo5, EnumMap<RoShamBo5, OutCome>> table = new EnumMap<RoShamBo5, EnumMap<RoShamBo5, OutCome>>(RoShamBo5.class);

    static
    {
        for (RoShamBo5 it : RoShamBo5.values())
        {
            table.put(it, new EnumMap<RoShamBo5, OutCome>(RoShamBo5.class));
        }

        /**
         * DRAW, LOSE, WIN参考的是OutCome _paper, OutCome _scissors, OutCome _rock
         */
        initRow(PAPER, OutCome.DRAW, OutCome.LOSE, OutCome.WIN);
        initRow(SCISSORS, OutCome.WIN, OutCome.DRAW, OutCome.LOSE);
        initRow(ROCK, OutCome.LOSE, OutCome.WIN, OutCome.DRAW);
    }

    static void initRow(RoShamBo5 it, OutCome _paper, OutCome _scissors, OutCome _rock)
    {
        EnumMap<RoShamBo5, OutCome> row = RoShamBo5.table.get(it);

        row.put(RoShamBo5.PAPER, _paper);
        row.put(RoShamBo5.SCISSORS, _scissors);
        row.put(RoShamBo5.ROCK, _rock);
    }

    @Override
    public OutCome compete(RoShamBo5 rb5)
    {
        //this vs it
        return table.get(this).get(rb5);
    }

    public static void main(String[] args)
    {
        RoShamBo.play(RoShamBo5.class, 10);
    }
}

RoShamBo6

package com.enumDistribute;

public enum RoShamBo6 implements Competitor<RoShamBo6>
{
    PAPER, SCISSORS, ROCK;

    private static OutCome[][] table = { 

        {OutCome.DRAW, OutCome.LOSE, OutCome.WIN},// PAPER

        {OutCome.WIN, OutCome.DRAW, OutCome.LOSE},// SCISSORS

        {OutCome.LOSE, OutCome.WIN, OutCome.DRAW}// ROCK

        };

    public static void main(String[] args)
    {
        RoShamBo.play(RoShamBo6.class, 10);
    }

    @Override
    public OutCome compete(RoShamBo6 other)
    {
        return table[this.ordinal()][other.ordinal()];
    }
}

小结:确定情况的多路判断可以使用enum来改写。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值