结构型设计模式 ,共7种
(1)桥接模式
桥接模式
将抽象和实现部分的分离,更好的可扩展性,桥接模式要求正确识别出系统中两个独立变化的维度,因此其使用范围有一定的局限性
//抽象的实现类 public interface Memory { void addMemory(); } //具体的实现类 public class Memory6G implements Memory{ @Override public void addMemory() { System.out.println("安装了6G内存"); } } //具体的实现类 public class Memory8G implements Memory{ @Override public void addMemory() { System.out.println("安装了8G内存"); } } //具体部分的抽象 public abstract class Phone { protected Memory memory; public void setMemory(Memory memory) { this.memory = memory; } protected abstract void buyPhone(); } public class Huiwei extends Phone{ @Override protected void buyPhone() { System.out.println("购买华为手机"); memory.addMemory(); } } public class Xiaomi extends Phone { @Override protected void buyPhone() { System.out.println("购买小米手机"); memory.addMemory(); } } public class MainClass { public static void main(String[] args) { Phone phone = new Huiwei(); phone.setMemory(new Memory6G()); phone.buyPhone(); System.out.println("==============="); phone.setMemory(new Memory8G()); phone.buyPhone(); } }