桥接模式+C#发送邮件+配置文件

目录

一、桥接模式

二、桥接模式角色

三、代码实例一

UML 


一、桥接模式

抽象化和实现化脱耦,使得二者可以独立变化。桥接模式将继承方式转化为了组合聚合方式,强关联就变成了弱关联。

本质是分离抽象和实现。

符合 组合/聚合原则,符合开放封闭原则,松耦合,高内聚

耦合:两个实体行为的某种强关联。

解耦:将强关联去掉或改成弱关联。

继承->聚合

该模式包含两个等级结构:抽象化和实现化

二、桥接模式角色

分为两个等级

一是抽象化等级结构,二是实现化等级结构。

三、代码实例一

定义一个接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeOne
{
    interface Implementor
    {
        void Operation();
    }
}

然后是实现接口的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeOne
{
    class ConcreteImplementorA : Implementor
    {
        public void Operation()
        {
            Console.WriteLine("实现A方法!");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeOne
{
    class ConcreteImplementorB : Implementor
    {
        public void Operation()
        {
            Console.WriteLine("实现B方法!");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeOne
{
    class Abstraction
    {
        protected Implementor im;
        public void SetImplementor(Implementor e)
        {
            im = e;
        }
        public virtual void Operation()
        {
            im.Operation();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeOne
{
    class RefineAbstraction : Abstraction
    {
        public override void Operation()
        {
            im.Operation();
        }
    }
}

四、代码实例二

先实现普通消息和加急消息的功能,发送方式先实现站内短消息和Email这两种

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeTwo
{
    interface MessageImplementor
    {
        void Send(string message, string user);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeTwo
{
    class MessageEmail : MessageImplementor
    {
        public void Send(string message, string user)
        {
            Console.WriteLine("给" + user + "发送" + message);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeTwo
{
    class MessageSMS : MessageImplementor
    {
        public void Send(string message, string user)
        {
            Console.WriteLine("给" + user + "以邮件形式发送" + message);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeTwo
{
    class MessageMobile : MessageImplementor
    {
        public void Send(string message, string user)
        {
            Console.WriteLine("使用手机给"+user+"发送"+message);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeTwo
{
    class AbstractMessage
    {
        protected MessageImplementor im;

        public AbstractMessage(MessageImplementor im)
        {
            this.im = im;
        }
        public void SendMessage(string message, string user)
        {
            this.im.Send(message, user);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeTwo
{
    class CommonMessage:AbstractMessage
    {
        public CommonMessage(MessageImplementor IM) : base(IM) { }
        public void SendMessage(string message,string user)
        {
            base.SendMessage(message+"88", user);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BridgeTwo
{
    class UrgencyMessage : AbstractMessage
    {
       public UrgencyMessage(MessageImplementor IM) : base(IM) { }
        public void SendMessage(string message, string user)
        {
            base.SendMessage(message + "99", user);
        }

        public Object Watch(string messaged)
        {
            return null;
}
    }
}

修改一下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace BridgeTwo
{
    class MessageEmail : MessageImplementor
    {
        public void Send(string mailSubject, string mailContent)
        {
            string smtpServer = System.Configuration.ConfigurationManager.AppSettings["smtpServer"];//SMTP服务器  //smtp.qq.com 
            string mailFrom = System.Configuration.ConfigurationManager.AppSettings["mailFrom"];  // 当前发件箱登陆名
            string userPassword = System.Configuration.ConfigurationManager.AppSettings["userPassword"]; //授权码,在当前发送邮箱里面进行设置 
            string mailTo = System.Configuration.ConfigurationManager.AppSettings["mailTo"];  //收件人:收件箱邮箱名称,多个收件人以分号;做分割符
            bool b = false;
            string[] mailTos = mailTo.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            // 邮件服务设置
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            //smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;//指定电子邮件发送方式
            smtpClient.Host = smtpServer; //指定SMTP服务器
            smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
            for (int i = 0; i < mailTos.Length; i++)
            {
                // 发送邮件设置        
                MailMessage mailMessage = new MailMessage(mailFrom, mailTos[i]);
                mailMessage.Subject = mailSubject;//主题
                mailMessage.Body = mailContent;//内容
                mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
                mailMessage.IsBodyHtml = true;//设置为HTML格式
                mailMessage.Priority = MailPriority.Low;//优先级
                try
                {
                    smtpClient.Send(mailMessage); // 发送邮件
                    b = true;
                    Console.WriteLine("ok!");
                }
                catch (SmtpException ex)
                {
                    Console.WriteLine(ex.Message);
                    
                }
            }
        }
    }
}

 客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BridgeTwo
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            MessageImplementor im = new MessageEmail();
            UrgencyMessage me = new UrgencyMessage(im);//
            me.SendMessage("博客地址(17计算机二班 20171789 冯硕 )", "老师您好!这是设计模式专题的博客地址:https://blog.csdn.net/lanshan1111/column/info/35536");

        }
    }
}

就完成了邮件发送

效果图

UML 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值