|
7 | 7 | - **结构型模式**:[适配器模式](https://github.com/youlookwhat/DesignPattern#5-适配器模式)、[桥接模式](https://github.com/youlookwhat/DesignPattern#15-桥接模式)、[装饰模式](https://github.com/youlookwhat/DesignPattern#7-装饰者模式)、组合模式、[外观模式](https://github.com/youlookwhat/DesignPattern#8-外观模式)、[享元模式](https://github.com/youlookwhat/DesignPattern#13-享元模式)、[代理模式](https://github.com/youlookwhat/DesignPattern#14-代理模式)。 |
8 | 8 | - **行为型模式**:[模版方法模式](https://github.com/youlookwhat/DesignPattern#9-模板方法模式)、[命令模式](https://github.com/youlookwhat/DesignPattern#6-命令模式)、迭代器模式、[观察者模式](https://github.com/youlookwhat/DesignPattern#1-观察者模式)、中介者模式、备忘录模式、解释器模式、[状态模式](https://github.com/youlookwhat/DesignPattern#10-状态模式)、[策略模式](https://github.com/youlookwhat/DesignPattern#4-策略模式)、职责链模式(责任链模式)、访问者模式。 |
9 | 9 |
|
10 | | -> 参照Hongyang、菜鸟教程、极客学院等文章所写。如有错误欢迎指正,如有侵权,请联系我删除。 |
| 10 | +> 参照Hongyang、菜鸟教程、极客学院等处文章所写。如有错误欢迎指正,如有侵权,请联系我删除。 |
11 | 11 |
|
12 | 12 | ---- |
13 | 13 |
|
|
783 | 783 | ### 15. 桥接模式 |
784 | 784 | > 桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化。这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。 |
785 | 785 |
|
| 786 | + - 主要解决:在有多种可能会变化的情况下,用继承会造成类爆炸问题,扩展起来不灵活。 |
| 787 | + |
| 788 | +实现共分五步: |
| 789 | + |
| 790 | + - 1、创建桥接实现接口。 |
| 791 | + |
| 792 | + ```java |
| 793 | + public interface DrawAPI { |
| 794 | + void drawCircle(int radius, int x, int y); |
| 795 | + } |
| 796 | + ``` |
| 797 | + |
| 798 | + - 2、创建实现了 DrawAPI 接口的实体桥接实现类。[RedCircle](https://github.com/youlookwhat/DesignPattern/blob/master/app/src/main/java/com/example/jingbin/designpattern/bridge/RedCircle.java)、[GreenCircle](https://github.com/youlookwhat/DesignPattern/blob/master/app/src/main/java/com/example/jingbin/designpattern/bridge/GreenCircle.java) |
| 799 | + |
| 800 | + ```java |
| 801 | + public class RedCircle implements DrawAPI { |
| 802 | + |
| 803 | + @Override |
| 804 | + public void drawCircle(int radius, int x, int y) { |
| 805 | + Log.e("---", "Drawing Circle[ color: red, radius: " |
| 806 | + + radius + ", x: " + x + ", " + y + "]"); |
| 807 | + } |
| 808 | + } |
| 809 | + ``` |
| 810 | + |
| 811 | + - 3、使用 DrawAPI 接口创建抽象类 [Shape](https://github.com/youlookwhat/DesignPattern/blob/master/app/src/main/java/com/example/jingbin/designpattern/bridge/Shape.java)。 |
| 812 | + |
| 813 | + ```java |
| 814 | + public abstract class Shape { |
| 815 | + |
| 816 | + protected DrawAPI drawAPI; |
| 817 | + |
| 818 | + protected Shape(DrawAPI drawAPI) { |
| 819 | + this.drawAPI = drawAPI; |
| 820 | + } |
| 821 | + |
| 822 | + public abstract void draw(); |
| 823 | + } |
| 824 | + ``` |
| 825 | + |
| 826 | + - 4、创建实现了 Shape 接口的实体类。 |
| 827 | + |
| 828 | + ```java |
| 829 | + public class Circle extends Shape { |
| 830 | + |
| 831 | + private int x, y, radius; |
| 832 | + |
| 833 | + protected Circle(int x, int y, int radius, DrawAPI drawAPI) { |
| 834 | + super(drawAPI); |
| 835 | + this.x = x; |
| 836 | + this.y = y; |
| 837 | + this.radius = radius; |
| 838 | + } |
| 839 | + |
| 840 | + @Override |
| 841 | + public void draw() { |
| 842 | + drawAPI.drawCircle(radius, x, y); |
| 843 | + } |
| 844 | + } |
| 845 | + ``` |
| 846 | + |
| 847 | + - 5、使用 Shape 和 DrawAPI 类画出不同颜色的圆。 |
| 848 | + |
| 849 | + ```java |
| 850 | + // 画红圆 |
| 851 | + Circle circle = new Circle(10, 10, 100, new RedCircle());s |
| 852 | + circle.draw(); |
| 853 | + // 画绿圆 |
| 854 | + Circle circle2 = new Circle(20, 20, 100, new GreenCircle()); |
| 855 | + circle2.draw(); |
| 856 | + ``` |
| 857 | + |
786 | 858 | ## Download |
787 | 859 | - [DesignPattern.apk](http://download.csdn.net/detail/jingbin_/9684545) |
788 | 860 |
|
|
0 commit comments