Latest web development tutorials

Bridge Mode

Bridge (Bridge) is used to achieve decoupling and abstraction, so that the two can vary independently. This type of design patterns belong structural model by providing abstraction structured and bridging between the two to achieve decoupling.

This model involves as a bridging interface that allows an entity independent of the type of function interface class. These two types of classes can be structured to change and affect each other.

We by the following examples to demonstrate the bridge mode (Bridge Pattern) usage. Which you can use the same method but different abstract classes bridging implementation class to draw circles of different colors.

Introduction

Intent: an abstraction and implementation of part of the separation, so that they can vary independently.

Main Solution: In a variety of circumstances may change, class inheritance can cause explosion, it is not flexible expansion.

When to use: Implementing classification system may have multiple angles, each angle may change.

How to fix: put this multi-angle classification separated, so they are independent of changes, reduce the coupling between them.

The key code: abstract class implementation dependent class.

Application examples: 1, reborn from pig to pig Marshal canopy reincarnation, reincarnation of the earthly mechanism is divided into two levels, namely: the soul and body, the former corresponds to abstraction, which is equivalent to the realizable.Creatures by delegating functions, function call physical objects, such creatures can be dynamically selected. 2, switch on the wall, you can see the switch is abstract, not concrete pipe inside how to achieve.

Advantages: 1, abstract and achieve separation.2, excellent scalability. 3, implementation details transparent to the client.

Disadvantages: the introduction of bridge mode will increase the difficulty of understanding and design of the system, the polymerization relationship built on abstraction layer, requiring the developer for abstract design and programming.

Usage scenarios: 1, if a system is required between abstraction and concrete role member of the role to add more flexibility to avoid creating static inheritance link between the two levels, through the bridge mode so that they Abstraction Layer establish a relationship.2, for those who do not want to use the system as a multi-level inheritance or inheritance led to a sharp increase in the number of system classes, bridge mode is particularly applicable. 3, a class there are two independent dimensions of change, and these two dimensions need to be expanded.

Note: For achange of two independent dimensions, using the bridge mode easy to do.

achieve

We have as aDrawAPIinterface bridge and implementedDrawAPIinterface entity classRedCircle,GreenCircle.Shapeis an abstract class, the useDrawAPIobject.BridgePatternDemo,our demonstration classes usethe Shapeclass to draw circles of different colors.

Bridge mode UML diagram

step 1

Create a bridge that implements the interface.

DrawAPI.java

public interface DrawAPI {
   public void drawCircle (int radius, int x, int y);
}

Step 2

Create an implementation entity bridgingDrawAPIimplementation of the interface class.

RedCircle.java

public class RedCircle implements DrawAPI {
   @Override
   public void drawCircle (int radius, int x, int y) {
      System.out.println ( "Drawing Circle [color: red, radius:"
         + Radius + ", x:" + x + "," + y + "]");
   }
}

GreenCircle.java

public class GreenCircle implements DrawAPI {
   @Override
   public void drawCircle (int radius, int x, int y) {
      System.out.println ( "Drawing Circle [color: green, radius:"
         + Radius + ", x:" + x + "," + y + "]");
   }
}

Step 3

DrawAPIuse interface to create an abstract classShape.

Shape.java

public abstract class Shape {
   protected DrawAPI drawAPI;
   protected Shape (DrawAPI drawAPI) {
      this.drawAPI = drawAPI;
   }
   public abstract void draw ();	
}

Step 4

Create entity class implementsthe Shapeinterface.

Circle.java

public class Circle extends Shape {
   private int x, y, radius;

   public Circle (int x, int y, int radius, DrawAPI drawAPI) {
      super (drawAPI);
      this.x = x;  
      this.y = y;  
      this.radius = radius;
   }

   public void draw () {
      drawAPI.drawCircle (radius, x, y);
   }
}

Step 5

Using theShapeclass andDrawAPIdraw circles of different colors.

BridgePatternDemo.java

public class BridgePatternDemo {
   public static void main (String [] args) {
      Shape redCircle = new Circle (100,100, 10, new RedCircle ());
      Shape greenCircle = new Circle (100,100, 10, new GreenCircle ());

      redCircle.draw ();
      greenCircle.draw ();
   }
}

Step 6

Verify output.

Drawing Circle [color: red, radius: 10, x: 100, 100]
Drawing Circle [color: green, radius: 10, x: 100, 100]