如今的手機,按照操做系統分,有ios,有android,還有window phone等。java
若是是按軟件來分的話,就有通信錄,遊戲等。android
按繼承的思惟來考慮的話,以下:
ios
可是問題來了,若是是按照手機品牌來分的話,增長一個新的手機品牌的話,意味着不只要增長手機品牌,還要增長改手機品牌的通信錄和遊戲。iphone
若按照手機軟件來分的話,一樣,增長一個新的軟件的話,意味着不只要增長新的軟件,還要增長各個品牌手機對應的通信錄和遊戲。ide
經過橋接模式的話,若增長軟件,則只須要增長軟件便可,若增長品牌,只需增長品牌便可。this
/** * @Title: PhoneSoftware.java * @Package com.wangbiao.design.bridge * @Description: TODO * @author wangbiao * @date 2014-9-23 下午11:13:20 * @version V1.0 */ package com.wangbiao.design.bridge; /** * @Title: PhoneSoftware.java * @Package com.wangbiao.design.bridge * @Description: TODO * @author wangbiao * @date 2014-9-23 下午11:13:20 * @version V1.0 */ public interface PhoneSoftware { public void operation(); } class ContractList implements PhoneSoftware{ @Override public void operation() { System.out.println("這是通信錄"); } } class PhoneGame implements PhoneSoftware{ @Override public void operation() { System.out.println("這是遊戲"); } } package com.wangbiao.design.bridge; public abstract class Phone { private PhoneSoftware phoneSoftware; public Phone() { // TODO Auto-generated constructor stub } public void setPhoneSoftware(PhoneSoftware phoneSoftware) { this.phoneSoftware = phoneSoftware; } public abstract void display(); public void operation(){ this.phoneSoftware.operation(); } } class ApplePhone extends Phone{ @Override public void display() { System.out.println("這是iphone"); } } class AndroidPhone extends Phone{ @Override public void display() { System.out.println("這是android phone"); } } /** * @Title: Client.java * @Package com.wangbiao.design.bridge * @Description: TODO * @author wangbiao * @date 2014-9-23 下午10:30:29 * @version V1.0 */ package com.wangbiao.design.bridge; /** * @Title: Client.java * @Package com.wangbiao.design.bridge * @Description: TODO * @author wangbiao * @date 2014-9-23 下午10:30:29 * @version V1.0 */ public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub PhoneSoftware game = new PhoneGame(); PhoneSoftware contracts = new ContractList(); Phone phone = new ApplePhone(); phone.setPhoneSoftware(game); phone.display(); phone.operation(); phone.setPhoneSoftware(contracts); phone.display(); phone.operation(); } }