Design mode (12): Bridging mode

Source: Internet
Author: User

First, the definition

In a software system, some types, because of their own logic, have two or more dimensions that change, so how do you deal with this "multidimensional change"? How can you use object-oriented technology to make this type easily change in multiple directions without introducing additional complexity? This will use Bridge mode.

When an abstraction may have multiple implementations, inheritance is often used to reconcile. An abstract class defines an interface to that abstraction, and a specific subclass is implemented in a different way. The inheritance mechanism fixed the abstract part with its implementation part, making it difficult to modify, extend and reuse the abstract part and the implementation part independently.

If an abstract class or interface has more than one concrete implementation subclass, and these subclasses have content or conceptual overlap, we need to separate the common parts of the abstraction: that is, it is intended to be placed in an interface, now we need to design two interfaces-abstract interface and behavior interface. Then, the method and invocation relationship of abstract interface and behavior interface are defined for each specific subclass.

Separate the abstractions from the implementation, so that they can be changed independently.

Second, example:

On a Windows system we have a write operation, this write operation multiple types (txt\xml)

But now the functionality is not confined to the Windows platform, it needs to be implemented on the MAC as well. and also to increase the write type (bit stream)

Abstract System:

 Public Abstract class OS    {        publicabstractvoid  writetxt ();          Public Abstract void WriteXML ();    }

Realize:

  Public classMac:os { PublicMac () {Console.WriteLine ("mac System"); }         Public Override voidWritetxt () {Console.WriteLine ("Writetype:txt"); }         Public Override voidWriteXML () {Console.WriteLine ("Writetype:xml"); }    }     Public classWin:os { PublicWin () {Console.WriteLine ("Windows System"); }         Public Override voidWritetxt () {Console.WriteLine ("Writetype:txt"); }         Public Override voidWriteXML () {Console.WriteLine ("Writetype:xml"); }    }

Abstract Write operations:

 Public Abstract class Iohelp    {        public OS os; // Take a bridge        .  Public iohelp (OS _os)        {            = _os;        }          Public Abstract void Write ();
}

Realize:

  Public classTxthelp:iohelp { PublicTxthelp (OS _os):Base(_os) {} Public Override voidWrite () {OS.        Writetxt (); }          }     Public classXmlhelp:iohelp { PublicXmlhelp (OS _os):Base(_os) {} Public Override voidWrite () {OS.        WriteXML (); }           }

Client:

// ---------------------Bridging mode------------------------            New Bridge.mac ();             New Bridge.xmlhelp (MAC);            Txt. Write ();            Console.readkey ();

Iii. Summary

The old way: we write in an abstract way (because there are many write types), followed by the need to run different writing methods on different platforms.

That's it. We: Bridging mode

Abstract system, abstract writing method (Bridging the system), using the system's specific implementation methods.

-----example is hard to think of. -----

Design mode (12): Bridging mode

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.