现对不同手机类型(折叠式、直立式、旋转式)的不同品牌(华为、小米、苹果)实现操作编程(开机、关机、上网、打电话等)
在桥接模式下,如果需要新增一个品牌或者新增一个类别,直接增加相应的类即可,因为品牌和类别的类之间没有直接的耦合关系,类别依赖品牌的抽象
/***
* @author shaofan
* @Description
*/
public class Bridge {
public static void main(String[] args) {
Phone xiaomiFoldPhone = new FoldedPhone(new XiaoMi());
Phone xiaomiUpRightPhone = new UpRightPhone(new XiaoMi());
Phone huaweiFoldPhone = new FoldedPhone(new HuaWei());
Phone huaweiUpRightPhone = new UpRightPhone(new HuaWei());
xiaomiFoldPhone.call();
xiaomiUpRightPhone.call();
huaweiFoldPhone.call();
huaweiUpRightPhone.call();
}
}
/***
* 抽象持有一个手机的具体实现,然后向外提供功能接口,内部调用具体实现的接口
*/
abstract class Phone{
private Brand brand;
public Phone(Brand brand){
this.brand = brand;
}
protected void call(){
brand.call();
}
}
class FoldedPhone extends Phone{
public FoldedPhone(Brand brand) {
super(brand);
}
@Override
public void call(){
System.out.println("foledPhone");
super.call();
}
}
class UpRightPhone extends Phone{
public UpRightPhone(Brand brand) {
super(brand);
}
@Override
public void call(){
System.out.println("upRightPhone");
super.call();
}
}
interface Brand{
void call();
}
class XiaoMi implements Brand{
@Override
public void call() {
System.out.println("xiaomi call");
}
}
class HuaWei implements Brand{
@Override
public void call() {
System.out.println("huawei call");
}
}
java.sql.Connection即实现,DriverManager即抽象,而Mysql和Oracle等提供实现和抽象的子类实现,在扩展的时候,仅需要在两个父类上增加新的对应的子类即可