package BridgeModel;
public class BridgeModel1 {
/**
* @param args
*/
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);
}
}
abstract class Clothing{
public abstract void personDressCloth(Person person);
}
class Jacket extends Clothing{
@Override
public void personDressCloth(Person person) {
// TODO Auto-generated method stub
System.out.println(person.getType()+"穿夹克");
}
}
class Trouser extends Clothing{
@Override
public void personDressCloth(Person person) {
// TODO Auto-generated method stub
System.out.println(person.getType()+"穿裤子");
}
}
abstract class Person{
private Clothing clothing;
private String type;
public Clothing getClothing() {
return clothing;
}
public void setClothing(Clothing clothing) {
this.clothing = clothing;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public abstract void dress();
}
class Man extends Person{
public Man(){
setType("男人");
}
@Override
public void dress() {
// TODO Auto-generated method stub
Clothing clothing=getClothing();
clothing.personDressCloth(this);
}
}
class Lady extends Person{
public Lady(){
setType("女人");
}
@Override
public void dress() {
// TODO Auto-generated method stub
Clothing clothing=getClothing();
clothing.personDressCloth(this);
}
}
设计模式之桥接模式
最新推荐文章于 2024-05-05 20:59:58 发布