Moment For Technology

BridgePattern

Posted on Dec. 2, 2022, 12:39 p.m. by Anna Raymond
Category: reading Tag: Code specification

Bridge pattern definition

The bridge pattern, also known as the bridge pattern, the interface pattern, or the handle pattern, is a structural pattern that separates the abstract part from its concrete implementation, so that both can be changed independently. The connection between two classes is established by combination, rather than inheritance.

  • Abstract dimensions and implementation dimensions

advantages

  1. Separate the abstract part and its concrete implementation part
  2. The system scalability is improved
  3. Conforms to the open closed principle
  4. It conforms to the principle of synthetic reuse

Example 1

1. Before bridging mode

public interface ICourse {
}
Copy the code
public class JavaCourse implements ICourse {
}
Copy the code
public class PythonCourse implements ICourse {
}
Copy the code
public interface INote {
    void edit();
}
Copy the code
public class JavaNote implements INote {
    @Override
    public void edit() {

    }
}
Copy the code
public class PythonNote implements INote {
    @Override
    public void edit() {

    }
}
Copy the code
public interface IVideo {
}
Copy the code
public class JavaVideo implements IVideo {
}
Copy the code
public class PythonVideo implements IVideo {
}
Copy the code

2. After bridging mode

At the top of the code base changes, add a bridge to AbstractCourse.java

public class JavaCourse extends AbstractCourse {
}
Copy the code
public class PythonCourse extends AbstractCourse {
}
Copy the code
public class AbstractCourse implements ICourse{ private INote note; private IVideo video; public void setNote(INote note) { this.note = note; } public void setVideo(IVideo video) { this.video = video; } @Override public String toString() { return "AbstractCourse{" + "note=" + note + ", video=" + video + '}'; }}Copy the code

Example 2

1. Before bridging mode

Public interface IMessage {void send(String message,String toUser); }Copy the code
/** * Implements IMessage */ public class implements IMessage {@override public void send(String message implements, String message) String toUser) {system.out.println (message+". Send to: "+ toUser); }}Copy the code
/** * implements IMessage */ public class implements IMessage {@override public void send(String message, implements IMessage); String toUser) {system.out.println (message +". Send to: "+ toUser); }}Copy the code
Public class NomalMessage {public void sendMessage(String message, String toUser) {system.out.println ("[normal]" + message + "- "to:" + toUser); }}Copy the code
Public class UrgencyMessage {public void sendMessage(String message, String toUser) {system.out.println ("[urgent]" + message + "- "send to:" + toUser); }}Copy the code

2. After bridging mode

Public interface IMessage {void send(String message,String toUser); }Copy the code
/** * Implements IMessage */ public class implements IMessage {@override public void send(String message implements, String message) String toUser) { System.out.println(message+ toUser); }}Copy the code
/** * implements IMessage */ public class implements IMessage {@override public void send(String message, implements IMessage); String toUser) { System.out.println(message +toUser); }}Copy the code
Public abstract class AbstractMessage {private IMessage message; public AbstractMessage(IMessage message) { this.message = message; } void sendMessage(String message,String toUser) { this.message.send(message,toUser); }}Copy the code
Public UrgencyMessage extends AbstractMessage{public UrgencyMessage extends AbstractMessage{public UrgencyMessage extends AbstractMessage (IMessage message) { super(message); } public void sendMessage(String message, String toUser) {message = "[urgent]" + message + "- "send to: "; super.sendMessage(message,toUser); }}Copy the code
Public class NomalMessage extends AbstractMessage{public NomalMessage(IMessage message) {super(message);  } public void sendMessage(String message, String toUser) {message = "[normal]" + message + "- "sent to: "; super.sendMessage(message,toUser); }}Copy the code
public class Test { public static void main(String[] args) { IMessage message = new SmsMessage(); AbstractMessage abstractMessage = new NomalMessage(message); Overtime abstractMessage. SendMessage (" application ", "mr.wong"); IMessage message2 = new EmailMessage(); AbstractMessage abstractMessage2 = new UrgencyMessage(message2); Overtime abstractMessage2. SendMessage (" application ", "mr.wong"); }}Copy the code

JDBC bridge mode

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.