Bridge in design mode Learning)

Source: Internet
Author: User

Purpose:Separate the abstract part from its implementation part so that they can all change independently.
Implementation points:Separation and aggregation. Divides an object into two parts. They are maintained by aggregation.
UML:

Code:In this example, the electrical switch function is separated from the switch function to adapt to different changes.

    Public    Abstract     Class  SW
{
Public Abstract Void Openandclose ();
}
// Unidirectional Switch
Class Singlesw: SW
{
Public Override Void Openandclose ()
{
Console. writeline ( " This is a single switch! " );
}
}
// Bidirectional Switch
Class Doublesw: SW
{
Public Override Void Openandclose ()
{
Console. writeline ( " This is a double switch !! " );
}
}

Public Abstract Class El_device
{
Protected SW _ switcher;
Public SW switcher
{
Set {_ Switcher = Value ;}
}
Public Virtual Void Openandclose ()
{
_ Switcher. openandclose ();
}
}
// Refrigerator class, playing the role of bridging
Public Class Frigde: el_device
{
Public Override Void Openandclose ()
{
_ Switcher. openandclose ();
}
}
// ------------------------- Execute --------------------------
Class Program
{
Static Void Main ( String [] ARGs)
{
// Xinfei refrigerator is a one-way switch
El_device Xinfei = New Frigde ();
Xinfei. switcher = New Singlesw ();
// Midea refrigerator is a two-way switch
El_device Meidi = New Frigde ();
Meidi. switcher = New Doublesw ();

Xinfei. openandclose ();
Meidi. openandclose ();
Console. Readline ();
}
}
 
 

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.