public class BridgeDesignPattern {
public static void main(String[] args) {
Sport pingPangBall = new PingPangBall(new China());
Sport basketBall = new BasketBall(new USA());
pingPangBall.win();
basketBall.win();
}
}
interface Country {
String countryName();
}
class China implements Country {
@Override
public String countryName() {
return "中国";
}
}
class USA implements Country {
@Override
public String countryName() {
return "美国";
}
}
abstract class Sport {
protected Country country;
public Sport(Country country) {
this.country = country;
}
abstract void win();
}
class BasketBall extends Sport {
public BasketBall(Country country) {
super(country);
}
@Override
void win() {
System.out.println("东京奥运会," + country.countryName() + "在篮球项目获得了金牌");
}
}
class PingPangBall extends Sport {
public PingPangBall(Country country) {
super(country);
}
@Override
void win() {
System.out.println("东京奥运会," + country.countryName() + "在乒乓球项目获得了金牌");
}
}