• 18.中介者模式(Mediator Pattern)


    using System;
    
    namespace Test
    {
        class Program
        {
            /// <summary>
            /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关系。
            /// 中介者使各个对象之间不需要显式地相互引用,从而使耦合性降低,而且可以独立地改变它们之间的交互行为。
            /// </summary>
            /// <param name="args"></param>
            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赢了
                mediator.AWin(5);
                Console.WriteLine("A 现在的钱是:{0}", A.MoneyCount);
                // 应该是25
                Console.WriteLine("B 现在的钱是:{0}", B.MoneyCount);
                // 应该是15
    
                // B 赢了
                mediator.BWin(10);
                Console.WriteLine("A 现在的钱是:{0}", A.MoneyCount);
                // 应该是15
                Console.WriteLine("B 现在的钱是:{0}", B.MoneyCount);
                // 应该是25
                Console.Read();
            }
        }
    
        // 抽象牌友类
        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;
            }
        }
    }
  • 相关阅读:
    flume和kafka整合(转)
    Flume目录
    Flume的安装配置
    HBase系统架构及数据结构(转)
    toolbar ,textfield,图片拉伸,Bundle
    poj 1017 Packets
    jQuery使用serialize(),serializeArray()方法取得表单数据+字符串和对象类型两种表单提交的方法
    Android数据加密概述及多种加密方式 聊天记录及账户加密 提供高质量的数据保护
    LintCode-落单的数 III
    LeetCode90:Subsets II
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4651209.html
Copyright © 2020-2023  润新知