⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 GOF23种设计模式详细例子!附有详细的代码噢!
💻 CS
字号:
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]);
        }
    }

    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;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -