桥接模式

桥接模式是一种结构型模式,它主要应对的是:由于实际的需要,某个类具有两个或以上的维度变化,如果只是使用继承将无法实现这种需要,或者使得设计变得相当臃肿。


package com.fcy.model;
interface Peppery{
	String style();
}
class PepperyStyle implements Peppery{
	public String style(){
		return "辣味很重,很过瘾";
	}
}
class PlainStyle implements Peppery{
	public String style(){
		return "味道清淡,很养胃";
	}
}
abstract class AbstractNoodle{
	protected Peppery style;
	public AbstractNoodle(Peppery style){
		this.style=style;
	}
	public abstract void eat();
}
class ProkyNoodle extends AbstractNoodle{
	public ProkyNoodle(Peppery style){
		super(style);
	}
	public void eat(){
		System.out.println("这是一碗稍油腻的猪肉面条。"+super.style.style());
	}
}
class BeefNoodle extends AbstractNoodle{
	public BeefNoodle(Peppery style){
		super(style);
	}
	public void eat(){
		System.out.println("这是一碗美味的牛肉面条。"+super.style.style());
	}
}
public class BridgeTest {
	public static void main(String[] args) {
		AbstractNoodle noodle1=new BeefNoodle(new PepperyStyle());
		noodle1.eat();
		AbstractNoodle noodle2=new BeefNoodle(new PlainStyle());
		noodle2.eat();
		AbstractNoodle noodle3=new ProkyNoodle(new PepperyStyle());
		noodle3.eat();
		AbstractNoodle noodle4=new ProkyNoodle(new PlainStyle());
		noodle4.eat();
	}
}


运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值