File tree Expand file tree Collapse file tree 13 files changed +263
-0
lines changed
DesignPattern/src/main/java/com/design Expand file tree Collapse file tree 13 files changed +263
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .design .bridge ;
2
+
3
+ /**
4
+ * 黑色
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/2 17:05
8
+ */
9
+ public class BlackColorImpl implements Color {
10
+ @ Override
11
+ public void bepaint (String shape ) {
12
+ System .out .println ("黑色的" + shape );
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .bridge ;
2
+
3
+ /**
4
+ * 圆形
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/2 17:05
8
+ */
9
+ public class CircleShapeImpl extends Shape {
10
+ @ Override
11
+ public void draw () {
12
+ color .bepaint ("圆形" );
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .bridge ;
2
+
3
+ /**
4
+ * 颜色接口
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/2 17:05
8
+ */
9
+ public interface Color {
10
+
11
+ void bepaint (String shape );
12
+
13
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .bridge ;
2
+
3
+ /**
4
+ * 灰色
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/2 17:05
8
+ */
9
+ public class GrayColorImpl implements Color {
10
+ @ Override
11
+ public void bepaint (String shape ) {
12
+ System .out .println ("灰色的" + shape );
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .bridge ;
2
+
3
+ /**
4
+ * 桥接模式 - 在有多种可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活
5
+ * 使用一个画不同颜色的形状的小例子说明,比如可以画正方形、长方形、圆形
6
+ * 但是现在我们需要给这些形状进行上色,这里有三种颜色:白色、灰色、黑色
7
+ * 这里我们可以画出 3 * 3 = 9 种图形,一般直接的做法是使用继承,
8
+ * 为每种形状都提供各种颜色的版本,这样的做法不灵活,
9
+ * 会导致实现类几何增长,所以引出了桥接模式,根据实际需要对颜色和形状进行组合
10
+ *
11
+ * @author wliduo[i@dolyw.com]
12
+ * @date 2023/2/2 17:05
13
+ */
14
+ public class Main {
15
+
16
+ public static void main (String [] args ) {
17
+ // 颜色
18
+ Color whiteColor = new WhiteColorImpl ();
19
+ Color blackColor = new BlackColorImpl ();
20
+ Color grayColor = new GrayColorImpl ();
21
+ // 形状
22
+ Shape square = new SquareShapeImpl ();
23
+ Shape rectangleShape = new RectangleShapeImpl ();
24
+ Shape circleShape = new CircleShapeImpl ();
25
+ // 白色的正方形
26
+ square .setColor (whiteColor );
27
+ square .draw ();
28
+ // 灰色的正方形
29
+ square .setColor (grayColor );
30
+ square .draw ();
31
+ // 黑色的长方形
32
+ rectangleShape .setColor (blackColor );
33
+ rectangleShape .draw ();
34
+ // 灰色的圆形
35
+ circleShape .setColor (grayColor );
36
+ circleShape .draw ();
37
+ // 白色的圆形
38
+ circleShape .setColor (whiteColor );
39
+ circleShape .draw ();
40
+ }
41
+
42
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .bridge ;
2
+
3
+ /**
4
+ * 长方形
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/2 17:05
8
+ */
9
+ public class RectangleShapeImpl extends Shape {
10
+ @ Override
11
+ public void draw () {
12
+ color .bepaint ("长方形" );
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .bridge ;
2
+
3
+ /**
4
+ * 形状抽象类
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/2 17:05
8
+ */
9
+ public abstract class Shape {
10
+ protected Color color ;
11
+
12
+ public void setColor (Color color ) {
13
+ this .color = color ;
14
+ }
15
+
16
+ public abstract void draw ();
17
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .bridge ;
2
+
3
+ /**
4
+ * 正方形
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/2 17:05
8
+ */
9
+ public class SquareShapeImpl extends Shape {
10
+ @ Override
11
+ public void draw () {
12
+ color .bepaint ("正方形" );
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .bridge ;
2
+
3
+ /**
4
+ * 白色
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/2 17:05
8
+ */
9
+ public class WhiteColorImpl implements Color {
10
+ @ Override
11
+ public void bepaint (String shape ) {
12
+ System .out .println ("白色的" + shape );
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .templatemethod ;
2
+
3
+ /**
4
+ * 模板方法 - 创建一个抽象类,它的模板方法被设置为 final
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/2 10:00
8
+ */
9
+ public abstract class Game {
10
+
11
+ /**
12
+ * 初始化
13
+ */
14
+ abstract void initialize ();
15
+
16
+ /**
17
+ * 开始游戏
18
+ */
19
+ abstract void startPlay ();
20
+
21
+ /**
22
+ * 结束游戏
23
+ */
24
+ abstract void endPlay ();
25
+
26
+ /**
27
+ * 模板
28
+ */
29
+ public final void play () {
30
+ // 初始化游戏
31
+ initialize ();
32
+
33
+ // 开始游戏
34
+ startPlay ();
35
+
36
+ // 结束游戏
37
+ endPlay ();
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments