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