软件设计模式(Design pattern),又称设计模式,是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性、程序的重用性。
汇总目录链接:【玩转23种Java设计模式】学习目录汇总整理
桥接模式是将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(interface)模式。
桥梁模式所涉及的角色有:
public abstract class AbstractBrand {
protected AbstractSoftware abstractSoftware;
// 设置手机软件
public void setSoftware(AbstractSoftware abstractSoftware) {
this.abstractSoftware = abstractSoftware;
}
// 运行
public abstract void run();
}
public abstract class AbstractSoftware {
public abstract void run();
}
public class HuaWeiPhone extends AbstractBrand {
@Override
public void run() {
System.out.println("华为手机");
abstractSoftware.run();
}
}
public class GameCenter extends AbstractSoftware {
@Override
public void run() {
System.out.println("运行游戏中心");
}
}
public class Main {
public static void main(String[] args) {
AbstractBrand abstractBrand = new HuaWeiPhone();
abstractBrand.setSoftware(new GameCenter());
abstractBrand.run();
}
}
控制台输出:
华为手机
运行游戏中心
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://yunfan.blog.csdn.net/article/details/120651502
内容来源于网络,如有侵权,请联系作者删除!