脚本宝典收集整理的这篇文章主要介绍了设计模式——桥接模式(Bridge模式),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
public interface Brand {
// 品牌口号
String brandVoice();
}
public class Xiaomi implements Brand {
@override
public String brandVoice() {
return "为发烧而生...";
}
}
public interface PhoneStyle {
// 手机样式
String style();
}
public abstract class Phone {
//组合品牌
PRivate Brand mbrand;
//构造器
public Phone(Brand mBrand) {
this.mBrand = mBrand;
}
protected void oPEn(){
this.mBrand.brandVoice();
System.out.println("phone open");
}
protected void close(){
this.mBrand.brandVoice();
System.out.println("phone close");
}
protected void call(){
this.mBrand.brandVoice();
System.out.println("phone call");
}
}
定义具体的Phone类
//折叠式手机类,继承抽象类Phone
public class FoldedPhone extends Phone implements PhoneStyle {
//构造器
public FoldedPhone(Brand mBrand) {
super(mBrand);
}
public void open(){
super.open();
style();
}
public void close(){
super.close();
style();
}
public void call(){
super.call();
style();
}
@Override
public String style() {
return "折叠式手机";
}
}
public class Client {
public static void main(String[] args){
//获取折叠式手机(样式+品牌)
Phone phone1 = new FoldedPhone(new Xiaomi());
phone1.open();
phone1.call();
phone1.close();
}
}
以上是脚本宝典为你收集整理的设计模式——桥接模式(Bridge模式)全部内容,希望文章能够帮你解决设计模式——桥接模式(Bridge模式)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。