桥接模式

桥接模式应用场景:

比如在商场买手机,你可以买各种品牌的手机,而各种品牌的手机又很多种促销的礼品可供选择,那么就存在多个维度的变化。

 

public interface Gift {
		public String selectGift();
}

 

 

/**
 * 买手机的时候可以选择什么样的赠品
 * @author nickyzhang
 */
public abstract class AbstarctCellphone {
		protected Gift gift;
		public AbstarctCellphone (Gift gift){
				this.gift =gift;
		}
		
		public abstract void BuyCellphone();
}

 

 

public class CellphoneChain implements Gift{
	public String selectGift() {
			return "手机链";
	}
}

 

 

public class MobilePhonePower implements Gift {
		public String selectGift() {
				return "手机移动电源";
		}
}

 

 

public class GalaxyS4 extends AbstarctCellphone {

		public GalaxyS4(Gift gift) {
				super(gift);
		}

		public void BuyCellphone() {
				System.out.println("I have bought  GalaxyS4 "+super.gift.selectGift());
		}
}

 

 

public class IPhone4S extends AbstarctCellphone {
		public IPhone4S(Gift gift) {
				super(gift);
		}
		public void BuyCellphone() {
				System.out.println("I have bought  IPhone4S "+super.gift.selectGift());
		}
		
}

 

 

public class BridgeTest {
		public static void main(String[] args) {
				//购买三星且选择手机链
				AbstarctCellphone cellPhone1= new GalaxyS4(new CellphoneChain());
				cellPhone1.BuyCellphone();
				//购买苹果且选择移动电源
				AbstarctCellphone cellPhone2 = new IPhone4S(new MobilePhonePower());
				cellPhone2.BuyCellphone();
		}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值