Bridge

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bridge相关的知识,希望对你有一定的参考价值。

a simple example for Bridge:
/**
* Created by Administrator on 2016/3/9.
* a interface of taste style.
**/
public interface Peppery {
String style();
}

/**
* Created by Administrator on 2016/3/9.
* first type of taste
**/
public class PepperyStyle implements Peppery {
public String style(){
return"辣味很重,很过瘾...";
}
}



/**
* Created by Administrator on 2016/3/9.
* second type of taste
*/
public class PlainStyle implements Peppery{
public String style(){
return "味道清淡,很养胃...";
}
}



/**
* Created by Administrator on 2016/3/9.
* a abstract class who contain Peppery
*/
public abstract class AbstractNoodle {
/**组合一个Peppery变量,用于将该维度的变化独立出来**/
protected Peppery style;
/**每份Noodle必须组合一个Peppery对象**/
public AbstractNoodle(Peppery style) {
this.style=style;
}
public abstract void eat();
}



/**
* Created by Administrator on 2016/3/9.
* second type of noodle
*/
public class BeefNoodle extends AbstractNoodle{
public BeefNoodle(Peppery peppery) {
/**super是指的父类本身实例,super()是调用父类的构造方法
所以super也可以指向父类的成员。**/
super(peppery);
}
public void eat() {
System.out.println("这是一碗美味的牛肉面条。"
+ super.style.style());
}
}



/**
* Created by Administrator on 2016/3/9.
* first type of noodle
*/
public class PorkyNoodle extends AbstractNoodle {
public PorkyNoodle(Peppery peppery){
super(peppery);
}
public void eat(){
System.out.println("这是一碗稍嫌油腻的猪肉面条。"
+ super.style.style());
}
}



/**
* Created by Administrator on 2016/3/9.
* make a test
*/
public class Test {
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 PorkyNoodle(
new PepperyStyle());
noodle3.eat();
/**下面将得到“不辣”的猪肉面**/
AbstractNoodle noodle4 = new PorkyNoodle(
new PlainStyle());
noodle4.eat();
}
}
 

以上是关于Bridge的主要内容,如果未能解决你的问题,请参考以下文章