设计模式-中介者模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 中介着模式
{ //抽象中介者角色,用于各个同事角色之间的通信 
    abstract class  AbstractMediator
    {
        protected Purchase purchase;
        protected Stock stock;
        protected Sale sale;
       public AbstractMediator()
        {
            purchase = new Purchase(this);
            stock = new Stock(this);
            sale = new Sale(this);
        }
	public abstract void Execute(string str,params object[] obj);
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 中介着模式
{
    class Mediator:AbstractMediator
    {
        public override void Execute(string str,params object[] obj)
        {
            if (str.Equals("purchase.buy"))
            {
                BuyComputer((int) obj[0]);
            }
            else if (str.Equals("sale.sell"))
            {
                SellComputer((int) obj[0]);
            }else if (str.Equals("sale.offsell"))
            {
                OffSale();
            }else if (str.Equals("stock.clear"))
                ClearStock();
	 private void BuyComputer(int number)
        {
            int salestate = sale.GetSaleState();
            if (salestate > 80)
            {
                Console.WriteLine("采购IBM电脑" + number + "台");
                stock.Increase(number);
            }
            else
            {
                int buyNum = number/2;
                Console.WriteLine("采购IBM电脑" + buyNum + "台");
            }
        }  
  private void SellComputer(int number)
        {
            if (stock.GetStockComputer() < number)
                purchase.buyIbmComputer(number);
            stock.Decrease(number);
        }    
   private void OffSale()
        {
            Console.WriteLine("折价销售电脑" + stock.GetStockComputer() + "台");
        }
    private void ClearStock()
        {
            sale.offSale();
            purchase.refuseBuyIBM();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 中介着模式
{
    abstract class AbstractColleague
    {
        protected AbstractMediator mediator;
       public AbstractColleague(AbstractMediator _mediator)
        {
            mediator = _mediator;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 中介着模式
{
    class Purchase:AbstractColleague
    {
        public Purchase(AbstractMediator _mediator):base(_mediator)
        {
        }
        //采购IBM电脑
        public void buyIbmComputer(int number)
        {
                     mediator.Execute("purchase.buy", number);
        }
       public void refuseBuyIBM()
        {
            Console.WriteLine("不再采购电脑");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 中介着模式
{
    /// <summary>
    /// 库存管理
    /// </summary>
    class Stock:AbstractColleague
    {
        public Stock(AbstractMediator _mediator) : base(_mediator)
        {
            
        }
        private static int COMPUTER_NUM = 100;
     public void Increase(int number)
        {
            COMPUTER_NUM += number;
            Console.WriteLine("库存为:" + COMPUTER_NUM);
        }
public void Decrease(int number)
        {
            COMPUTER_NUM -= number;
            Console.WriteLine("库存为:" + COMPUTER_NUM);
        }
  public int GetStockComputer()
        {
            return COMPUTER_NUM;
        }
 public void clearStock()
        {
            Console.WriteLine("清理库存数量为 " + COMPUTER_NUM);
            mediator.Execute("stock.clear");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 中介着模式
{
    /// <summary>
    /// 销售管理
    /// </summary>
    class Sale:AbstractColleague
    {
        public Sale(AbstractMediator _mediator):base(_mediator)
        {}
        /// <summary>
        /// 销售IBM电脑
        /// </summary>
        /// <param name="number"></param>
        public void sellIBMcomputer(int number)
        {
                        mediator.Execute("sale.sell", number);
            Console.WriteLine("销售IBM电脑" + number + "台");
        }
       public int GetSaleState()
        {
            Random random = new Random(System.DateTime.Now.Millisecond);
            int saleState = random.Next(1, 100);
            Console.WriteLine("电脑的销售情况 " + saleState);
            return saleState;
        }
    public void offSale()
        {
                       mediator.Execute("sale.offsell");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 中介着模式
{
    class Program
    {
        static void Main(string[] args)
        {
            AbstractMediator mediator = new Mediator();
            Console.WriteLine("采购开始采购");
            Purchase purchase = new Purchase(mediator);
            purchase.buyIbmComputer(100);
            Sale sale = new Sale(mediator);
            sale.sellIBMcomputer(1);
            Stock stock = new Stock(mediator);
            stock.clearStock();
          Console.ReadKey();
        }
    }
}
</p>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源中直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值