GOF23设计模式——桥接模式

UML图:
这里写图片描述

/**
 * 品牌
 * 
 * @author Administrator
 *
 */
public interface Brand {

    public void sale();
}
/**
 * 比亚迪
 * 
 * @author Administrator
 *
 */
public class BydBrand implements Brand {

    @Override
    public void sale() {
        System.out.println("销售比亚迪品牌");
    }

}
/**
 * 长安汽车
 * 
 * @author Administrator
 *
 */
public class ChangAnBrand implements Brand {

    @Override
    public void sale() {
        System.out.println("销售长安汽车品牌");
    }
}
/**
 * 吉利汽车
 * 
 * @author Administrator
 *
 */
public class GeelyBrand implements Brand {

    @Override
    public void sale() {
        System.out.println("销售吉利汽车品牌");
    }

}
/**
 * 汽车
 * 
 * @author Administrator
 *
 */
public class Car {

    Brand brand;

    public Car(Brand brand) {
        this.brand = brand;
    }
    public void sale() {
        brand.sale();
        System.out.println("销售汽车");
    }
}
/**
 * 公交车
 * 
 * @author Administrator
 *
 */
public class Bus extends Car {

    public Bus(Brand brand) {
        super(brand);
    }

    @Override
    public void sale() {
        brand.sale();
        System.out.println("销售公交车");
    }
}
/**
 * 小桥车
 * 
 * @author Administrator
 *
 */
public class Sedan extends Car {

    public Sedan(Brand brand) {
        super(brand);
    }

    @Override
    public void sale() {
        brand.sale();
        System.out.println("销售小桥车");
    }
}
/**
 * 测试客户端
 * 
 * @author Administrator
 *
 */
public class Client {

    public static void main(String[] args) {

        Car car1 = new Bus(new BydBrand());
        car1.sale();
        System.out.println();

        Car car2 = new Sedan(new BydBrand());
        car2.sale();
        System.out.println();

        Car car3 = new Bus(new ChangAnBrand());
        car3.sale();
        System.out.println();

        Car car4 = new Sedan(new GeelyBrand());
        car4.sale();
        System.out.println();
    }

}

运行结果:

销售比亚迪品牌
销售公交车

销售比亚迪品牌
销售小桥车

销售长安汽车品牌
销售公交车

销售吉利汽车品牌
销售小桥车

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值