/// <summary> /// 中介者模式和状态者模式组合运用 /// </summary> public void Mediator_State_CardPartner() { AStateCardPartner A = new StateParterA(); AStateCardPartner B = new StateParterB(); // 初始钱 A.MoneyCount = 20; B.MoneyCount = 20; AStateMediator mediator = new MediatorPater(new InitState()); // A,B玩家进入平台进行游戏 mediator.Enter(A); mediator.Enter(B); // A赢了 mediator.State = new AwinState(mediator); mediator.ChangeCount(5); Console.WriteLine("A 现在的钱是:{0}", A.MoneyCount); // 应该是25 Console.WriteLine("B 现在的钱是:{0}", B.MoneyCount); // 应该是15 // B 赢了 mediator.State = new BwinSate(mediator); mediator.ChangeCount(10); Console.WriteLine("A 现在的钱是:{0}", A.MoneyCount); // 应该是15 Console.WriteLine("B 现在的钱是:{0}", B.MoneyCount); // 应该是25 }
public override void ChangeCount(int count) { foreach (AStateCardPartner p in mediator.list) { StateParterB b = p as StateParterB; if (b != null) { b.MoneyCount += count; } else { p.MoneyCount -= count; } } }