桥接模式Bridge Pattern

桥接模式Bridge Pattern

1.桥接模式定义桥接模式是一种结构型设计模式,它将抽象部分和它的实现部分分离,使它们都可以独立地变化。

2.桥接模式优点它可以让抽象和实现部分变化,它可以提高系统的灵活性,它可以减少系统的复杂性。

3.桥接模式缺点它会增加系统的理解难度和编程难度,因为它需要开发者具备两个不同的知识体系。

4.桥接模式示例代码传统设计模式讲解时使用的示例代码,大都采用与读者日常生活接解的业务系统没有多大关联关系。以致大部分读者无法做到学以致用,学完就忘记。本文采用使用日常生活中随处可见的优惠券业务来编写实现代码:

//桥接模式的定义抽象类

public abstract class Coupon {

protected CouponImplementor couponImplementor;

public Coupon(CouponImplementor couponImplementor) {

this.couponImplementor = couponImplementor;

}

public abstract void issueCoupon();

}

//桥接模式的定义实现类

public class ConcreteCoupon extends Coupon {

public ConcreteCoupon(CouponImplementor couponImplementor) {

super(couponImplementor);

}

@Override

public void issueCoupon() {

couponImplementor.issueCoupon("discount");

}

}

//定义桥接模式的抽象实现类

public abstract class CouponImplementor {

public abstract void issueCoupon(String type);

}

//定义桥接模式的具体实现类

public class ConcreteCouponImplementor extends CouponImplementor {

@Override public void issueCoupon(String type) {

System.out.println("Issuing " + type + " coupon");

}

}

//桥接模式的客户端使用

public class Client {

public static void main(String[] args) {

CouponImplementor couponImplementor = new ConcreteCouponImplementor();

Coupon coupon = new ConcreteCoupon(couponImplementor);

coupon.issueCoupon();

}

}

相关星际资讯

假冒小姐什么时候播出
28365

假冒小姐什么时候播出

🕒 07-10 👁️ 6088
word大声朗读功能,不能使用,如何解决?
365bet足球平台

word大声朗读功能,不能使用,如何解决?

🕒 07-25 👁️ 836
FGO小安:从游戏角色到文化现象
365bet足球平台

FGO小安:从游戏角色到文化现象

🕒 01-05 👁️ 9830