package com.xgp.company.结构性模式.桥接模式;
/**
* 品牌类
*/
public interface Brand {
void info();
}
package com.xgp.company.结构性模式.桥接模式;
/**
* 苹果品牌
*/
public class Apple implements Brand {
@Override
public void info() {
System.out.print("苹果");
}
}
package com.xgp.company.结构性模式.桥接模式;
public class Laptop extends Computer {
public Laptop(Brand brand) {
super(brand);
}
@Override
protected void info() {
super.info();
System.out.println("笔记本");
}
}
package com.xgp.company.结构性模式.桥接模式;
/**
* 抽象的电脑类型类
*/
public abstract class Computer {
//组合:品牌,电脑自带品牌
protected Brand brand;
public Computer(Brand brand) {
this.brand = brand;
}
protected void info() {
//自带品牌
brand.info();
}
}
package com.xgp.company.结构性模式.桥接模式;
public class Desktop extends Computer {
public Desktop(Brand brand) {
super(brand);
}
@Override
protected void info() {
super.info();
System.out.println("台式机");
}
}
package com.xgp.company.结构性模式.桥接模式;
public class Laptop extends Computer {
public Laptop(Brand brand) {
super(brand);
}
@Override
protected void info() {
super.info();
System.out.println("笔记本");
}
}
package com.xgp.company.结构性模式.桥接模式;
public class Test {
public static void main(String[] args) {
//苹果笔记本
Computer computer = new Laptop(new Apple());
computer.info();
//联想台式机
Computer computer12 = new Desktop(new Lenovo());
computer12.info();
}
}
苹果笔记本
联想台式机
原文:https://www.cnblogs.com/xgp123/p/12309435.html