0 设计模式
不了解设计模式的小伙伴可以通过这篇文章了解一下什么是设计模式
https://blog.csdn.net/qq_42874315/article/details/120006447
1 桥接模式
桥接是用于把抽象化与实现化解耦,使得二者可以独立变化。分离抽象和具体的实现,让他们可以独自发展。
举例:人和吃饭的关系,人本身是抽象的,饭也是抽象的,这样就可以认为人和饭都是需要扩展的。
2 实现思路
我们有一个作为桥接实现的 DrawAPI 接口和实现了 DrawAPI 接口的实体类 RedCircle、GreenCircle。
Shape 是一个抽象类,将使用 DrawAPI 的对象。BridgePatternDemo 类使用 Shape 类来画出不同颜色的圆。
3 需要的类
这里引入了一个例子帮助大家更好的理解:人用水彩笔去画画,不同的人可以用不同的水彩笔去画画,双维度扩展。
- 桥接接口(水彩笔)
- 桥接实现类(蓝色水彩笔,红色水彩笔…)
- 抽象类(人类,聚合了桥接接口,在抽象类中构造了这个接口)
- 抽象类的实现类(你,我,他,在实现抽象类的方法中使用聚合的接口去调用)
- 测试类
4 具体实现
4.1 DrawAPI(桥接接口)
/**
* 桥接接口
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:46
*/
public interface DrawAPI {
void drawCircle(int radius, int x, int y);
}
4.2 桥接实现类
4.2.1 GreenCircle
/**
* 实现了DrawAPI接口的实体桥接实现类
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:48
*/
public class GreenCircle implements DrawAPI{
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Circle[ color: green, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}
4.2.2 RedCircle
/**
* 实现了DrawAPI接口的实体桥接实现类
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:47
*/
public class RedCircle implements DrawAPI{
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Circle[ color: red, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}
4.3 Shape(抽象类)
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:49
*/
public abstract class Shape {
protected DrawAPI drawAPI;
public Shape(DrawAPI drawAPI) {
this.drawAPI = drawAPI;
}
public abstract void draw();
}
4.4 Circle(Shape的实现类)
/**
* 实现了 Shape 抽象类的实体类
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:50
*/
public class Circle extends Shape{
private int x, y, radius;
public Circle(DrawAPI drawAPI, int x, int y, int radius) {
super(drawAPI);
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public void draw() {
drawAPI.drawCircle(radius,x,y);
}
}
4.5 测试类
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:52
*/
public class Test {
public static void main(String[] args) {
Shape redCircle = new Circle(new RedCircle(),100,100, 10);
Shape greenCircle = new Circle(new GreenCircle(),100, 10,100);
redCircle.draw();
greenCircle.draw();
}
}
5 扩展
5.1 实例中的缺陷设想
- 如果Shape再多一个Square的实现类,对应的可以再加一个BlackSquare的桥接实现类
- 但是由于Shape的实现类和桥接的实现类之间没有限制,所以可能会造成,new Square(new RedCircle)的情况
- 这样的话还是会输出,“红色的圈”(这样就出问题了)
- 所以应该一个DrawApi的子类接口,例如CircleDrawAPI或SquareDrawApi,让具体的桥接实现类去实现这些子接口
- 同时让Shape的实现类在构造时,不再使用DrawApi接口进行构造,而使用具体的子接口进行构造,这样每种类型就规范
- 例如:Square就只能去拿BlackSquare,Circle就只能去拿RedCircle
5.2 实例改造
5.2.1 未改变:DrawAPI(,桥接接口)
/**
* 桥接接口
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:46
*/
public interface DrawAPI {
public void drawCircle(int radius, int x, int y);
}
5.2.2 新增:桥接接口的子接口(只起标志作用)
CircleDrawAPI
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 22:16
*/
public interface CircleDrawAPI extends DrawAPI {
}
SquareDrawAPI
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 22:16
*/
public interface SquareDrawAPI extends DrawAPI{
}
5.2.3 修改:桥接子接口的实现类(原本是直接实现桥接接口)
1.实现CircleDrawAPI
GreenCircle
/**
* 实现了DrawAPI接口的实体桥接实现类
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:48
*/
public class GreenCircle implements CircleDrawAPI {
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Circle[ color: green, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}
RedCircle
/**
* 实现了DrawAPI接口的实体桥接实现类
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:47
*/
public class RedCircle implements CircleDrawAPI {
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Circle[ color: red, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}
2.实现SquareDrawAPI
BlackSquare
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 22:18
*/
public class BlackSquare implements SquareDrawAPI {
@Override
public void drawCircle(int radius, int x, int y) {
System.out.println("Drawing Square[ color: black, radius: "
+ radius +", x: " +x+", "+ y +"]");
}
}
5.2.4 未改变:Shape(抽象类)
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:49
*/
public abstract class Shape {
protected DrawAPI drawAPI;
public Shape(DrawAPI drawAPI) {
this.drawAPI = drawAPI;
}
public abstract void draw();
}
5.2.5 修改:抽象类的实现类(原先在构造时是面向DrawAPI,现在面向DrawAPI的子接口)
Circle(修改构造方法中的DrawAPI为CircleDrawAPI)
/**
* 实现了 Shape 抽象类的实体类
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:50
*/
public class Circle extends Shape{
private int x, y, radius;
public Circle(CircleDrawAPI drawAPI, int x, int y, int radius) {
super(drawAPI);
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public void draw() {
drawAPI.drawCircle(radius,x,y);
}
}
Square(修改构造方法中的DrawAPI为SquareDrawAPI)
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 22:20
*/
public class Square extends Shape{
private int x, y, radius;
public Square(SquareDrawAPI drawAPI, int x, int y, int radius) {
super(drawAPI);
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public void draw() {
drawAPI.drawCircle(radius,x,y);
}
}
5.2.6 测试类
用Squaer去调用GreenCircle会报错,达到预期的效果了
/**
* @Author ChenJiahao(程序员五条)
* @Date 2021/8/23 21:52
*/
public class Test {
public static void main(String[] args) {
Shape redCircle = new Circle(new RedCircle(),100,100, 10);
// new GreenCircle() 报错
// Shape greenCircle = new Square(new GreenCircle(),100, 10,100);
Shape blackSquare = new Square(new BlackSquare(),100, 10,100);
redCircle.draw();
blackSquare.draw();
}
}
6 思维导图
7 示例源码地址
https://github.com/ChenJiahao0205/design-pattern/tree/master
最后
我是通过马士兵老师的视频和菜鸟教程学习的,部分内容可能会有雷同
想阅读更多设计模式相关文章,欢迎到我的专栏【设计模式学习笔记】、【设计模式】中去查看
在23篇设计模式文章发布完成之后,我会公开完整的思维导图,点关注,不迷路
感谢大家看到这里,文章如有不足,欢迎大家指出;彦祖点个赞吧彦祖点个赞吧彦祖点个赞吧,欢迎关注程序员五条!