Preliminary Exploration of Design Pattern-Bridge pattern

Source: Internet
Author: User

The Bridge mode, also known as the Handle/Body mode, belongs to the object structure mode. Used to separate abstract parts from their implementations so that they can all change independently. For example, for a common computer window interface, the drawing principle of the window interface of different operating systems is certainly different. Here, the window interface is equivalent to the abstract action, and the window interface itself can be expanded with different functions. The expanded window is the abstract class of the abstract window (refined1_action ), these abstractions require specific operating system implementation before we can see them. The Window implementation based on the operating system is the implementation part (Implementor), whether it is windows, Linux or Mac. Windows, Linux, or Mac is the specific implementation class of the implementation interface (ConcreteImplementor ).

I. Application scenarios

1. If a system requires more flexibility between abstraction and specifics, avoid establishing static inheritance relationships between the two layers, the bridge mode enables them to establish an association on the abstraction layer to increase flexibility.
2. "abstract Part" and "implementation part" can be expanded independently in an inherited manner without affecting each other, when running the program, you can dynamically combine an abstract subclass object and an object that implements the subclass. That is, the system needs to dynamically couple the abstract role and the actual role.
3. A class has two (or more) Independent dimensions, and both (or more) dimensions need to be expanded independently.
4. The bridge mode is particularly applicable to systems that do not want to use inheritance or the number of System Classes increases sharply due to multi-level inheritance.

Ii. UML diagram


Iii. Java implementation

Package study. patterns. bridge;/*** bridge Mode: This mode decouples dimensions to handle multi-dimensional changes. * @ Author qbg */public class BridgePattern {public static void main (String [] args) {/*** for clients, you can program at the abstraction layer of two dimensions, when the program runs, it dynamically determines the child classes of the two dimensions. * It dynamically combines the objects and completely decouples the two independently changed dimensions, this allows you to flexibly expand any dimension without affecting the other dimension. */Brush brush = new LargeImpl (); BrushImpl impl = new RedImpl (); brush. setBrushImpl (impl); brush. drawPicture ("Mona Lisa Smile"); }}/ *** auxiliary class: Drawing */class Picture {}/ *** abstract base class of the paint brush: There are types of changed dimensions, model, color, etc. * here, take the model (big, medium, small) and color (red, green, blue) as the sample dimension. * The model is an abstract dimension, and the color is an implemented dimension. * To Add a new paint Brush model, you only need to expand the abstract class Brush. * to add a new paint color, you only need to add the specific implementation class of the BrushImpl interface. */abstract class Brush {protected BrushImpl impl; public void setBrushImpl (BrushImpl impl) {this. impl = impl ;}/*** Business Method of the abstract Part: Drawing */public abstract void drawPicture (String name);}/*** abstract pen color implementation class: implementation class interface */interface BrushImpl {public void doDraw (Picture pic); // apply the corresponding color}/*** red paint implementation class: specific implementation class */class RedImpl implements BrushImpl {@ Overridepublic void doDraw (Picture pic) {System. out. println ("smear red .... ") ;}}/**** Green paint implementation class: Implementation class */class GreenImpl implements BrushImpl {@ Overridepublic void doDraw (Picture pic) {System. out. println ("smear green .... ") ;}}/*** Blue paint brush implementation class: Implementation class */class BlueImpl implements BrushImpl {@ Overridepublic void doDraw (Picture pic) {System. out. println ("smear blue .... ") ;}}/*** Large paint Brush: Expanded abstract class */class LargeImpl extends Brush {@ Overridepublic void drawPicture (String name) {// simulate the Picture pic = new Picture (); impl. doDraw (pic); System. out. println (name + ", draw with a large paint brush... ") ;}}/*** Medium paint Brush: extends the abstract class */class MediumImpl extends Brush {@ Overridepublic void drawPicture (String name) {// simulate the Picture pic = new Picture (); impl. doDraw (pic); System. out. println (name + ", draw with a medium paint brush... ") ;}}/*** Small paint Brush: extends the abstract class */class TinyImpl extends Brush {@ Overridepublic void drawPicture (String name) {// simulate the Picture pic = new Picture (); impl. doDraw (pic); System. out. println (name + ", draw with a small paint brush... ");}}

Iv. Advantages and Disadvantages

Advantages:

1. Separate abstract interfaces and their implementations. The Bridge Mode decouples the inherent binding relationship between abstraction and implementation using the "association relationship between objects", so that abstraction and implementation can change along their respective dimensions. The so-called abstraction and implementation Change along their respective dimensions. That is to say, abstraction and implementation are no longer in the same inheritance hierarchy, but are "subclass", so that they have their own subclasses, so that any combination subclass can obtain multi-dimensional combination objects.
2. In many cases, the bridge mode can replace the multi-layer inheritance scheme. The multi-layer inheritance scheme violates the "single Responsibility Principle" and has poor reusability and a large number of classes, the bridge mode is a better solution than the multi-layer inheritance solution, which greatly reduces the number of sub-classes.
3. The Bridge Mode improves the scalability of the system. You do not need to modify the original system if you expand one of the two dimensions, which complies with the "Open and Close principle ".

Disadvantages:

1. The use of the Bridge Mode increases the understanding and design difficulty of the system. Since the association is established on the abstraction layer, developers are required to design and program the abstraction layer from the very beginning.
2. The bridge mode requires correct identification of two independent dimensions in the system. Therefore, the scope of use has certain limitations. It also requires some experience to identify the two independent dimensions correctly.

PS: The above summary is based on livelion. The experience is not good. You can only rely on the gods!

Related Article

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.