zoukankan      html  css  js  c++  java
  • 桥接模式(Bridge Pattern)

    桥接模式(Bridge Pattern)

    桥接模式也称桥梁模式接口模式是将抽象部分和具体实现部分进行分离,使得他们独立变化,简而言之,我们之前想拥有一个类的功能使用的是继承,那现在我们就用组合的方式把他们联合在一起,而不是继承,实际上一种多重继承的替代方案。

    illustrate bridge pattern with a sceneof sending message

    first we have two methods that are email and sms

    public interface IMessage {
        void sendMessage(String content,String receiver);
    }
    public class EMailMessage implements  IMessage {
        @Override
        public void sendMessage(String content, String receiver) {
            System.out.println(" To Use E-mail send【"+content+"】to-->"+receiver);
        }
    }
    public class SmsMessage implements IMessage {
        @Override
        public void sendMessage(String content, String receiver) {
            System.out.println(" To Use Sms  send【"+content+"】to-->"+receiver);
        }
    }

    we have two patterns that are  urgent and normal

    public abstract class AbstractMessage {
        private IMessage iMessage;
    
    
        public AbstractMessage(IMessage iMessage){
            this.iMessage=iMessage;
    
        }
        public void sendMessage(String content, String receiver) {
            this.iMessage.sendMessage(content,receiver);
        }
    
    }
    public class NormalMessage  extends  AbstractMessage {
        public NormalMessage(IMessage iMessage) {
            super(iMessage);
        }
    }
    public class UrgentMessage extends  AbstractMessage  {
        public UrgentMessage(IMessage iMessage) {
            super(iMessage);
        }
    
        public void sendMessage(String content, String receiver) {
            content="[Hot..]"+content;
            super.sendMessage(content,receiver);
        }
        // You also can do whatever you want here
        public Object watch(String messageId){
            return null;
        }
    }

    tips:in class of AbstractMessage hold IMessage(that is key to bridge pattern,in the other word,all classes that extends class of AbstractMessage  can use methods of  classes that implements IMessage)

    To text

    public class Test {
        public static void main(String[] args) {
            // use sms to send message
            IMessage iMessage=new SmsMessage();
            // send message with pattern of normal
            AbstractMessage abstractMessage=new NormalMessage(iMessage);
            abstractMessage.sendMessage("I'm applying for overtime","boss");
    
            // use e-mail to send message
            iMessage=new EMailMessage();
            // send message with pattern of urgent
            AbstractMessage message=new UrgentMessage(iMessage);
    
            message.sendMessage("I'm applying for overtime","boss");
        }
    }

    To explain

    actually, we use AbstractMessage  as a bridge to combine urgency degree with pattern of sending message

    How  is bridge pattern applied in source of code

    java.sql.DriverManager is a representative example

     java.sql.DriverManager#getConnection(java.lang.String, java.util.Properties, java.lang.Class<?>)

     

    Sum up

    Advantages:

      expansibility:We can add new classes implementing and do not change our 'bridge',Two different dimension can be combined with each other

    Disadvantage:

      complexity:We shoud to know Which interfaces should be implement

    tips: in example of sending message, e-mail and sms are  instances and urgent degree  are part of abstract ,we combine them with  class named AbstractMessage.in jdbc connecting,'java.sql 'is abstract and mysql is instance

     

  • 相关阅读:
    C++迭代器
    JdbcTemplateUtil 工具类分享
    PE和CDlinux二合一启动盘制作
    程序员自述——2019新年篇
    HTML/CSS常用单词
    JAVA学习常用单词
    Spring集成Mybatis3
    Spring集成struts2
    解决VS2010打开Web页面时经常由于内存较低而导致VS2010自动关闭的问题
    年终总结
  • 原文地址:https://www.cnblogs.com/UpGx/p/14724937.html
Copyright © 2011-2022 走看看