空客(Airbus)、波音(Boeing)、麦道(McDonnell-Douglas)都是飞机制造商,他们都可以生产载客飞机(PassengerPlane)、载货飞机(CargoPlane),请写一段java代码使用桥接模式来描述飞机制造商和他们生产的飞机。
时间: 2024-02-03 11:02:34 浏览: 201
java中飞机类
好的,以下是符合您要求的Java桥接模式代码示例:
```
interface PlaneManufacturer {
void producePlane(PlaneType type);
}
class Airbus implements PlaneManufacturer {
@Override
public void producePlane(PlaneType type) {
if (type == PlaneType.PASSENGER) {
System.out.println("Airbus produces passenger plane.");
} else if (type == PlaneType.CARGO) {
System.out.println("Airbus produces cargo plane.");
}
}
}
class Boeing implements PlaneManufacturer {
@Override
public void producePlane(PlaneType type) {
if (type == PlaneType.PASSENGER) {
System.out.println("Boeing produces passenger plane.");
} else if (type == PlaneType.CARGO) {
System.out.println("Boeing produces cargo plane.");
}
}
}
class McDonnellDouglas implements PlaneManufacturer {
@Override
public void producePlane(PlaneType type) {
if (type == PlaneType.PASSENGER) {
System.out.println("McDonnell-Douglas produces passenger plane.");
} else if (type == PlaneType.CARGO) {
System.out.println("McDonnell-Douglas produces cargo plane.");
}
}
}
enum PlaneType {
PASSENGER, CARGO
}
abstract class Plane {
protected PlaneManufacturer manufacturer;
public Plane(PlaneManufacturer manufacturer) {
this.manufacturer = manufacturer;
}
abstract void produce();
}
class PassengerPlane extends Plane {
public PassengerPlane(PlaneManufacturer manufacturer) {
super(manufacturer);
}
@Override
void produce() {
System.out.print("Passenger plane is produced by ");
manufacturer.producePlane(PlaneType.PASSENGER);
}
}
class CargoPlane extends Plane {
public CargoPlane(PlaneManufacturer manufacturer) {
super(manufacturer);
}
@Override
void produce() {
System.out.print("Cargo plane is produced by ");
manufacturer.producePlane(PlaneType.CARGO);
}
}
public class Main {
public static void main(String[] args) {
PlaneManufacturer airbus = new Airbus();
PlaneManufacturer boeing = new Boeing();
PlaneManufacturer mcdonnellDouglas = new McDonnellDouglas();
Plane passengerPlane = new PassengerPlane(airbus);
Plane cargoPlane = new CargoPlane(boeing);
passengerPlane.produce(); // Passenger plane is produced by Airbus.
cargoPlane.produce(); // Cargo plane is produced by Boeing.
}
}
```
请问您还有其他问题需要我回答吗?
阅读全文