Discussion on the Design Pattern series 11-bridging Pattern

Source: Internet
Author: User

Story

In the pursuit of light snow, some of them are friends who are familiar with and some friends who have never met each other. In the past few months, Xiao Xue has been maintaining normal communication with them. However, one of the boys impressed her deeply. He is a humorous, witty, lively, and funny person. Day by day, Xiao Xue had a kind of curiosity about him, more than once in his mind to draw his voice and smile.

At last one day, Xiao Xue told him whether he could send a photo to me via his cell phone. However, Xiao Xue's mobile phone can only receive text messages from different mobile phones. Is there a way for Xiaoxue's mobile phone to receive text messages sent by different mobile phones and photos sent by different mobile phones?

Bridge Solution
Gof pointed out that,The bridging mode decouples abstract actions and implementations so that the two can change independently.. In other words, the changes in the two dimensions are separated.
Abstract: The conceptual relationships that exist in multiple entities are abstract. For example, different mobile phone brands (Nokia and Motorola ).
Implementation: The specific implementation provided by abstraction is implementation. For example, the type of message to be sent (Common SMS or MMS)
Roles defined in the Bridge Mode:
Abstract role (mobile): abstract interface definition
Specific abstract roles (Nokia and motorala): Specific abstract implementations
Implementation Role (type): Implementation Interface Definition
Specific implementation roles (messagetype and picturetype): specific implementation of the role Interface

Abstract roles are as follows:

Namespace Xiaoxue
{
Public abstract mobile
{
Public type _ type;
Public Mobile (type)
{
This. _ type = type;
}
Public abstract void sendmessage (Object MSG );
Public abstract void sendpictue (Object pic );
}
}

The specific abstract roles are as follows:

Namespace Xiaoxue
{
Public class Nokia: Mobile
{
Public Nokia (type): Base (type)
{
}
Public override void sendmessage (Object MSG)
{
// Send a Nokia text message;
}
Public override void sendmessage (Object MSG)
{
// Send Nokia MMS;
}
}

Public class MOTOROLA: Mobile
{
Public Motorola (type): Base (type)
{
}
Public override void sendmessage (Object MSG)
{
// Send a Motorola SMS;
}
Public override void sendmessage (Object MSG)
{
// Send Motorola MMS;
}
}
}

The implementation roles are as follows:

Namespace Xiaoxue
{
Public abstract class type
{
Public abstract string GetType ();
}
}

The specific implementation roles are as follows:

Namespace Xiaoxue
{
Public class messagetype: Type
{
Public override string GetType ()
{
Return "message ";
}
}
Public class picturetype: Type
{
Public override string GetType ()
{
Return "picture ";
}
}
}

The client call is as follows:

Moblile mobile = new Nokia (New messagetype ())
If (mobile. GetType () = "message ")
{
Mobile. sendmessage ("hello ");
}

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.