public class ApplePhone extends Phone {
public ApplePhone(Color color){
super.setColor(color);
}
@Override
public void run() {
color.useColor();
System.out.println("苹果手机");
}
}
public class BlueColor implements Color {
@Override
public void useColor() {
System.out.println("蓝色");
}
}
public interface Color {
void useColor();
}
public class HWPhone extends Phone {
public HWPhone(Color color){
super.setColor(color);
}
@Override
public void run() {
color.useColor();
System.out.println("华为手机");
}
}
public abstract class Phone {
/**
* 通过组合的方式来桥接其他行为
*/
protected Color color;
public void setColor(Color color) {
this.color = color;
}
/**
* 手机的方法
*/
abstract public void run();
}
public class RedColor implements Color {
@Override
public void useColor() {
System.out.println("红色");
}
}
public class SXPhone extends Phone {
public SXPhone(Color color){
super.setColor(color);
}
@Override
public void run() {
color.useColor();
System.out.println("三星手机");
}
}
public class YellowColor implements Color {
@Override
public void useColor() {
System.out.println("黄色");
}
}
HWPhone blueHwPhone = new HWPhone(new BlueColor());
blueHwPhone.run();
HWPhone redHwPhone = new HWPhone(new RedColor());
redHwPhone.run();
ApplePhone applePhone = new ApplePhone(new RedColor());
applePhone.run();