ブリッジパターンを正しく理解していますか:
前:
public class Main2 {
@SuppressWarnings("unused")
public static void main(String[] args) {
Car car11 = new BadNativeCar();
Car car12 = new GoodNativeCar();
Car car21 = new BadForeignCar();
Car car22 = new GoodForeignCar();
}
}
interface Car{
public void drive();
public void stop();
}
class NativeCar implements Car{
@Override
public void drive() {
}
@Override
public void stop() {
}
}
class ForeignCar implements Car{
@Override
public void drive() {
}
@Override
public void stop() {
}
}
class GoodNativeCar extends NativeCar{
}
class BadNativeCar extends NativeCar{
}
class GoodForeignCar extends ForeignCar{
}
class BadForeignCar extends ForeignCar{
}
後 (ブリッジ):
public class Main2 {
public static void main(String[] args) {
BadCar badCar = new BadCar();
GoodCar goodCar = new GoodCar();
CarAbstraction car11 = new NativeCar(badCar);
CarAbstraction car12 = new NativeCar(goodCar);
CarAbstraction car21 = new ForeignCar(badCar);
CarAbstraction car22 = new ForeignCar(goodCar);
}
}
interface CarAbstraction{
public void drive();
public void stop();
}
//Abstraction
abstract class CarAbstractionImpl implements CarAbstraction{
private CarImplementor carImplementor;
public CarAbstractionImpl(CarImplementor carImplementor) {
this.carImplementor = carImplementor;
}
@Override
public void drive() {
carImplementor.drive();
}
@Override
public void stop() {
carImplementor.stop();
}
}
//RefinedAbstraction1
class NativeCar extends CarAbstractionImpl{
public NativeCar(CarImplementor carImplementor) {
super(carImplementor);
}
}
//RefinedAbstraction2
class ForeignCar extends CarAbstractionImpl{
public ForeignCar(CarImplementor carImplementor) {
super(carImplementor);
}
}
//Implementor
interface CarImplementor extends CarAbstraction{
}
//ConcreteImplementor1
class GoodCar implements CarImplementor{
@Override
public void drive() {
}
@Override
public void stop() {
}
}
//ConcreteImplementor2
class BadCar implements CarImplementor{
@Override
public void drive() {
}
@Override
public void stop() {
}
}