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);}}
?