浅谈Java设计模式——桥接模式(Bridge)

一、概述

        将抽象部分与它的实现部分分离,使它们都可以独立地变化。如果说某个系统能够从多个角度来进行分类,且每一种分类都可能会变化,那么我们需要做的就是讲这多个角度分离出来,使得他们能独立变化,减少他们之间的耦合,这个分离过程就使用了桥接模式。所谓桥接模式就是讲抽象部分和实现部分隔离开来,使得他们能够独立变化。桥接模式将继承关系转化成关联关系,封装了变化,完成了解耦,减少了系统中类的数量,也减少了代码量。

二、使用场景

1.你不希望在抽象和它的实现部分之间有一个固定的绑定关系。 例如这种情况可能是因为,在程序运行时刻实现部分应可以被选择或者切换。

2.类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。 这时Bridge模式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充。 

3.对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。 

4.正如在意图一节的第一个类图中所示的那样,有许多类要生成。 这样一种类层次结构说明你必须将一个对象分解成两个部分。

5.你想在多个对象间共享实现(可能使用引用计数),但同时要求客户并不知道这一点。

三、参与者

1.Abstraction 定义抽象类的接口。 维护一个指向Implementor类型对象的指针。 

2.RefinedAbstraction 扩充由Abstraction定义的接口。 

3.Implementor 定义实现类的接口,该接口不一定要与Abstraction的接口完全一致。 事实上这两个接口可以完全不同。 一般来                                            讲, Implementor接口仅提供基本操作,而Abstraction则定义了基于这些基本操作的较高层次的操作。

4.ConcreteImplementor 实现Implementor接口并定义它的具体实现。

四、类图

五、示例代码

1.Abstraction:

/**
 * Abstractor类
 * @author zhipeng_Tong
 */
public abstract class Person {
    private Clothing clothing;
    private String type;

    public Clothing getClothing() {
        return clothing;
    }

    public void setClothing(Clothing clothing) {
        this.clothing = clothing;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public abstract void dress();
}

 2.RefinedAbstraction:

public class Man extends Person {
    public Man() {
        setType("男人");
    }

    @Override
    public void dress() {
        Clothing clothing = getClothing();
        clothing.personDressCloth(this);
    }
}

public class Lady extends Person {
    public Lady() {
        setType("女人");
    }

    @Override
    public void dress() {
        Clothing clothing = getClothing();
        clothing.personDressCloth(this);
    }
}

3.Implementor:

/**
 * Implementor类:Clothing
 * @author zhipeng_Tong
 */
public abstract class Clothing {
    public abstract void personDressCloth(Person person);
}

4.ConcreteImplementor:

/**
 * ConctreteImplementor
 * @author zhipeng_Tong
 */
public class Jecket extends Clothing {
    @Override
    public void personDressCloth(Person person) {
        System.out.println(person.getType() + "穿马甲");
    }
}

/**
 * ConctreteImplement
 * @author zhipeng_Tong
 */
public class Trouser extends Clothing {
    @Override
    public void personDressCloth(Person person) {
        System.out.println(person.getType() + "穿裤子");
    }
}

5.测试代码

public class Client {
    public static void main(String[] args) {

        Person man = new Man();

        Person lady = new Lady();

        Clothing jacket = new Jecket();

        Clothing trouser = new Trouser();

        jacket.personDressCloth(man);
        trouser.personDressCloth(man);

        jacket.personDressCloth(lady);
        trouser.personDressCloth(lady);
    }
}

运行结果:

男人穿马甲
男人穿裤子
女人穿马甲
女人穿裤子

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
桥接模式是一种结构型设计模式,它的主要目的是将抽象部分与实现部分分离,使它们能够独立地变化。下面是一个Java桥接模式的示例: 首先定义一个抽象类Shape,它有一个DrawAPI的成员变量,表示它的实现。 ```java public abstract class Shape { protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI) { this.drawAPI = drawAPI; } public abstract void draw(); } ``` 然后定义具体的形状类,比如Circle和Rectangle,它们继承自抽象类Shape,并实现了draw方法。 ```java public class Circle extends Shape { private int x, y, radius; public Circle(int x, int y, int radius, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.radius = radius; } @Override public void draw() { drawAPI.drawCircle(radius, x, y); } } public class Rectangle extends Shape { private int x, y, width, height; public Rectangle(int x, int y, int width, int height, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.width = width; this.height = height; } @Override public void draw() { drawAPI.drawRectangle(x, y, width, height); } } ``` 最后定义一个DrawAPI接口,它有两个方法drawCircle和drawRectangle,表示画圆和画矩形的实现。 ```java public interface DrawAPI { void drawCircle(int radius, int x, int y); void drawRectangle(int x, int y, int width, int height); } ``` 现在,我们可以使用不同的DrawAPI实现来创建不同的Shape对象,比如: ```java DrawAPI redDrawAPI = new RedDrawAPI(); Shape redCircle = new Circle(100, 100, 10, redDrawAPI); redCircle.draw(); DrawAPI greenDrawAPI = new GreenDrawAPI(); Shape greenRectangle = new Rectangle(50, 50, 100, 200, greenDrawAPI); greenRectangle.draw(); ``` 这样就可以将形状的抽象部分和实现部分分离了。如果需要增加一种新的形状或者实现,只需要创建一个新的类实现Shape或者DrawAPI接口即可,不需要修改原有的代码。 完整的代码示例可以参考以下链接:https://github.com/iluwatar/java-design-patterns/tree/master/bridge

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值