Java的桥接模式

转载自:http://www.importnew.com/6857.html

放在这里,主要是方便记忆。侵权立删。

简单来讲,桥接模式是一个两层的抽象。

桥接模式是用于“把抽象和实现分开,这样它们就能独立变化”。 桥接模式使用了封装、聚合,可以用继承将不同的功能拆分为不同的类。

1、桥接模式的故事

电视和遥控器(图中有错字)是一个完美展示两层抽象的例子。你有一个电视机的接口,还有一个遥控器的抽象类。我们都知道,将它们中任何一个定义为一个具体类都不是好办法,因为其它厂家会有不同的实现方法。

2、桥接模式Java示例代码

首先定义电视机的接口:ITV

1
2
3
4
5
public interface ITV {
     public void on();
     public void off();
     public void switchChannel( int channel);
}

实现三星的 ITV 接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class SamsungTV implements ITV {
     @Override
     public void on() {
         System.out.println( "Samsung is turned on." );
     }
 
     @Override
     public void off() {
         System.out.println( "Samsung is turned off." );
     }
 
     @Override
     public void switchChannel( int channel) {
         System.out.println( "Samsung: channel - " + channel);
     }
}

再实现索尼的ITV接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class SonyTV implements ITV {
 
     @Override
     public void on() {
         System.out.println( "Sony is turned on." );
     }
 
     @Override
     public void off() {
         System.out.println( "Sony is turned off." );
     }
 
     @Override
     public void switchChannel( int channel) {
         System.out.println( "Sony: channel - " + channel);
     }
}

遥控器要包含对TV的引用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public abstract class AbstractRemoteControl {
     /**
      * @uml.property  name="tv"
      * @uml.associationEnd 
      */
     private ITV tv;
 
     public AbstractRemoteControl(ITV tv){
         this .tv = tv;
     }
 
     public void turnOn(){
         tv.on();
     }
 
     public void turnOff(){
         tv.off();
     }
 
     public void setChannel( int channel){
         tv.switchChannel(channel);
     }
}

定义遥控器的具体类。

1
2
3
4
5
6
7
8
9
10
11
public class LogitechRemoteControl extends AbstractRemoteControl {
 
     public LogitechRemoteControl(ITV tv) {
         super (tv);
     }
 
     public void setChannelKeyboard( int channel){
         setChannel(channel);
         System.out.println( "Logitech use keyword to set channel." );
     }
}
1
2
3
4
5
6
7
public class Main {
     public static void main(String[] args){
         ITV tv = new SonyTV();
         LogitechRemoteControl lrc = new LogitechRemoteControl(tv);
         lrc.setChannelKeyboard( 100 );   
     }
}

输出如下:

1
2
Sony: channel – 100
Logitech use keyword to set channel.

总结一下, 桥接模式允许两层实现的抽象,上面的电视机和遥控器就是很好的例子。可见,桥接模式提供了更多的灵活性。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
桥接模式是一种结构型设计模式,它可以将抽象部分与实现部分分离,使它们可以独立地变化。在桥接模式中,抽象部分和实现部分之间通过一个桥接接口相互连接。 下面是一个简单的 Java 桥接模式示例: 首先,我们需要定义一个实现接口(Implementor): ```java public interface Implementor { void operationImpl(); } ``` 然后,我们需要定义一个抽象类(Abstraction),它包含一个实现接口的引用: ```java public abstract class Abstraction { protected Implementor impl; public Abstraction(Implementor impl) { this.impl = impl; } public abstract void operation(); } ``` 接下来,我们需要定义具体的实现类(ConcreteImplementorA 和 ConcreteImplementorB): ```java public class ConcreteImplementorA implements Implementor { @Override public void operationImpl() { System.out.println("ConcreteImplementorA.operationImpl() called."); } } public class ConcreteImplementorB implements Implementor { @Override public void operationImpl() { System.out.println("ConcreteImplementorB.operationImpl() called."); } } ``` 最后,我们定义一个具体的抽象类(RefinedAbstraction),它通过实现抽象类中的 operation() 方法来调用实现接口中的 operationImpl() 方法: ```java public class RefinedAbstraction extends Abstraction { public RefinedAbstraction(Implementor impl) { super(impl); } @Override public void operation() { System.out.println("RefinedAbstraction.operation() called."); impl.operationImpl(); } } ``` 现在我们可以使用桥接模式来创建一个 RefineAbstraction 对象,并将其连接到 ConcreteImplementorA 或 ConcreteImplementorB 对象: ```java public static void main(String[] args) { Implementor implA = new ConcreteImplementorA(); Implementor implB = new ConcreteImplementorB(); Abstraction absA = new RefinedAbstraction(implA); Abstraction absB = new RefinedAbstraction(implB); absA.operation(); absB.operation(); } ``` 输出结果为: ``` RefinedAbstraction.operation() called. ConcreteImplementorA.operationImpl() called. RefinedAbstraction.operation() called. ConcreteImplementorB.operationImpl() called. ``` 这就是一个简单的 Java 桥接模式示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值