桥接模式

桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立地变化。以下是例子:

 

public class Jacket extends Clothing{

public void personDressCloth(Person person) {

System.out.println(person.getType()+"穿马甲"); } }

public class Trouser extends Clothing{

public void personDressCloth(Person person) {
System.out.println(person.getType()+"穿裤子");}}

public abstract class Person {

private Clothing clothing;
private String type;
public Clothing getClothing(){
return clothing;
}
public void setType(String type){
this.type = type;
}
public String getType(){
return this.type;
}
public abstract void dress();
}

public class Lady extends Person{  

public Lady(){

setType("女人"); }

public void dress() {

Clothing clothing = getClothing();

clothing.personDressCloth(this); } }

public class Man extends Person{  

public Man(){

setType("男人"); }

public void dress() {

Clothing clothing = getClothing();

clothing.personDressCloth(this); } }

 public class Test {

/**
* 将抽象部分与它的实现部分分离,使它们都可以独立地变化。
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person man = new Man();
Person lady = new Lady();
Clothing jacket = new Jacket();
Clothing trouser = new Trouser();
jacket.personDressCloth(man);
trouser.personDressCloth(man);
jacket.personDressCloth(lady);
trouser.personDressCloth(lady);
}
}
原文地址:https://www.cnblogs.com/lee0oo0/p/2513410.html