Follow the example learning design pattern (9)-Bridging mode Bridge (structural type)

Source: Internet
Author: User

Bridging mode is a structural design pattern.

Design Intent: separate the abstractions from the implementation, so that they can be changed independently .

When you see the design intent, you may be a little confused, we see that the inheritance and interface are not both abstract and implementation of separation? In particular, interfaces and abstract classes are such implementations Ah! Then why is there such a bridging separation?

Let's look at an example first.

For example: The car brand built-in navigator, we want to achieve, each brand of navigation can be installed on any one brand of the car and start.

There are two car brands: BMW, Mercedes-Benz.

The navigator has three brands: Skywalker, Beidou, and German.

To look at the normal design, we will certainly be using inheritance to implement each combination of installation and start-up processing, each brand + navigation requires a separate class to implement functionality, but there is a problem, if we add another brand then it means to add three more classes, each added a brand is the same, This is a too painful thing ah! So can you find a way to install new brands and turn on navigation without adding classes?

To take a look at the chart below, we will make a combination of the car brand and the Navigator brand.

If you can combine this, then each additional car brand , you just need to add a brand class, this brand and the previous navigation device combination can be.

We can see that the navigator and the car are actually a kind of aggregation relationship, that is, the navigator is just a part of the car, and this part does not disappear with the car dies. So they are loosely coupled, the aggregation of relationships.

Bridging mode follows the synthesis/aggregation multiplexing principle (which we'll cover later in this article), and we'll look at general-purpose generic class diagrams.

Abstraction: An interface that defines an abstract class that defines how the Implementor interface type object is used.

Refinedabstraction: Implements an interface method defined by abstraction and may have some private methods of its own.

Implementor: Defines the implementation of the abstraction abstraction, which is a kind of grafting relationship, like the computer motherboard and sound card, video card relationship, it can be an interface, Implementor interface provides Abstraction The operations required by an abstract interface, while abstraction defines a higher level of operation based on these basic operations.

Concreteimplementor: Implements the Implementor interface and defines its specific implementation.

Let's look at the example code, through the instance code can better understand the Magic Bridge mode, it really is to achieve pluggable mode.

Package com.bridge;/** * 1: Automotive brand abstract class, this abstract class is mainly the installation and launch of navigation, other accessories can also provide methods in this class, * but in order to open-close principle, it is best to add other interfaces. * 2: Bridging is to achieve the call of Plug and go, we think about the motherboard and plug-in, the motherboard provides an interface to define the function of the interface, * by the plug-in to implement the function of the interface. This is the main idea of bridging, the separation between abstraction and realization. * 3: This class defines how to inject a method of an interface to the install and defines how to manipulate the concrete implementation of the injected interface open. * 4: Here can actually use the interface, if the use of interfaces, it is more flexible, later I will write to you a number of examples of the interface *, each accessory an interface. This way, each brand of car can implement multiple interfaces can also add different accessories. * @author Gaoxu * Practice the truth! */public abstract class Abstractvehiclebrand {/** installation navigation method * @author Gaoxu * @param n */public abstract void Install (Inavigat or n);/** Open navigation * Open navigation is the way to invoke the navigation interface implementation class, in fact, that method is the extension of our method. * @author Gaoxu */public abstract Void Open ();} </span>

<span style= "FONT-SIZE:14PX;" >package com.bridge;/** * @author Gaoxu * Practice a genuine knowledge! */public class Bmwvehicle extends abstractvehiclebrand{//navigation inavigator navigator = null; @Overridepublic void Install ( Inavigator N) {navigator = n;} @Overridepublic void Open () {navigator.work ();}}

Package com.bridge;/** * @author Gaoxu * Practice the truth! */public class Benzvehicle extends abstractvehiclebrand{//navigation inavigator navigator = null; @Overridepublic void Install ( Inavigator N) {navigator = n;} @Overridepublic void Open () {navigator.work ();}}
the public interface of the navigation plug-in defines the behavior method of navigation.
Package com.bridge;/** * @author Gaoxu * Practice the truth! */public interface Inavigator {public void work ();}
Package Com.bridge;public class Sxznavigator implements inavigator{@Overridepublic void work () {System.out.println (" I am Skywalker Navigator! ");}}

Package Com.bridge;public class Bdnavigator implements inavigator{@Overridepublic void work () {System.out.println (" I am the Beidou navigation! ");}}

Let's look at the client code

Package Com.bridge;public class Client {public static void main (string[] para) {//We are now implementing different brands of cars, can install different brand of navigation, That is, the car and the navigation are aggregated together. We accomplish this by bridging the way, bridging is more flexible than the way it is inherited, it is the car and accessories can be                  independent of their own development. We can achieve the aggregation relationship: BMW + Beidou, BMW + Skywalker, Mercedes + Beidou, Mercedes + physique//Of course we can also give the car to configure more different accessories such as: Air purifier. Inavigator bdnavigator = new Bdnavigator () inavigator sxznavigator = new Sxznavigator ();//BMW Installation Beidou navigation Abstractvehiclebrand BMW = new Bmwvehicle (); Bmw.install (Bdnavigator); Bmw.open ();//Mercedes-Benz installed skywalker navigation Abstractvehiclebrand Benz = new Bmwvehicle (); Benz.install (Sxznavigator); Benz.open ();}}

Pros: It is a model of loose coupling to realize the independent change of two categories with different classifications.

Use range: Bridging mode is easy to implement when multiple categories with different classifications are used together.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Follow the example learning design pattern (9)-Bridging mode Bridge (structural type)

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.