将抽象与实现分离,使它们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度
就是将2种抽象桥接起来 从而实现2中实现类的组合和随意扩展
package study.org;
public class Demo {
public static void main(String[] args) {
Phone phone = new Huawei();
Style style = new AStyle(phone);
style.draw();
style.phone.open();
}
}
interface Phone {
void open();
}
class Huawei implements Phone {
@Override
public void open() {
System.out.println("Huawei open");
}
}
class Xiaomi implements Phone {
@Override
public void open() {
System.out.println("Xiaomi open");
}
}
abstract class Style {
protected Phone phone;
public Style(Phone phone) {
this.phone = phone;
}
public abstract void draw();
}
class AStyle extends Style{
public AStyle(Phone phone) {
super(phone);
}
@Override
public void draw() {
System.out.println("AStyle draw");
}
}
网友评论