百度 移动网站优化,删除首页wordpress,厅网站建设项目背景,wordpress 免备案cdn结构型模式 - 桥接模式 (Bridge)
桥接模式是一种结构型设计模式#xff0c;它将抽象部分与实现部分分离#xff0c;使它们可以独立地变化。 // 软件接口#xff0c;作为实现部分
interface Software {void run();
}// 游戏软件类#xff0c;实现 Software 接口
class Game…结构型模式 - 桥接模式 (Bridge)
桥接模式是一种结构型设计模式它将抽象部分与实现部分分离使它们可以独立地变化。 // 软件接口作为实现部分
interface Software {void run();
}// 游戏软件类实现 Software 接口
class Game implements Software {Overridepublic void run() {System.out.println(运行游戏软件);}
}// 音乐播放器软件类实现 Software 接口
class MusicPlayer implements Software {Overridepublic void run() {System.out.println(运行音乐播放器软件);}
}// 手机抽象类持有 Software 接口的引用
abstract class Phone {protected Software software;public Phone(Software software) {this.software software;}public abstract void runSoftware();
}// 华为手机类继承自 Phone 抽象类
class HuaweiPhone extends Phone {public HuaweiPhone(Software software) {super(software);}Overridepublic void runSoftware() {System.out.print(华为手机 );software.run();}
}// 苹果手机类继承自 Phone 抽象类
class ApplePhone extends Phone {public ApplePhone(Software software) {super(software);}Overridepublic void runSoftware() {System.out.print(苹果手机 );software.run();}
}// 客户端代码
public class BridgePatternExample {public static void main(String[] args) {// 创建游戏软件对象Software game new Game();// 创建音乐播放器软件对象Software musicPlayer new MusicPlayer();// 创建华为手机并安装游戏软件Phone huaweiGamePhone new HuaweiPhone(game);huaweiGamePhone.runSoftware();// 创建苹果手机并安装音乐播放器软件Phone appleMusicPhone new ApplePhone(musicPlayer);appleMusicPhone.runSoftware();}
}