NOTE
6.18 桥接模式
1. 定义 适用于多个维度变化的类层次 2. uml 3. Java 3.1. client 3.2. 维度1 3.3. 维度2 4. 参考 - 设计模式之结构型模式总结 \- Structural Patterns \- 简书
这是历史学习笔记,可能存在过时或不完整的理解。
1. 定义
适用于多个维度变化的类层次
2. uml
3. Java
3.1. client
public class Client
{
public static void main(String[] args)
{
//创建联想兼笔记本
Computer computer = new Laptop(new Lenovo());
computer.sale();
}
}
3.2. 维度1
public interface Brand
{
void sale();
}
public class Dell implements Brand
{
@Override
public void sale()
{
System.out.println("戴尔品牌");
}
}
public class Lenovo implements Brand
{
@Override
public void sale()
{
System.out.println("联想品牌");
}
}
3.3. 维度2
public class Computer
{
protected Brand brand;
public Computer(Brand brand)
{
this.brand = brand;
}
public void sale()
{
this.brand.sale();
}
}
class Desktop extends Computer
{
public Desktop(Brand brand)
{
super(brand);
}
@Override
public void sale()
{
super.sale();
System.out.println("桌面机");
}
}
public class Laptop extends Computer
{
public Laptop(Brand brand)
{
super(brand);
}
@Override
public void sale()
{
super.sale();
System.out.println("笔记本");
}
}
讨论
使用 GitHub 账号参与讨论,评论会保存在 GitHub Issues 中。在 GitHub 查看