zoukankan      html  css  js  c++  java
  • 中介者模式

    namespace MediatorPattern
    {
        // 抽象牌友类
        public abstract class AbstractCardPartner
        {
            public int MoneyCount { get; set; }
    
            public AbstractCardPartner()
            {
                MoneyCount = 0;
            }
    
            public abstract void ChangeCount(int Count, AbstractMediator mediator);
        }
    
        // 牌友A类
        public class ParterA : AbstractCardPartner
        {
            // 依赖与抽象中介者对象
            public override void ChangeCount(int Count, AbstractMediator mediator)
            {
                mediator.AWin(Count);
            }
        }
    
        // 牌友B类
        public class ParterB : AbstractCardPartner
        {
            // 依赖与抽象中介者对象
            public override void ChangeCount(int Count, AbstractMediator mediator)
            {
                mediator.BWin(Count);
            }
        }
    
        // 抽象中介者类
        public abstract class AbstractMediator
        {
            protected AbstractCardPartner A;
            protected AbstractCardPartner B;
            public AbstractMediator(AbstractCardPartner a, AbstractCardPartner b)
            {
                A = a;
                B = b;
            }
    
            public abstract void AWin(int count);
            public abstract void BWin(int count);
        }
    
        // 具体中介者类
        public class MediatorPater : AbstractMediator
        {
            public MediatorPater(AbstractCardPartner a, AbstractCardPartner b)
                : base(a, b)
            {
            }
    
            public override void AWin(int count)
            {
                A.MoneyCount += count;
                B.MoneyCount -= count;
            }
    
            public override void BWin(int count)
            {
                B.MoneyCount += count;
                A.MoneyCount -= count;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                AbstractCardPartner A = new ParterA();
                AbstractCardPartner B = new ParterB();
                // 初始钱
                A.MoneyCount = 20;
                B.MoneyCount = 20;
    
                AbstractMediator mediator = new MediatorPater(A, B);
    
                // A赢了
                A.ChangeCount(5, mediator);
                Console.WriteLine("A 现在的钱是:{0}", A.MoneyCount);// 应该是25
                Console.WriteLine("B 现在的钱是:{0}", B.MoneyCount); // 应该是15
    
                // B 赢了
                B.ChangeCount(10, mediator);
                Console.WriteLine("A 现在的钱是:{0}", A.MoneyCount);// 应该是15
                Console.WriteLine("B 现在的钱是:{0}", B.MoneyCount); // 应该是25
                Console.Read();
            }
        }
    }
  • 相关阅读:
    python 利用正则表达的式提取特定数据如手机号
    python 横向比较最大值 贴标签
    Go语言基础之17--Redis基本操作
    Mysql5.7.20源码编译安装
    Go语言基础之16--Mysql基本操作
    Go语言学习包(1)之bufio包
    Go语言基础之15--文件基本操作
    Go语言基础练习题系列5
    Go语言基础之14--Waitgroup和原子操作
    Go语言基础之13--线程安全及互斥锁和读写锁
  • 原文地址:https://www.cnblogs.com/gaocong/p/6824660.html
Copyright © 2011-2022 走看看