ErBing

往事已经定格,未来还要继续。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

来源:http://www.bjsxt.com/ 
一、【GOF23设计模式】_桥接模式、多层继承结构、银行日志管理、管理系统消息管理、人力资源的奖金计算 
场景

未用桥接模式:

 1 package com.test.bridge;
 2 
 3 public interface Computer {
 4     void sale();
 5 }
 6 
 7 class Desktop implements Computer{
 8     @Override
 9     public void sale() {
10         System.out.println("销售台式机");
11     }
12 }
13 class Laptop implements Computer{
14     @Override
15     public void sale() {
16         System.out.println("销售笔记本");
17     }
18 }
19 class Pad implements Computer{
20     @Override
21     public void sale() {
22         System.out.println("销售平板电脑");
23     }
24 }
25 
26 class LenovoDesktop extends Desktop{
27     @Override
28     public void sale(){
29         System.out.println("销售联想台式机");
30     }
31 }
32 class LenovoLaptop extends Laptop{
33     @Override
34     public void sale(){
35         System.out.println("销售联想笔记本");
36     }
37 }
38 class LenovoPad extends Pad{
39     @Override
40     public void sale(){
41         System.out.println("销售联想平板电脑");
42     }
43 }
44 
45 class ShenzhouDesktop extends Desktop{
46     @Override
47     public void sale(){
48         System.out.println("销售神舟台式机");
49     }
50 }
51 class ShenzhouLaptop extends Laptop{
52     @Override
53     public void sale(){
54         System.out.println("销售神舟笔记本");
55     }
56 }
57 class ShenzhouPad extends Pad{
58     @Override
59     public void sale(){
60         System.out.println("销售神舟平板电脑");
61     }
62 }
63 
64 class DellDesktop extends Desktop{
65     @Override
66     public void sale(){
67         System.out.println("销售戴尔台式机");
68     }
69 }
70 class DellLaptop extends Laptop{
71     @Override
72     public void sale(){
73         System.out.println("销售戴尔笔记本");
74     }
75 }
76 class DellPad extends Pad{
77     @Override
78     public void sale(){
79         System.out.println("销售戴尔平板电脑");
80     }
81 }

场景分析

桥接模式核心要点

使用桥接模式:

 1 package com.test.bridge;
 2 /**
 3  * 电脑类型的维度
 4  */
 5 public class Computer2 {
 6     protected Brand brand;
 7 
 8     public Computer2(Brand brand) {
 9         super();
10         this.brand = brand;
11     }
12 
13     public void sale(){
14         brand.sale(); 
15     }
16 }
17 
18 class Desktop2 extends Computer2{
19     public Desktop2(Brand brand) {
20         super(brand);
21     }
22     @Override
23     public void sale() {
24         super.sale();
25         System.out.println("销售台式机");
26     }
27 }
28 class Laptop2 extends Computer2{
29     public Laptop2(Brand brand) {
30         super(brand);
31     }
32     @Override
33     public void sale() {
34         super.sale();
35         System.out.println("销售笔记本");
36     }
37 }
 1 package com.test.bridge;
 2 /**
 3  * 品牌
 4  */
 5 public interface Brand {
 6     void sale();
 7 }
 8 
 9 class Lenovo implements Brand{
10     @Override
11     public void sale() {
12         System.out.println("销售联想电脑");
13     }
14 }
15 class Dell implements Brand{
16     @Override
17     public void sale() {
18         System.out.println("销售戴尔电脑");
19     }
20 }
21 //新增之前没有的品牌,不用同时新增电脑类型(不用在Computer2中写代码)
22 class Shenzhou implements Brand{
23     @Override
24     public void sale() {
25         System.out.println("销售神舟电脑");
26     }
27 }
 1 package com.test.bridge;
 2 
 3 public class Client {
 4     public static void main(String[] args) {
 5         //销售联想的笔记本电脑
 6         Computer2 c = new Laptop2(new Lenovo());
 7         c.sale();
 8 
 9         //销售神舟的笔记本电脑
10         Computer2 c2 = new Desktop2(new Shenzhou());
11         c2.sale();
12     }
13 }

桥接模式总结

桥接模式实际开发中应用场景

posted on 2016-08-24 12:44  ErBing  阅读(212)  评论(0编辑  收藏  举报