桥接模式(结构型)

桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。

设想如果要绘制矩形、圆形、椭圆、正方形,我们至少需要4个形状类,但是如果绘制的图形需要具有不同的颜色,如红色、绿色、蓝色等,此时至少有如下两种设计方案:

        • 第一种设计方案是为每一种形状都提供一套各种颜色的版本。

        • 第二种设计方案是根据实际需要对形状和颜色进行组合。

 

    对于有两个变化维度(即两个变化的原因)的系统,采用方案二来进行设计系统中类的个数更少,且系统扩展更为方便。设计方案二即是桥接模式的应用。桥接模式将继承关系转换为关联关系,从而降低了类与类之间的耦合,减少了代码编写量。

 


 例子,我们现在要开发一款电视,它有三个品牌,每个品牌又有2个遥控器配备,下面有两种方案设计类图

 

如上图分析,第一种设计需要创建6个最终类,每个类都有电视和遥控的功能,第二个设计将遥控器和电视分离,它们有各自的实现类,并通过组合,来形成不同的电视 ,它只用创建5各类,而且电视机与遥控器实现了解耦

下面开始写代码

第一步 创建电视和遥控器的接口

package bridge.demo.tv;
 
/**
 * 电视
 */
public abstract class TV {
 
	public abstract void on();
 
	public abstract void off();
 
	public abstract void tuneChannel(int channel);
 
	public abstract int getChannel();
}
package bridge.demo.control;


import bridge.demo.tv.TV;

/**
 * 遥控器
 */
public abstract class RemoteControl {
	TV tv;
 
	public void on() {
		this.tv.on();
	}
 
	public void off() {
		this.tv.off();
	}
 
	/**
	 * 设置频道
	 * @param channel
	 */
	public void setChannel(int channel) {
		tv.tuneChannel(channel);
	}
 
	public int getChannel() {
		return tv.getChannel();
	}
 
	public void setTV(TV tv) {
	     this.tv = tv;
	}
}

第二部,创建它们各自的实现类

  电视实现类

package bridge.demo.tv;
 
/**
 * Sony电视
 */
public class Sony extends TV {
 
	int station = 0;
 
	public void on() {
		System.out.println("Turning on the Sony TV");
	}
 
	public void off() {
		System.out.println("Turning off the Sony TV");
	}
 
	public void tuneChannel(int channel) {
		this.station = channel;
		System.out.println("Set the Sony TV station to " + this.station);
	}
 
	public int getChannel() {
		return station;
	}
}
 
 
 
package bridge.demo.tv;

/**
 * LG电视
 */
public class LG extends TV {
 
	int channel = 1;
	public void on() {
		System.out.println("Turning on the LG TV");
	}
	public void off() {
		System.out.println("Turning off the LG TV");
	}
 
 
	public void tuneChannel(int channel) {
		this.channel = channel;
		System.out.println("Set the LG TV Channel to " + this.channel);
	}
 
	public int getChannel() {
		return channel;
	}
}

   遥控器实现类

package bridge.demo.control;
 
/**
 * 普通遥控器
 */
public class GenericRemote extends RemoteControl {
 
 
	public void nextChannel() {
		int channel = this.getChannel();
		this.setChannel(channel+1);
	}
 
	public void prevChannel() {
		int channel = this.getChannel();
		this.setChannel(channel-1);
	}
}
 
package bridge.demo.control;
 
/**
 * 特别遥控器
 */
public class SpecialRemote extends RemoteControl {
 
 
	public void up() {
		int channel = this.getChannel();
		this.setChannel(channel+1);
	}
 
	public void down() {
		int channel = this.getChannel();
		this.setChannel(channel-1);
	}
}

第三步,测试功能

package bridge.demo;


import bridge.demo.control.GenericRemote;
import bridge.demo.control.SpecialRemote;
import bridge.demo.tv.LG;
import bridge.demo.tv.Sony;

public class Client {
	public static void main(String[] args) {
 
		SpecialRemote remoteSony = new SpecialRemote();
		remoteSony.setTV(new Sony());
		System.out.println("Connect your remote to the TV");
		remoteSony.on();
		remoteSony.up();
		remoteSony.down();
		remoteSony.off();

		System.out.println();

		GenericRemote remoteLG = new GenericRemote();
		remoteLG.setTV(new LG());
		System.out.println("Connect your remote to the TV");
		remoteLG.on();
		remoteLG.nextChannel();
		remoteLG.prevChannel();
		remoteLG.off();
	}
}
Connect your remote to the TV
Turning on the Sony TV
Set the Sony TV station to 1
Set the Sony TV station to 0
Turning off the Sony TV

Connect your remote to the TV
Turning on the LG TV
Set the LG TV Channel to 2
Set the LG TV Channel to 1
Turning off the LG TV
发布了138 篇原创文章 · 获赞 34 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/bbj12345678/article/details/105327235
今日推荐