public class BridgeTest1 { public static void main(String[] args) { HandsetBrand ab; ab = new HandsetBrandN(); ab.SetHandsetSoft(new HandsetGame()); ab.Run(); ab.SetHandsetSoft(new HandsetAddressList()); ab.Run(); ab = new HandsetBrandM(); ab.SetHandsetSoft(new HandsetGame()); ab.Run(); ab.SetHandsetSoft(new HandsetAddressList()); ab.Run(); ab = new HandsetBrandS(); ab.SetHandsetSoft(new HandsetGame()); ab.Run(); ab.SetHandsetSoft(new HandsetAddressList()); ab.Run(); ab.SetHandsetSoft(new HandsetMP3()); ab.Run(); System.out.println(); } } abstract class HandsetSoft { public abstract void Run(); } class HandsetGame extends HandsetSoft { public void Run() { System.out.println("运行手机游戏"); } } class HandsetAddressList extends HandsetSoft { public void Run() { System.out.println("运行手机通讯录"); } } class HandsetMP3 extends HandsetSoft { public void Run() { System.out.println("运行手机MP3播放"); } } abstract class HandsetBrand { protected HandsetSoft soft; public void SetHandsetSoft(HandsetSoft soft) { this.soft = soft; } public abstract void Run(); } class HandsetBrandN extends HandsetBrand { public void Run() { soft.Run(); } } class HandsetBrandM extends HandsetBrand { public void Run() { soft.Run(); } } class HandsetBrandS extends HandsetBrand { public void Run() { soft.Run(); } } public class BridgeTest2 { public static void main(String[] args) { Abstraction ab = new RefinedAbstraction(); ab.setImplementor(new ConcreteImplementorA()); ab.Operation(); ab.setImplementor(new ConcreteImplementorB()); ab.Operation(); System.out.println(); } } abstract class Implementor { public abstract void Operation(); } class ConcreteImplementorA extends Implementor { public void Operation() { System.out.println("具体实现A的方法执行"); } } class ConcreteImplementorB extends Implementor { public void Operation() { System.out.println("具体实现B的方法执行"); } } class Abstraction { protected Implementor implementor; public void setImplementor(Implementor implementor) { this.implementor = implementor; } public void Operation() { implementor.Operation(); } } class RefinedAbstraction extends Abstraction { public void Operation() { implementor.Operation(); } }