Design Patterns Learning Notes (16: Bridging mode)

Source: Internet
Author: User

1.1 Overview

separate the abstract part from its implementation so that they can all change independently. This is the definition of bridging mode.

Abstract classes or interfaces can define several abstract methods, which are used to refer to abstract methods as operations. Abstract classes or interfaces let the designer of the program ignore the details of the operation, that is, do not have to consider how these operations are implemented, when the user program for the abstract class or interface, it will not rely on the specific implementation, so that the system has a good extensibility. However, abstract methods in the abstract class are always required to implement the subclass, in most cases the subclass of the abstract class is fully capable of such work, but in some cases, subclasses may encounter some difficult problems.

For example, there is an abstract class in the TV systemCCTV, the class has an abstract methodabstract void Makeprogram (). Now in order to meet the needs of some users to watch TV, here is aCCTVSubclass of class:CCTV8, an instance of the class callsMakeprogram ()method to make TV shows, so the subclassCCTV8the parent class must be implementedMakeprogram ()methods, such as using this method to make a number of frame images. Sub-classCCTV8of theMakeprogram ()method After making the first frame image, for example, in the first frame image display "CCTV8, the following two questions are immediately found:

(1) from the beginning of frame 2 should be the image of the TV series, and such images should not be produced by the CCTV8 class of Makeprogram () method.

(2) if cctv8 In makeprogram () 2 After the frames of each image, then the user uses the CCTV8 CCTV sub-class, which is a very unreasonable design for the TV system, because CCTV cctv8 instead of multiple , that is, not because a new user to see different TV dramas, there will be a special for the user to create a "TV show" sub-category.

To address the above problems, the implementation and abstraction should be placed in two different class hierarchies, so that they can be independent of the change, an abstract class in an abstract method of the important implementation of the other class to the subclass of another abstract class or implement another interface. For example, for the above problem, the implementation of the Makeprogram () method should be given to another abstract class:Program, which defines the maketvfilm () thatmakes the image . method.

we should redesign the abstract classCCTVclass so that the class contains Programthe reference, which can makeCCTVSubclass of ClassCCTV8in implementingMakeprogram ()method, the important implementation part of the method is given to the ProgramClass ofmaketvfilm ()method, which is delegated to Programinstance invocation of a subclassmaketvfilm ()method.

We call The relationship between the CCTV class and The program class is bridging, whichmeans that the CCTV8 of the CCTV class is only CCTV Classes and Program a "bridging" function between classes, as shown in the specific class relationship:

Figure one: Bridging relationship between TV program and TV series production

Structure of the 1.2 pattern

Bridging mode consists of the following four roles:

(1) abstract (abstration): An abstract class that contains variables declared by implementor that maintain a implementor The type object.

(2) the implementor: Theimplementation role is an interface (abstract class), and the methods in that interface (abstract class) are not necessarily consistent with the methods in the abstration class. the implementor Interface (abstract class) is responsible for defining basic operations, while the abstration class is responsible for defining higher-level operations based on these basic operations.

(3) refinement abstraction (refined abstration): Refinement abstraction is a subclass of an abstract role that, when overridden (overriding) an abstract method in an abstract role, after giving some necessary actions, the delegate maintains The implementor type Object calls the appropriate method.

(4) Concrete Implementation (concrete implementor): The specific implementation is the implementation (extension)implementor Interface (abstract Class) class.

The class diagram for the bridging mode structure is shown in two:

Figure two: Class diagram of bridge mode

1.3 Bridge Connection advantages of the model

(1) Bridging mode separation implementation and abstraction, so that the abstraction and implementation can be independent extension. When you modify the implemented code, it does not affect the abstract code, and vice versa.

(2) to meet the open- close principle. Abstract and the implementation of the same level, so that the system can be independent of the expansion of two levels. Add new concrete implementation, do not need to modify the refinement object, conversely, add new refinement object does not need to modify the concrete implementation.

1.4 Fit using bridging pattern of the scene

(1) do not want to let the abstraction and some important implementation code is a fixed binding relationship, this part of the implementation can be run-time dynamic decision.

(2) The methods of abstraction and implementation can be inherited independently of each other, and the program may need to dynamically combine an instance of an abstract subclass with an instance of a subclass of an implementation during runtime.

(3) The desire to modify the code at the level of the implementation has no effect on the abstraction layer, that is, the code of the abstraction layer does not have to be recompiled, and vice versa.

Design Patterns Learning Notes (16: 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.