Created
February 5, 2015 13:36
-
-
Save ufcpp/a8393f5d2118c9df25e5 to your computer and use it in GitHub Desktop.
type pattern matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// バトル中のアクション。 | |
| /// </summary> | |
| public abstract class BattleAction | |
| { | |
| public abstract void ApplyTo(Unit u); | |
| } | |
| /// <summary> | |
| /// 攻撃アクション。 | |
| /// </summary> | |
| public class Attack : BattleAction | |
| { | |
| public int Power { get; set; } | |
| public override void ApplyTo(Unit u) => u.Life -= Power; | |
| } | |
| /// <summary> | |
| /// 回復アクション。 | |
| /// </summary> | |
| public class Heal : BattleAction | |
| { | |
| public int Power { get; set; } | |
| public override void ApplyTo(Unit u) => u.Life += Power; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Threading.Tasks; | |
| /// <summary> | |
| /// デモ用 モデルもどき。 | |
| /// </summary> | |
| public class SampleModel | |
| { | |
| /// <summary> | |
| /// コマンド実行。 | |
| /// </summary> | |
| public static async Task ExecuteCommandAsync() | |
| { | |
| var selectedAction = await SelectCommandAsync(); | |
| var target = await SelectTargetAsync(); | |
| // モデル側は普通に仮想メソッドとか使って処理を掛けるのが楽 | |
| selectedAction.ApplyTo(target); | |
| // ほんとは ApplyTo でアクション結果を返してもらって、ビューに通知 | |
| } | |
| /// <summary> | |
| /// コマンド選択。 | |
| /// </summary> | |
| private static Task<BattleAction> SelectCommandAsync() | |
| { | |
| // ほんとはビューにメッセージを投げて、 | |
| // コマンド入力ウィンドウでも開いて、ユーザー入力を待って、アクションを選択する | |
| return Task.FromResult<BattleAction>(new Attack()); | |
| } | |
| /// <summary> | |
| /// ターゲット選択。 | |
| /// </summary> | |
| private static Task<Unit> SelectTargetAsync() | |
| { | |
| // ほんとはビューにメッセージを投げて、 | |
| // ターゲット選択ウィンドウでも開いて、ユーザー入力を待って、ターゲットを選択する | |
| return Task.FromResult(new Unit()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// ユニット。 | |
| /// </summary> | |
| public class Unit | |
| { | |
| public int Life { get; set; } | |
| // ほんとは他に攻撃力、魔力、防御力、… などなど。 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// デモ用 ビューもどき。 | |
| /// </summary> | |
| public class SampleView | |
| { | |
| public static void ShowBattleAction(BattleAction action) | |
| { | |
| // モデル側はビューの事情を知ったことではないので、処理はこっち側で分岐することに。 | |
| // 型を見て分岐。 | |
| var heal = action as Heal; | |
| if (heal != null) ShowHeal(heal); | |
| var attack = action as Attack; | |
| if (attack != null) ShowAttack(attack); | |
| } | |
| private static void ShowHeal(Heal heal) | |
| { | |
| // 回復アクションの結果表示 | |
| } | |
| private static void ShowAttack(Attack attack) | |
| { | |
| // 攻撃アクションの結果表示 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment