程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 無廢話C#設計模式之二十:Mediator(2)

無廢話C#設計模式之二十:Mediator(2)

編輯:關於C語言

現在,再來看看引入AccountSystem後的通訊:

l 網站調用AccountSystem的注冊方法(1)

l AccountSystem調用A、B和C大區的注冊方法(2)

l A、B和C大區的充值方法調用AccountSystem的充值方法(3)

l A、B和C大區的消費方法調用AccountSystem的充值方法(4)

l AccountSystem的充值方法調用A、B和C大區的專有充值方法(只針對本大區的充值)(5)

l AccountSystem的充值方法調用A、B和C大區的專有消費方法(只針對本大區的消費)(6)

至此,你已經實現了中介者模式。你可能會覺得,(1)和(2)非常類似門面模式,沒錯,它確實就是門面模式,而有了(3)~(6)的行為,AccountSystem也就是一個中介者的角色了。

示例代碼

using System;

using System.Collections.Generic;

using System.Text;

namespace MediatorExample

{

    class Program

    {

        static void Main(string[] args)

        {

            AccountSystem accountSystem = new AccountSystem();

            GameSystem gameArea1 = new GameArea1(accountSystem);

            GameSystem gameArea2 = new GameArea2(accountSystem);

            accountSystem.RegisterGameArea(gameArea1);

            accountSystem.RegisterGameArea(gameArea2);

            string userName = "aaa";

            accountSystem.CreateAccount(userName);

            gameArea1.Recharge(userName, 200);

            gameArea2.Consume(userName, 50);

            accountSystem.QueryBalance(userName);

        }

}

    class AccountSystem

    {

        private Dictionary<string, int> userBalance = new Dictionary<string, int>();

        private List<GameSystem> gameAreaList = new List<GameSystem>();

        public void RegisterGameArea(GameSystem gs)

        {

            gameAreaList.Add(gs);

        }

        public void CreateAccount(string userName)

        {

            userBalance.Add(userName, 0);

            foreach (GameSystem gs in gameAreaList)

                gs.CreateAccountSelf(userName);

        }

        public void Recharge(string userName, int amount)

        {

            if (userBalance.ContainsKey(userName))

            {

                bool ok = true;

                foreach (GameSystem gs in gameAreaList)

                    ok = gs.RechargeSelf(userName, amount);

                if (ok)

                    userBalance[userName] += amount;

            }

        }

        public void Consume(string userName, int amount)

        {

            if (userBalance.ContainsKey(userName))

            {

                bool ok = true;

                foreach (GameSystem gs in gameAreaList)

                    ok = gs.ConsumeSelf(userName, amount);

                if (ok)

                    userBalance[userName] -= amount;

            }

        }

        public void QueryBalance(string userName)

        {

            Console.WriteLine("Your balance is " + userBalance[userName]);

        }

    }

    abstract class GameSystem

    {

        private AccountSystem accountSystem;

        protected Dictionary<string, int> userBalance = new Dictionary<string, int>();

        public GameSystem(AccountSystem accountSystem)

        {

            this.accountSystem = accountSystem;

        }

        internal virtual bool CreateAccountSelf(string userName)

        {

            userBalance.Add(userName, 0);

            return true;

        }

        internal virtual bool RechargeSelf(string userName, int amount)

        {

            if (userBalance.ContainsKey(userName))

                userBalance[userName] += amount;

            return true;

        }

        internal virtual bool ConsumeSelf(string userName, int amount)

        {

            if (userBalance.ContainsKey(userName))

                userBalance[userName] -= amount;

            return true;

        }

        public void Recharge(string userName, int amount)

        {

            accountSystem.Recharge(userName, amount);

        }

        public void Consume(string userName, int amount)

        {

            accountSystem.Consume(userName, amount);

        }

    }

    class GameArea1 : GameSystem

    {

        public GameArea1(AccountSystem accountSystem) : base(accountSystem) { }

        internal override bool CreateAccountSelf(string userName)

        {

            Console.WriteLine(userName + " Registered in GameAre1");

            return base.CreateAccountSelf(userName);

        }

        internal override bool RechargeSelf(string userName, int amount)

        {

            base.RechargeSelf(userName, amount);

            Console.WriteLine(userName + "'s amount in GameArea1 is " + userBalance[userName]);

            return true;

        }

        internal override bool ConsumeSelf(string userName, int amount)

        {

            base.ConsumeSelf(userName, amount);

            Console.WriteLine(userName + "'s amount in GameArea1 is " + userBalance[userName]);

            return true;

        }

}

    class GameArea2 : GameSystem

    {

        public GameArea2(AccountSystem accountSystem) : base(accountSystem) { }

        internal override bool CreateAccountSelf(string userName)

        {

            Console.WriteLine(userName + " Registered in GameAre2");

            return base.CreateAccountSelf(userName);

        }

        internal override bool RechargeSelf(string userName, int amount)

        {

            base.RechargeSelf(userName, amount);

            Console.WriteLine(userName + "'s amount in GameArea2 is " + userBalance[userName]);

            return true;

        }

        internal override bool ConsumeSelf(string userName, int amount)

        {

            base.ConsumeSelf(userName, amount);

            Console.WriteLine(userName + "'s amount in GameArea2 is " + userBalance[userName]);

            return true;

        }

    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved