public abstract class AbstractPhone {
protected OperatingSystem os;
public OperatingSystem getOs() {
return os;
}
public void setOs(OperatingSystem os) {
this.os = os;
}
* 开机
*/
public abstract void startUp();
}
public interface OperatingSystem {
void loadCore();
void loadMoudleTree();
void done();
}
public class IOS implements OperatingSystem {
@Override
public void loadCore() {
System.out.println("加载IOS内核");
}
@Override
public void loadMoudleTree() {
System.out.println("加载IOS核心模块树");
}
@Override
public void done() {
System.out.println("Welcome-Assembled in China.Designed by Apple in California!");
}
}
public class TouchWizAndroid implements OperatingSystem {
@Override
public void loadCore() {
System.out.println("Samsung加载C4核心");
}
@Override
public void loadMoudleTree() {
System.out.println("Samsung预热雷管模块");
}
@Override
public void done() {
System.out.println("Samsung随身Bomb加载完毕");
}
}
public class OSFactory {
private OSFactory(){};
private static class OSFactoryImpl{
static OSFactory factory=new OSFactory();
}
public static OSFactory instance(){
return OSFactoryImpl.factory;
}
public OperatingSystem getOS(String osKey){
if(osKey.equalsIgnoreCase("ios")){
return new IOS();
}
if(osKey.equalsIgnoreCase("samsung")){
return new TouchWizAndroid();
}
return null;
}
}
public class IPhone extends AbstractPhone{
@Override
public void startUp() {
setOs(OSFactory.instance().getOS("ios"));
System.out.println("硬件自检");
os.loadCore();
os.loadMoudleTree();
os.done();
System.out.println("enjoy!");
}
}
public class SamsungPhone extends AbstractPhone {
@Override
public void startUp() {
setOs(OSFactory.instance().getOS("samsung"));
System.out.println("检测炸弹状态");
os.loadCore();
os.loadMoudleTree();
os.done();
System.out.println("炸弹启动完毕");
}
}
public class App {
public static void main(String[] args) {
* 桥接模式:适用于多个维度的互相变化(本例中:手机的启动,不同操作系统的启动)
* 抽象部分与实现部分分离,使他们可以各自独立的变化
*/
AbstractPhone iphone=new IPhone();
AbstractPhone samsung=new SamsungPhone();
iphone.startUp();
samsung.startUp();
}
}