Bridge mode of design mode (bridge)--structural model

Source: Internet
Author: User

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

  
 
  1. public abstract class AbstractMessage {
  2. protected ISendMethod sm;
  3. public AbstractMessage(ISendMethod sm) {
  4. super();
  5. this.sm = sm;
  6. }
  7. public void sendMessage(String str){
  8. this.sm.send(str);
  9. }
  10. }

Commonmessage class

  
 
  1. public class CommonMessage extends AbstractMessage{
  2. public CommonMessage(ISendMethod sm) {
  3. super(sm);
  4. // TODO Auto-generated constructor stub
  5. }
  6. @Override
  7. public void sendMessage(String str) {
  8. // TODO Auto-generated method stub
  9. super.sendMessage(str);
  10. }
  11. }

Urgentmessage class

  
 
  1. public class UrgentMessage extends AbstractMessage{
  2. public UrgentMessage(ISendMethod sm) {
  3. super(sm);
  4. // TODO Auto-generated constructor stub
  5. }
  6. @Override
  7. public void sendMessage(String str) {
  8. // TODO Auto-generated method stub
  9. str += "(Urgent!!!)";
  10. this.sm.send(str);
  11. }
  12. }

Isendmethod interface

 
   
  
  1. public interface ISendMethod {
  2. public void send(String str);
  3. }

SendEmail class

  
 
  1. public class SendEmail implements ISendMethod {
  2. @Override
  3. public void send(String str) {
  4. // TODO Auto-generated method stub
  5. System.out.println("This is a email message:"+ str);
  6. }
  7. }

Sendsms class

  
 
  1. public class SendSms implements ISendMethod {
  2. @Override
  3. public void send(String str) {
  4. // TODO Auto-generated method stub
  5. System.out.print("This a sms message:" + str);
  6. }
  7. }

Main test class

  
 
  1. public class Main {
  2. public static void main(String[] args){
  3. ISendMethod sender = new SendEmail();
  4. AbstractMessage aMessage = new UrgentMessage(sender);
  5. aMessage.sendMessage("Test");
  6. ISendMethod sender2 = new SendSms();
  7. AbstractMessage aMessage2 = new CommonMessage(sender2);
  8. aMessage2.sendMessage("Test2");
  9. }
  10. }

Output results

This is a email message:test (Urgent!!!)
This a SMS Message:test2



From for notes (Wiz)

List of attachments
    • Bridge.jpg
    • Bridge.png

Bridge mode of design mode (bridge)--structural model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.