1. Intentions
Separate the abstract part from its implementation so that they can vary independently.
2. Applicability
- You don't want to have a fixed binding relationship between the abstraction and its implementation section.
- The abstraction of a class and its implementation should be extended by means of subclasses.
- The part of the abstraction and implementation can change independently without affecting each other.
- Extend the application from multiple dimensions.
3. Structure
4. Participants
- Abstraction: Defines an abstract interface, and maintains a reference to a Implementor object.
- Refinedabstraction: Expands the interface with abstracttion definition.
- Implementor: The interface that defines the implementation class, which is not necessarily identical to the abstraction interface, in fact the two interfaces can be completely different. In general, the Implementor interface provides only basic operations, while abstraction defines a higher level of operations based on these basic operations.
- Concreteimplementor: Implements the Implementor interface and defines its implementation.
5. Effects
1) Separating the interface and its implementation part.
2) provides scalability.
3) Implementation details are transparent to the customer.
6. Example 1) Scenario
In the scenario where the message is sent, the message type is abstracted as normal and urgent messages, and sent as messages and text messages. With bridging mode, you can add various types of abstractions to the dimension of a message type, or you can add various implementations on the Send as dimension. The two are independently changed to achieve the decoupling effect.
2) UML diagram
3) Code
Abstractmessage Abstract class
public abstract class AbstractMessage {
protected ISendMethod sm;
public AbstractMessage(ISendMethod sm) {
super();
this.sm = sm;
}
public void sendMessage(String str){
this.sm.send(str);
}
}
Commonmessage class
public class CommonMessage extends AbstractMessage{
public CommonMessage(ISendMethod sm) {
super(sm);
// TODO Auto-generated constructor stub
}
@Override
public void sendMessage(String str) {
// TODO Auto-generated method stub
super.sendMessage(str);
}
}
Urgentmessage class
public class UrgentMessage extends AbstractMessage{
public UrgentMessage(ISendMethod sm) {
super(sm);
// TODO Auto-generated constructor stub
}
@Override
public void sendMessage(String str) {
// TODO Auto-generated method stub
str += "(Urgent!!!)";
this.sm.send(str);
}
}
Isendmethod interface
public interface ISendMethod {
public void send(String str);
}
SendEmail class
public class SendEmail implements ISendMethod {
@Override
public void send(String str) {
// TODO Auto-generated method stub
System.out.println("This is a email message:"+ str);
}
}
Sendsms class
public class SendSms implements ISendMethod {
@Override
public void send(String str) {
// TODO Auto-generated method stub
System.out.print("This a sms message:" + str);
}
}
Main test class
public class Main {
public static void main(String[] args){
ISendMethod sender = new SendEmail();
AbstractMessage aMessage = new UrgentMessage(sender);
aMessage.sendMessage("Test");
ISendMethod sender2 = new SendSms();
AbstractMessage aMessage2 = new CommonMessage(sender2);
aMessage2.sendMessage("Test2");
}
}
Output results
This is a email message:test (Urgent!!!)
This a SMS Message:test2
From for notes (Wiz)
List of attachments
Bridge mode of design mode (bridge)--structural model