//苹果品牌
public class Apple implements Brand{
@Override
public void info() {
System.out.println("苹果");
}
}
-----------------------------------
public interface Brand {
void info();
}
-----------------------------------
public class Computer {
//组合,品牌
protected Brand brand;
public Computer(Brand brand){
this.brand = brand;
}
public void info(){
brand.info();//自带品牌
}
}
-----------------------------------
public class Desktop extends Computer{
public Desktop(Brand brand){
super(brand);
}
@Override
public void info() {
super.info();
System.out.println("台式机");
}
}
--------------------------------------
public class Laptop extends Computer{
public Laptop(Brand brand){
super(brand);
}
@Override
public void info() {
super.info();
System.out.println("笔记本");
}
}
----------------------------------
//联想品牌
public class Lenovo implements Brand{
@Override
public void info() {
System.out.println("联想");
}
}
-----------------------------------
public class Test {
public static void main(String[] args) {
Computer computer = new Computer(new Apple());
computer.info();
Computer computer1 = new Computer(new Lenovo());
computer1.info();
Desktop desktop = new Desktop(new Apple());
desktop.info();
Laptop laptop = new Laptop(new Apple());
laptop.info();
}
}
-----------------------------------
返回结果
苹果
联想
苹果
台式机
苹果
笔记本
桥接设计模式
于 2022-03-01 21:19:37 首次发布