首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Java桥接方式(Bridge)

2012-07-04 
Java桥接模式(Bridge)概念性的东西就不说了,毕竟自己理解还不够透彻,所以前面一段直接删了。?简单的设计图:

Java桥接模式(Bridge)

概念性的东西就不说了,毕竟自己理解还不够透彻,所以前面一段直接删了。

?

简单的设计图:

?

Java桥接方式(Bridge)

?

代码的实现:

?

package bridge;public abstract class Clothing {/** * 抽象方法 * @param person */public abstract void dressCloth(Person person);}

?

package bridge;public class Jacket extends Clothing {@Overridepublic void dressCloth(Person person) {// TODO Auto-generated method stubSystem.out.println(person.getType() + "穿上马甲!");}}

?

package bridge;public class Trouser extends Clothing {@Overridepublic void dressCloth(Person person) {// TODO Auto-generated method stubSystem.out.println(person.getType() + "穿上裤子!");}}

?

package bridge;public abstract class Person {private String type;private Clothing clothing;public String getType() {return type;}public void setType(String type) {this.type = type;}public Clothing getClothing() {return clothing;}public void setClothing(Clothing clothing) {this.clothing = clothing;}//抽象方法public abstract void dress();}

?

package bridge;public class Man extends Person {public Man(){setType("男人");}@Overridepublic void dress() {getClothing().dressCloth(this);}}

?

package bridge;public class Woman extends Person {public Woman(){setType("女人");}@Overridepublic void dress() {getClothing().dressCloth(this);}}

?

package bridge;public class ClientTest {/** * @param args */public static void main(String[] args) {//两类人Person man = new Man();                Person lady = new Woman();        //两种衣服的类        Clothing jacket = new Jacket();                Clothing trouser = new Trouser();        jacket.dressCloth(man);        trouser.dressCloth(man);        jacket.dressCloth(lady);        trouser.dressCloth(lady);}}

?

热点排行