桥接模式

package org.designPattern.bridge15;

public class Bus extends Car{

    public Bus(Engine engine) {
        super(engine);
    }

    public void installEngine() {
        System.out.print("Bus£º");
        this.getEngine().installEngine();
    }
}
package org.designPattern.bridge15;

public abstract class Car {
    private Engine engine;
    
    public Car(Engine engine) {
        this.engine = engine;
    }
    
    public Engine getEngine() {
        return engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public abstract void installEngine();
}

package org.designPattern.bridge15;

public interface Engine {
    public void installEngine();
}

package org.designPattern.bridge15;

public class Engine2000 implements Engine {

    public void installEngine() {
        System.out.println("安装Engine2000");
    }

}

package org.designPattern.bridge15;

public class Engine2200 implements Engine{

    public void installEngine() {
        System.out.println("安装Engine2200");
    }

}

package org.designPattern.bridge15;

public class Jeep extends Car {

    public Jeep(Engine engine) {
        super(engine);
    }

    public void installEngine() {
        System.out.print("Jeep");
        this.getEngine().installEngine();
    }

}

package org.designPattern.bridge15;

public class MainClass {
    public static void main(String[] args) {
        
        Engine engine2000 = new Engine2000();
        Engine engine2200 = new Engine2200();
        
        Car car1 = new Bus(engine2000);
        car1.installEngine();
        
        Car car2 = new Bus(engine2200);
        car2.installEngine();
        
        Car jeep1 = new Jeep(engine2000);
        jeep1.installEngine();
        
        Car jeep2 = new Jeep(engine2200);
        jeep2.installEngine();
        
    }
}



你可能感兴趣的:(桥接模式)