码迷,mamicode.com
首页 > 其他好文 > 详细

Bridge

时间:2016-03-26 13:57:45      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/41ZZY/p/5322731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!