(Bridge Pattern)

(Bridge Pattern)

Bridge mode is also known as bridge mode and interface modeIt is to separate the abstract part from the concrete implementation part,To make them change independently, in short, we used to want to have a class that uses inheritance, and now we are combining them together in a compositional way, rather than inheritance, which is actually an alternative to multiple inheritance.

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

 

 

 

posted @ 2021-05-01 22:28  UpGx  阅读(348)  评论(0)    收藏  举报