为何要设计多功能消息组件
之前写过一篇装饰器模式的文章,感觉不够深入,这次的例子是实现项目中遇到的,所以把它拿出来,再写写,之前也写过消息组件的文章,主要采用了策略模式实现的,即每个项目可以通过配置进行一种消息的订制,如,你可以订制email,sms,rtx,qq等,但不能同时采用多种机制完成消息的发送,这在一些情况下是没有问题的,但有时,我们也需要同时为客户提供多种消息的推送,这在目前还是挺现时的,如在用户下单后,同时为它发email 和短信进行通过,并对每个订单的过程进行跟踪并通知给用户,这对于用户来说,体验非常不错!
多功能消息组件的组成
1 消息元件抽象 MessageComponent
2 默认的消息实现者DefaultMessage
3 装饰器抽象 Decorator
4 实现Email消息的装饰器 EmailDecorator
5 实现SMS消息的装饰器 SMSDecorator
下面将部分核心代码公开于世
using System; namespace test
{
/// <summary>
/// 消息发送元件
/// </summary>
public abstract class MessageComponent
{
public abstract void Send (string message);
} /// <summary>
/// 默认实现消息发达,可以记录日志,订阅事件,实现默认的发送消息等功能
/// </summary>
public class DefaultMessage:MessageComponent
{
public override void Send (string message)
{
Console.WriteLine ("默认的消息发送机制");
} } /// <summary>
/// 对消息发送的装饰,如加个Email发送,SMS发送等
/// </summary>
public abstract class Decorator:MessageComponent
{
private MessageComponent _messageContaion; public Decorator (MessageComponent messageContaion)
{
_messageContaion = messageContaion;
} public override void Send (string message)
{ if (_messageContaion != null)
_messageContaion.Send (message);
}
} /// <summary>
/// Email消息发送装饰器
/// </summary>
public class EmailDecorator:Decorator
{
public EmailDecorator () : this (null)
{
} public EmailDecorator (MessageComponent messageContaion) : base (messageContaion)
{
} public override void Send (string message)
{
base.Send (message);
Console.WriteLine ("采用Email发送消息...");
} } /// <summary>
/// SMS消息发送装饰器
/// </summary>
public class SMSDecorator:Decorator
{
public SMSDecorator () : this (null)
{
} public SMSDecorator (MessageComponent messageContaion) : base (messageContaion)
{
} public override void Send (string message)
{
base.Send (message);
Console.WriteLine ("采用SMS发送消息...");
} }
}
调用代码如下:
MessageComponent defaultMessage = new DefaultMessage ();
Decorator emailDecorator = new EmailDecorator (defaultMessage);
Decorator smsDecorator = new SMSDecorator (emailDecorator);//把emailDecorator添加到sms后,同时它集成了defaultMessage
smsDecorator.Send ("仓储大叔消息组件...");
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAS4AAABoCAIAAADAVIrRAAAOI0lEQVR4nO2c63MVRRbA56uILiD4wMfyFgSTgEDkmfBGkkDCQwQUwQVXEPICFeUtCCEBAXkaWEGEAGWttVu7tVVblVTl2/qsMlX+CbuBKleEvO4Nhu39cG6azkz36TP3JrlzJ+dXp1IzZ7p7ei79m74zcwfnpx+/FkL89OPXHBwcSQznJ1aRgyMA0UHFXsM2JhgPDt/44PC3IXqPeLv3iE29R2zq/eymh2Kx+aGRmx+GGFX4OxnPFfWBGF3UZ3RR39HFfUcX9x1T3HdMcb8xJf2eL+n3fMkjEGmlj6SV9k8r7Z9e2j99S//0LQPStwzI2DIgY+uAjK2PZmx9dCzEO4+Ni8Xj4959/IVYPDH+3SfGvwcxcMJ7AydsGzhh28CJ256cuO3Jie8/OfH9pzLb48UPnpYxafszEJO3PzN5x+8hpuwYNGXHoCk7B03ZOWjqzsFTdw6eumvwtF2Dp+0aAjF995Dpu4dO3z00a/fQrD1Ds/YMy9ozLHvPsOy9w7P3Dp8RixEzPhwxMxbPztonY+TsfSNn7x85e/+oORAfjZrz0XNzIQ48N+/AaBnzD45pj+dfKovFgrK0BWVpCw6lLTiUlnMoPedQek55em55em55Rm55Rm5FRl5FRl7F2LyKsQsrxi48PA5i0eFxi468sOjIC/kQH4/P/3h8QSwmLD4KMXHx0YlLjkFkLjmWufRY5tLjmUuPv7gM4pMXl30y6WWIE5OX348pr5yMxYqTU1ecisXKU9NWnpq28vS0VaenrTo9fdXp6avOTH/1TBbEa2ezXjubDbH6bPbqT2es/nTG6xCVM1+vnLmmcuaayllrKmetOTdr7blZa8/NXntu9hvnIea8cX7OH/4EMXcdxGfz1n02bz3Ehflvyrj40h/b462LC976PBYbPs/ZcClnw6Wcj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yPmJSnlR/X5HikrYKXRiTihR1KXmGcZxkPFd0OmrgSmpXcU+s6I4b6zyStG7Cu2TtCdNz6f73Fb2OuVrQLnvLa/tAO2hqxVrzGQSxi9gaw3Sg+99XtKroGuhEFa0DHfHH2qbsCaWi9kCQnjOM4yTjfUVkMrGqiHeAOPrVZom1/Kpo6qH1KJieS3LfV/QmnY6qIOW97cetCi6JtxuItKZieP8ZJjnvK6pViKvajLrJtaBdRcogR2dqnNi+Nc8wjpOM9xVVo0zL6ipSRtu4qQ/WvmnNxxu02sX6MVS6/33FLlVRrhLPGni3tT13keAuTO34zTMpTwieK1o7Y9pKaQ0/cNNhamuZtrKKjOOk+PuKiIemPeJb8V3EpyJxK9PTCdP7isi86uqVo0wvWkxVXMuIqHizDOMmBO8r4gO9Ex1AxGaYRInjfUWGYTofVpFhAgGryDCBgFVkmEDAKjJMIGAVGSYQsIoMEwhYRYYJBKwiwwQCVpFhAgGryDCBgFVkmEDAKjJMIGAVGSYQsIoMEwhYRYYJBKwiwwQCVpFhAgGryDCBgFVkmEDAKjJMIGAVGSYQsIoMEwhYRYYJBKwiwwQCVpFhAgGryDCBgFVkmEDAKjJMIGAVGSYQsIoMEwhYRYYJBKwiwwSB/wPY4jeLDzBj2QAAAABJRU5ErkJggg==" alt="" />
而如果只希望使用sms发送,那就是:
Decorator smsDecorator = new SMSDecorator ();
smsDecorator.Send ("仓储大叔消息组件...");
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARwAAAA9CAIAAADwEUGQAAAJoElEQVR4nO2b+VOURxrH+TXGZNVgNsHEKCBRQWa8QLkRPMIhXnhAJOhqIh4oYBI8uBSRQwUPUMFVg7KVlLWp3dqtrdoqqOK3jUeqQlX+hGSwKisCAzPvEEznh+edpnmPfnuGlyPD861vUf0+b3dPv9Cfed7u98Xvxx++Q6PRJtqPoFAoU+U36Vij0T5mhAqNNtkyVDOCD4/Rry86/PqiI+CZIUdmhhydGXJ05odH35B97I3Fx94EL8n/E/XS47PA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