Event实例应用 汽车销售与顾客

项目场景:

汽车销售员有一张客户列表,当新客户注册时,汽车销售员通知客户已注册成功,如果有新车则推送给客户。如果客户不希望再收到推送,则注销该客户。


代码描述:

首先定义两个委托, InformationHandler通知客户注册成功和新车推送。LogOffHandler 通知客户已注销。

using System;
namespace EventsSample
{
    delegate void InformationHandler(Information x);
    delegate void LogOffHandler(Information x);
}

再定义一个消息类,方便以后扩展

using System;
namespace EventsSample
{    class Information
    {
        public string _information;
        public Information(String information)
        {
            _information=information;
        }
    }
}

定义一个销售员类,包括定义相应事件InformationHandler Informing, LogOffHandler LoggedOff,以及一系列通知客户的方法。这里提一下?,这个符号这里作为Null-Conditional操作符,它将检查主体object是否是null, 如果是null则返回null值,不会触发异常。

using System;
namespace EventsSample
{
    class CarDealer
    {
        public event InformationHandler Informing;
        public event LogOffHandler LoggedOff;
        public void NewCar(string car, double price)
        {
            Console.WriteLine($"CarDealer, New car {car}, Price ${price}");
            Informing?.Invoke(new Information($" New car {car}, Price ${price} is here."));
        } 
        public void RegisteCustomer()
        {
            Informing?.Invoke(new Information("Succesfully Registed!"));
        }
        public void CancelInform()
        {
            LoggedOff?.Invoke(new Information("You will no logner received new information!"));
        }
        public void RegisteList()
        {
            Informing.Invoke(new Information("You will receive our new information!"));
        }
    }
}

定义一个客户类,只包括名字和通知方法Inform(Information e)

using System;
namespace EventsSample
{
    class Customer
    {
        public string Name{get;}
        public Customer(string name)
        {
            Name=name;
        }
        public void Inform(Information e)
        {
            Console.WriteLine($"{Name}, {e._information}");
        }
    }
}

最后,测试一下,先注册两个客户,Max和Luc,他们将分别受到注册成功消息,之后收到新车推送。再将Max从正常消息通知事件类中移除,并加入到注销消息通知事件类中,这样只有Max将收到注销消息。再推送新车,只有Luc收到。

using System;

namespace EventsSample
{

    class Program
    {
        static void RegistNewCustomer(Customer customer,CarDealer carDealer)
        {

        }
        static void Main(string[] args)
        {
            CarDealer dealer=new CarDealer();
            Customer Max=new Customer("Max");
            Customer Luc=new Customer("Luc");
            
            dealer.Informing+=Max.Inform;
            dealer.Informing+=Luc.Inform;
            dealer.RegisteCustomer();
            dealer.RegisteList();
            Console.WriteLine("-----------------------------");
            dealer.NewCar("Dodge journey",31000.00);
            Console.WriteLine("-----------------------------");
            dealer.Informing-=Max.Inform;
            dealer.LoggedOff+=Max.Inform;
            dealer.CancelInform();
            dealer.LoggedOff-=Max.Inform;
            Console.WriteLine("-----------------------------");
            dealer.NewCar("Benz GL450",53000.00);
            Console.WriteLine("-----------------------------");
        }
    }
}


结果:

用分割线隔开,更清楚
在这里插入图片描述

总结:

这里用Event模拟了汽车销售与顾客的消息互动,当然这里只是为了展示Event机制和其好处,并不是这种项目的最佳方案,依靠它可以让dealer类和customer类相互解绑,降低类依赖性,增加灵活度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值