Bridge Mode in Design Mode

Source: Internet
Author: User
1. Why the bridge design mode?

Let's talk about the differences between integrated components and independent components in computer hardware. Let's talk about integrated components first.

The memory box contains a theme called a motherboard, which is used to plug in various components, such as sound cards, video cards, and hard disks, and then coordinates these components for work.

In fact, the earliest computers were all integrated components, that is to say, the motherboard could not be plugged in to replace the components, and the integrated video card and sound card were used. The coupling was very high.

Summarize the disadvantages of this design model:

A. If we want to add a motherboard, for example, a MSI motherboard, We need to separately create a MSI motherboard integrated graphics card and a MSI motherboard integrated sound card, adding the Onda motherboard is the same as described earlier, so this structure will be a huge object.

B. If we modify the Asus motherboard category, its sub-classes (Asus motherboard integrated graphics card and Asus motherboard integrated sound card) will be affected.

So is there any way to reduce the coupling of such a design? Here we need to propose a design principle called the synthesis/aggregation design principle. The original intention is to give priority to the synthesis/aggregation method and then consider the inheritance method. what is synthesis/aggregation?

Here, the relationship between the geese and the geese is an aggregation relationship, and the relationship between the wings and the geese is a synthesis relationship. the aggregation relationship represents a weak coupling relationship. The aggregation class (wild goose group) does not necessarily have the object of the aggregated class (wild goose. the compositing relationship represents a relatively

Strong coupling relationship, synthetic class (geese) must have the object of the synthetic class (WINGS.

Then we can decouple it based on the synthesis/aggregation design principles just now. There is an aggregation relationship between the motherboard and the components.

2. What bridge design mode?

The bridge design pattern is to use the design principles of synthesis/aggregation to separate abstract and implementation parts so that they can all change independently.

For the instance just now, the abstract part is the motherboard, and the Implementation part is the component.

3. How to use the bridge design mode

After we modify it according to the bridge design mode, the UML is as follows:

This design mode separates the abstract part from the implementation part, that is, the separation of the Main Board and the component, so that the addition of the main board or sound card does not affect each other, and does not multiply the expansion of sub-classes, modifying the main board category does not affect the component class, making up for the shortcomings of A and B. it also complies with the open-closed design principles.

The source code is as follows:

Motherboard. Class

Public abstract class motherboard {private sound card shengka = NULL; private video card xianka = NULL; Public void Mount sound card (Sound Card Yuanjian) {shengka = Yuanjian;} public void Mount video card (graphics card Yuanjian) {xianka = Yuanjian;} public void run sound card () {If (shengka = NULL) {system. out. println ("Please load the sound card first. "); return;} shengka. run component ();} public void run video card () {If (xianka = NULL) {system. out. println ("Please load the video card first. "); return;} xianka. running element ();} // because the running mode of the specific motherboard is different, leave it to the subclass to implement public abstract void running motherboard ();}

Component. Class

Public abstract class component {public abstract void running component ();}

Asus motherboard. Class

Public class Asus motherboard extends motherboard {@ overridepublic void run motherboard () {// todo auto-generated method stubsystem. out. print ("On Asus motherboard \ n"); this. run the sound card (); this. running graphics card ();}}

Msi motherboard. Class

Public class MSI motherboard extends motherboard {@ overridepublic void run motherboard () {// todo auto-generated method stubsystem. out. print ("On the MSI motherboard \ n"); this. run the sound card (); this. running graphics card ();}}

Sound Card. Class

Public class sound card extends element {@ overridepublic void running component () {// todo auto-generated method stubsystem. Out. println ("running sound card ");}}

Graphics card. Class

Public class video card extends element {@ overridepublic void running element () {// todo auto-generated method stubsystem. Out. println ("running video card ");}}

Main. Class

Public class main {public static void main (string [] ARGs) {motherboard zhuban1 = new Asus motherboard (); zhuban1. mount the sound card (New Sound Card ()); zhuban1. video card (new video card (); zhuban1. running motherboard (); motherboard zhuban2 = new MSI motherboard (); zhuban2. loading sound card (New Sound Card ()); zhuban2. mount the video card (new video card (); zhuban2. run the motherboard ();}}

Final output:

On the Asus Motherboard
Run sound card
Running graphics card
On the MSI Motherboard
Run sound card
Running graphics card

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.