文章

23种设计模式----桥接模式----结构型模式

本文深入解析桥接模式,一种用于分离功能与实现的设计模式。通过实例演示如何在不同功能和实现间建立灵活的关联,避免代码冗余,提高系统的可扩展性和可维护性。

23种设计模式----桥接模式----结构型模式

文章信息

  • 原文链接:https://jiayq.blog.csdn.net/article/details/83831800
  • 发布时间:2018-11-07 21:03:26
  • 阅读量:498
  • 分类:java同时被 2 个专栏收录, 订阅专栏, 设计模式
  • 标签:#设计模式, #桥接模式, #结构型模式, #类的功能结构, #类的实现结构

摘要

文章浏览阅读498次。本文深入解析桥接模式,一种用于分离功能与实现的设计模式。通过实例演示如何在不同功能和实现间建立灵活的关联,避免代码冗余,提高系统的可扩展性和可维护性。


桥接模式

  • 1.什么是桥接模式
  • 2.桥接模式的意义
  • 3.角色
  • 4.例子
    • 4.1抽象化类
      • 4.2 改善的抽象化类
      • 4.3实现者
      • 4.4 具体的实现者
      • 4.5 测试
      • 4.6结果
  • 5.扩充
    • 5.1在上述的例子中#换成*(实现结构)
        • 5.1.1新增实现类
          • 5.1.2测试类中新增:
          • 5.1.3测试结果:
          • 5.1.4总结
      • 5.2新增left,right方法(功能)
        • 5.2.1新增方法
          • 5.2.2 新增测试
          • 5.2.3结果
          • 5.2.4总结
      • 5.3扩展

23种设计模式

1.什么是桥接模式

桥接模式就是像桥一样连接类的功能结构和类的实现结构。
新的问题:什么是类的功能结构和类的实现结构。
类的功能结构:
子类在父类的基础上增加新的方法—-类的功能结构
类的实现结构:
子类或者类实现父类的抽象方法(接口)—-类的实现结构

所以,桥接模式就是用来连接这两种结构的。

2.桥接模式的意义

要理解桥接模式的意义,首先需要理解类的功能结构和类的实现结构的意义。

如果写一个子类,那么,需要的功能放在实现里面还是放在扩充的方法里面。
这样的选择就是使用类的功能结构还是类的实现结构。
如果实现结构和功能结构混在一起,那么会非常的混乱;如果分开会缺少联系。

所以,桥接模式就是解决类的功能结构和类的实现结构分离缺少联系的问题。

3.角色

Abstraction抽象化类
Implementor实现者
RefinedAbstraction改善的抽象化
ConcreteImplementor具体的实现者
关系:
抽象化和改善的抽象化是类的功能结构
实现者和具体的实现者是类的实现结构

桥接模式体现在抽象化类和实现者之间。
桥接模式的核心:
调用抽象化类的方法,实际调用的是实现者的抽象方法,真正调用的是具体的实现者的方法。抽象化类中关联一个实现者的对象,但是实现者又是一个抽象类或者接口,就把真正的方法的实现给了具体的实现者。

关联

继承

实现

Abstraction抽象化类

Implementor实现者

RefinedAbstraction改善的抽象化

ConcreteImplementor具体的实现者

4.例子

4.1抽象化类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import cn.com.startimes.bridge.impl.Implementor;

public class Abstraction {

	private Implementor implementor;
	
	public Abstraction(Implementor implementor){
		this.implementor = implementor;
	}
	
	protected void start(){
		implementor.start();
	}
	
	protected void middle(){
		implementor.middle();
	}
	
	protected void end(){
		implementor.end();
	}
	
	public void startToEnd(){
		start();
		middle();
		end();
	}
	
}

4.2 改善的抽象化类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import cn.com.startimes.bridge.impl.Implementor;

public class RefinedAbstration extends Abstraction{

	public RefinedAbstration(Implementor implementor) {
		super(implementor);
	}

	@Override
	public void startToEnd(){
		end();
		middle();
		start();
	}
}

4.3实现者

1
2
3
4
5
6
7
8
9
public abstract class Implementor {

	public abstract void start();
	
	public abstract void middle();
	
	public abstract void end();
	
}

4.4 具体的实现者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ConcreteImplementor extends Implementor{

	@Override
	public void start() {
		System.out.println("ConcreteImplementor#star");
	}

	@Override
	public void middle() {
		System.out.println("ConcreteImplementor#middle");
	}

	@Override
	public void end() {
		System.out.println("ConcreteImplementor#end");
	}

}

4.5 测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import cn.com.startimes.bridge.impl.ConcreteImplementor;
import cn.com.startimes.bridge.structure.Abstraction;
import cn.com.startimes.bridge.structure.RefinedAbstration;

public class Main {

	public static void main(String[] args) {
		System.out.println(1);
		Abstraction abstraction = new Abstraction(new ConcreteImplementor());
		abstraction.startToEnd();
		System.out.println(2);
		Abstraction abstraction2 = new RefinedAbstration(new ConcreteImplementor());
		abstraction2.startToEnd();
		System.out.println(3);
		RefinedAbstration refinedAbstration = new RefinedAbstration(new ConcreteImplementor());
		refinedAbstration.startToEnd();
	}

}

4.6结果

1
2
3
4
5
6
7
8
9
10
11
12
1
ConcreteImplementor#star
ConcreteImplementor#middle
ConcreteImplementor#end
2
ConcreteImplementor#end
ConcreteImplementor#middle
ConcreteImplementor#star
3
ConcreteImplementor#end
ConcreteImplementor#middle
ConcreteImplementor#star

5.扩充

5.1在上述的例子中#换成*(实现结构)

5.1.1新增实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class StarConcreteImplementor extends Implementor{

	@Override
	public void start() {
		System.out.println("StarConcreteImplementor#star");
	}

	@Override
	public void middle() {
		System.out.println("StarConcreteImplementor#middle");
	}

	@Override
	public void end() {
		System.out.println("StarConcreteImplementor#end");
	}
	
}

5.1.2测试类中新增:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import cn.com.startimes.bridge.impl.ConcreteImplementor;
import cn.com.startimes.bridge.impl.StarConcreteImplementor;
import cn.com.startimes.bridge.structure.Abstraction;
import cn.com.startimes.bridge.structure.RefinedAbstration;

public class Main {

	public static void main(String[] args) {
		System.out.println(1);
		Abstraction abstraction = new Abstraction(new ConcreteImplementor());
		abstraction.startToEnd();
		System.out.println(2);
		Abstraction abstraction2 = new RefinedAbstration(new ConcreteImplementor());
		abstraction2.startToEnd();
		System.out.println(3);
		RefinedAbstration refinedAbstration = new RefinedAbstration(new ConcreteImplementor());
		refinedAbstration.startToEnd();
		
		System.out.println();
		System.out.println();
		System.out.println(4);
		Abstraction abstraction3 = new Abstraction(new StarConcreteImplementor());
		abstraction3.startToEnd();
		System.out.println(5);
		Abstraction abstraction4 = new RefinedAbstration(new StarConcreteImplementor());
		abstraction4.startToEnd();
		System.out.println(6);
		RefinedAbstration refinedAbstration2 = new RefinedAbstration(new StarConcreteImplementor());
		refinedAbstration2.startToEnd();
	}

}

5.1.3测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
ConcreteImplementor#star
ConcreteImplementor#middle
ConcreteImplementor#end
2
ConcreteImplementor#end
ConcreteImplementor#middle
ConcreteImplementor#star
3
ConcreteImplementor#end
ConcreteImplementor#middle
ConcreteImplementor#star


4
StarConcreteImplementor#star
StarConcreteImplementor#middle
StarConcreteImplementor#end
5
StarConcreteImplementor#end
StarConcreteImplementor#middle
StarConcreteImplementor#star
6
StarConcreteImplementor#end
StarConcreteImplementor#middle
StarConcreteImplementor#star

5.1.4总结

增加不同的实现,不需要修改功能这边的类,只需要增加实现那边的类和测试的类(使用类)

5.2新增left,right方法(功能)

5.2.1新增方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import cn.com.startimes.bridge.impl.Implementor;

public class LeftRightAbstraction extends Abstraction{

	public LeftRightAbstraction(Implementor implementor) {
		super(implementor);
	}
	
	public void left(){
		System.out.println("LeftRightAbstraction#left");
	}
	
	public void right(){
		System.out.println("LeftRightAbstraction#right");
	}
	
	@Override
	public void startToEnd(){
		start();
		middle();
		end();
		left();
		right();
	}

}

5.2.2 新增测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import cn.com.startimes.bridge.impl.ConcreteImplementor;
import cn.com.startimes.bridge.impl.StarConcreteImplementor;
import cn.com.startimes.bridge.structure.Abstraction;
import cn.com.startimes.bridge.structure.LeftRightAbstraction;
import cn.com.startimes.bridge.structure.RefinedAbstration;

public class Main {

	public static void main(String[] args) {
		System.out.println(1);
		Abstraction abstraction = new Abstraction(new ConcreteImplementor());
		abstraction.startToEnd();
		System.out.println(2);
		Abstraction abstraction2 = new RefinedAbstration(new ConcreteImplementor());
		abstraction2.startToEnd();
		System.out.println(3);
		RefinedAbstration refinedAbstration = new RefinedAbstration(new ConcreteImplementor());
		refinedAbstration.startToEnd();
		
		System.out.println();
		System.out.println();
		System.out.println(4);
		Abstraction abstraction3 = new Abstraction(new StarConcreteImplementor());
		abstraction3.startToEnd();
		System.out.println(5);
		Abstraction abstraction4 = new RefinedAbstration(new StarConcreteImplementor());
		abstraction4.startToEnd();
		System.out.println(6);
		RefinedAbstration refinedAbstration2 = new RefinedAbstration(new StarConcreteImplementor());
		refinedAbstration2.startToEnd();
		
		System.out.println();
		System.out.println();
		System.out.println(7);
		Abstraction abstraction5 = new Abstraction(new ConcreteImplementor());
		abstraction5.startToEnd();
		System.out.println(8);
		Abstraction abstraction6 = new Abstraction(new StarConcreteImplementor());
		abstraction6.startToEnd();
		System.out.println(9);
		RefinedAbstration refinedAbstration3 = new RefinedAbstration(new ConcreteImplementor());
		refinedAbstration3.startToEnd();
		System.out.println(10);
		RefinedAbstration refinedAbstration4 = new RefinedAbstration(new StarConcreteImplementor());
		refinedAbstration4.startToEnd();
		System.out.println(11);
		LeftRightAbstraction leftRightAbstraction = new LeftRightAbstraction(new ConcreteImplementor());
		leftRightAbstraction.startToEnd();
		System.out.println(12);
		LeftRightAbstraction leftRightAbstraction2 = new LeftRightAbstraction(new StarConcreteImplementor());
		leftRightAbstraction2.startToEnd();
		
	}

}

5.2.3结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
1
ConcreteImplementor#star
ConcreteImplementor#middle
ConcreteImplementor#end
2
ConcreteImplementor#end
ConcreteImplementor#middle
ConcreteImplementor#star
3
ConcreteImplementor#end
ConcreteImplementor#middle
ConcreteImplementor#star


4
StarConcreteImplementor#star
StarConcreteImplementor#middle
StarConcreteImplementor#end
5
StarConcreteImplementor#end
StarConcreteImplementor#middle
StarConcreteImplementor#star
6
StarConcreteImplementor#end
StarConcreteImplementor#middle
StarConcreteImplementor#star


7
ConcreteImplementor#star
ConcreteImplementor#middle
ConcreteImplementor#end
8
StarConcreteImplementor#star
StarConcreteImplementor#middle
StarConcreteImplementor#end
9
ConcreteImplementor#end
ConcreteImplementor#middle
ConcreteImplementor#star
10
StarConcreteImplementor#end
StarConcreteImplementor#middle
StarConcreteImplementor#star
11
ConcreteImplementor#star
ConcreteImplementor#middle
ConcreteImplementor#end
LeftRightAbstraction#left
LeftRightAbstraction#right
12
StarConcreteImplementor#star
StarConcreteImplementor#middle
StarConcreteImplementor#end
LeftRightAbstraction#left
LeftRightAbstraction#right

5.2.4总结

增加不同的功能,实现那边不需要做任何修改就能够使用,只需要修改功能这边的类和测试的类。

5.3扩展

增加功能,具体的功能实现还是在功能这一侧,没有在实现那一边做。
不过可以考虑在功能一侧再次使用桥接模式,把功能这一侧分为功能的抽象实现和功能定义。
同样的可以考虑在实现的一侧再次使用桥接模式,把实现这一侧分为实现的抽象和实现的实现。

关联

关联

实现

实现

关联

继承

继承

AbsAbs

ImplAbs

Impl

Star

Con

Abs

Re

Left

在这里插入图片描述
上图中Abstraction.java可删除
ImplAbs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class ImplAbs {

	private Implementor implementor;
	
	public ImplAbs(Implementor implementor){
		this.implementor = implementor;
	}
	
	public void start(){
		implementor.start();
	}
	
	public void middle(){
		implementor.middle();
	}
	
	public void end(){
		implementor.end();
	}
	
}

Abs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import cn.com.startimes.bridge.impl.implAbs.ImplAbs;

public class Abs {

	private ImplAbs implAbs;
	
	public Abs(ImplAbs implAbs){
		this.implAbs = implAbs;
	}
	
	protected void start(){
		implAbs.start();
	}
	
	protected void middle(){
		implAbs.middle();
	}
	
	protected void end(){
		implAbs.end();
	}
	
	public void startToEnd(){
		start();
		middle();
		end();
	}
	
}

AbsAbs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import cn.com.startimes.bridge.impl.implAbs.ImplAbs;

public class AbsAbs {

	private ImplAbs implAbs;
	
	private Abs abs;
	
	public AbsAbs(ImplAbs implAbs){
		this.implAbs = implAbs;
		this.abs = new Abs(implAbs);
	}
	
	public void start(){
		implAbs.start();
	}
	
	public void middle(){
		implAbs.middle();
	}
	
	public void end(){
		implAbs.end();
	}
	
	public void startToEnd(){
		abs.startToEnd();
	}
}

main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import cn.com.startimes.bridge.impl.implAbs.ImplAbs;
import cn.com.startimes.bridge.impl.implimpl.ConcreteImplementor;
import cn.com.startimes.bridge.impl.implimpl.StarConcreteImplementor;
import cn.com.startimes.bridge.structure.absabs.AbsAbs;
import cn.com.startimes.bridge.structure.absabs.Abstraction;
import cn.com.startimes.bridge.structure.absimpl.LeftRightAbstraction;
import cn.com.startimes.bridge.structure.absimpl.RefinedAbstration;

public class Main {

	public static void main(String[] args) {
		System.out.println(1);
		Abstraction abstraction = new Abstraction(new ConcreteImplementor());
		abstraction.startToEnd();
		System.out.println(2);
		Abstraction abstraction2 = new RefinedAbstration(new ConcreteImplementor());
		abstraction2.startToEnd();
		System.out.println(3);
		RefinedAbstration refinedAbstration = new RefinedAbstration(new ConcreteImplementor());
		refinedAbstration.startToEnd();
		
		System.out.println();
		System.out.println();
		System.out.println(4);
		Abstraction abstraction3 = new Abstraction(new StarConcreteImplementor());
		abstraction3.startToEnd();
		System.out.println(5);
		Abstraction abstraction4 = new RefinedAbstration(new StarConcreteImplementor());
		abstraction4.startToEnd();
		System.out.println(6);
		RefinedAbstration refinedAbstration2 = new RefinedAbstration(new StarConcreteImplementor());
		refinedAbstration2.startToEnd();
		
		System.out.println();
		System.out.println();
		System.out.println(7);
		Abstraction abstraction5 = new Abstraction(new ConcreteImplementor());
		abstraction5.startToEnd();
		System.out.println(8);
		Abstraction abstraction6 = new Abstraction(new StarConcreteImplementor());
		abstraction6.startToEnd();
		System.out.println(9);
		RefinedAbstration refinedAbstration3 = new RefinedAbstration(new ConcreteImplementor());
		refinedAbstration3.startToEnd();
		System.out.println(10);
		RefinedAbstration refinedAbstration4 = new RefinedAbstration(new StarConcreteImplementor());
		refinedAbstration4.startToEnd();
		System.out.println(11);
		LeftRightAbstraction leftRightAbstraction = new LeftRightAbstraction(new ConcreteImplementor());
		leftRightAbstraction.startToEnd();
		System.out.println(12);
		LeftRightAbstraction leftRightAbstraction2 = new LeftRightAbstraction(new StarConcreteImplementor());
		leftRightAbstraction2.startToEnd();
		
		
		System.out.println();
		System.out.println();
		System.out.println(13);
		AbsAbs absAbs = new AbsAbs(new ImplAbs(new StarConcreteImplementor()));
		absAbs.startToEnd();
	}

}

不过,这样做除了有规律,在没有任何好处,有点过度设计。
但是当需要类似实现的类相当的多时候,可以考虑如此实现。

23种设计模式

本文由作者按照 CC BY 4.0 进行授权