JAVA Transformation (seven) bridge design patterns Design Patterns

Copyright Notice: Copyright https://blog.csdn.net/qq_21046965/article/details/91629640 procedures monkey jwang

Foreword

      This chapter explains the bridging mode of knowledge

method

1. Concept

Before explaining bridge mode, we first look at a common example of life:

Bridge type, divided according to use, there are railway bridge , a road bridge , railway bridge, pedestrian bridge, the bridge carrying water (aqueduct) and other special bridges (e.g., through a conduit, cable, etc.). Divided by barriers to cross, there are bridges over rivers, and valleys bridge, flyover (overpass also known), viaduct, trestle.

If the Java classes in how to use the representation of it?

We can design a bridge class, bridge class below the railway bridge , road bridge , footbridge three sub-categories, including the railway bridge following a river-crossing the railway bridge across the river road bridge , across the river footbridge three subclasses, road bridge and pedestrian similar bridges have three subclasses, of course, easily expressed.

However, if the above is designed to increase the use of a bridge across the barriers of class or increase, then certainly we need to add three subclasses again, scalability poor!

In order to solve the problem of class explosion, Java provides a solution to bridge mode.

Realization of ideas 2. bridge mode

1) First, the establishment of appropriate project

2) write an abstract class bridge

Bridge is an abstract class where the bridge, the bridge according to zoning three classes inherit the abstract class, as follows:

package cn.edu.ccut;

/**
 * 桥梁抽象类,按照用途分
 * @author jwang
 *
 */
public abstract class Bridge {
	
	protected Obstacle obstacle;
	
	public Bridge(Obstacle obstacle) {
		obstacle.say();
		this.obstacle = obstacle;
	}
	
	public abstract void say();
}

/**
 * 铁路大桥
 * @author jwang
 *
 */
class TL extends Bridge{

	public TL(Obstacle obstacle) {
		super(obstacle);
	}

	@Override
	public void say() {
		System.out.println("铁路大桥");
	}
	
}
/**
 * 公路大桥
 * @author jwang
 *
 */
class GL extends Bridge{
	
	public GL(Obstacle obstacle) {
		super(obstacle);
	}
	
	@Override
	public void say() {
		System.out.println("公路大桥");
	}
	
}

/**
 * 人行大桥
 * @author jwang
 *
 */
class RX extends Bridge{
	
	public RX(Obstacle obstacle) {
		super(obstacle);
	}
	
	@Override
	public void say() {
		System.out.println("人行大桥");
	}
	
}

3) written across the barriers class interface

 Obstacle is an interface used to represent the class interface across the barrier, across the barrier according to different subclasses of the three implements this interface.

package cn.edu.ccut;

/**
 * 跨越障碍接口,按跨越障碍分
 * @author jwang
 *
 */
public interface Obstacle {
	
	void say();
}

/**
 * 跨河大桥
 * @author jwang
 *
 */
class KH implements Obstacle{

	@Override
	public void say() {
		System.out.println("跨河大桥");
	}
	
}
/**
 * 跨谷大桥
 * @author jwang
 *
 */
class KG implements Obstacle{
	
	@Override
	public void say() {
		System.out.println("跨谷大桥");
	}
	
}
/**
 * 跨线大桥
 * @author jwang
 *
 */
class KX implements Obstacle{
	
	@Override
	public void say() {
		System.out.println("跨线大桥");
	}
	
}

4) preparation of test class test

package cn.edu.ccut;

public class Test {

	public static void main(String[] args) {
		//跨河铁路桥
		Bridge bridge = new TL(new KH());
		bridge.say();
	}

}

Thus, when we need cross-sea railway bridge, just use a combination of the way instead of the previous traditional class inheritance hierarchy.

Results are as follows:

Since there is no multiple inheritance in Java, it is forced to use this mode to alleviate the problems the project categories explosion.

In this case, after the new or increased use of the Bridge of the category that transcends barriers, it is easy to solve, no longer need cumbersome inherited. 

Guess you like

Origin blog.csdn.net/qq_21046965/article/details/91629640