首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 設計模式無廢話系列二十:中介者模式(二)—— 通訊流程與完整程式碼

     編輯:關於C語言

引入 AccountSystem 後的通訊流程

  1. 網站呼叫 AccountSystem 註冊方法
  2. AccountSystem 呼叫 A、B、C 大區註冊方法
  3. A、B、C 大區充值方法呼叫 AccountSystem 充值方法
  4. A、B、C 大區消費方法呼叫 AccountSystem 消費方法
  5. AccountSystem 充值方法呼叫各大區專屬充值方法(僅針對本大區)
  6. AccountSystem 消費方法呼叫各大區專屬消費方法(僅針對本大區)

步驟 (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