public enum VehicleColor {
BLACK,
WHITE,
GRAY,
RED;
}
public abstract class BaseVehicle implements Vehicle {
private VehicleType type;
private VehicleColor color;
public BaseVehicle(VehicleType type , VehicleColor color) {
this.type = type;
this.color = color;
}
@Override
public void specification() {
System.out.println("This is a " + color.name().toLowerCase() + " " + type.name().toLowerCase());
}
}
public interface ICar extends Vehicle {
void showOwner();
}
public class BlackCar extends BaseVehicle implements ICar {
public BlackCar() {
super(VehicleType.CAR, VehicleColor.BLACK);
}
@Override
public void showOwner() {
System.out.println("It's Tom's car");
}
}
public class WhiteCar extends BaseVehicle implements ICar {
public WhiteCar() {
super(VehicleType.CAR, VehicleColor.WHITE);
}
@Override
public void showOwner() {
System.out.println("It's Smith's car");
}
}
public class GrayCar extends BaseVehicle implements ICar {
public GrayCar() {
super(VehicleType.CAR, VehicleColor.GRAY);
}
@Override
public void showOwner() {
System.out.println("It's Annie's car");
}
}
public class RedCar extends BaseVehicle implements ICar {
public RedCar() {
super(VehicleType.CAR, VehicleColor.RED);
}
@Override
public void showOwner() {
System.out.println("It's Zhao's car");
}
}
public interface IBus extends Vehicle {
void showBusNo();
}
public class BlackBus extends BaseVehicle implements IBus {
public BlackBus() {
super(VehicleType.BUS, VehicleColor.BLACK);
}
@Override
public void showBusNo() {
System.out.println("Bus No. 50");
}
}
public class WhiteBus extends BaseVehicle implements IBus {
public WhiteBus() {
super(VehicleType.BUS, VehicleColor.WHITE);
}
@Override
public void showBusNo() {
System.out.println("Bus No. 9");
}
}
public class GrayBus extends BaseVehicle implements IBus {
public GrayBus() {
super(VehicleType.BUS, VehicleColor.GRAY);
}
@Override
public void showBusNo() {
System.out.println("Bus No. 187");
}
}
public class RedBus extends BaseVehicle implements IBus {
public RedBus() {
super(VehicleType.BUS, VehicleColor.RED);
}
@Override
public void showBusNo() {
System.out.println("Bus No. 19");
}
}
public interface ITaxi extends Vehicle {
void showCompany();
}
public class BlackTaxi extends BaseVehicle implements ITaxi {
public BlackTaxi() {
super(VehicleType.TAXI, VehicleColor.BLACK);
}
@Override
public void showCompany() {
System.out.println("This taxi is belong to Didi Limited.");
}
}
public class WhiteTaxi extends BaseVehicle implements ITaxi {
public WhiteTaxi() {
super(VehicleType.TAXI, VehicleColor.WHITE);
}
@Override
public void showCompany() {
System.out.println("This taxi is belong to Nantong Taxi Company");
}
}
public class GrayTaxi extends BaseVehicle implements ITaxi {
public GrayTaxi() {
super(VehicleType.TAXI, VehicleColor.GRAY);
}
@Override
public void showCompany() {
System.out.println("This taxi is belong to Hello Chuxing Company");
}
}
public class RedTaxi extends BaseVehicle implements ITaxi {
public RedTaxi() {
super(VehicleType.TAXI, VehicleColor.RED);
}
@Override
public void showCompany() {
System.out.println("This taxi is belong to Ma Anshan taxi Company");
}
}
工厂模式用于创建一种产品,抽象工厂用于创建同一系列的多个产品。
public interface VehicleFactory {
<T> T createBlackVehicle();
<T> T createWhiteVehicle();
<T> T createGrayVehicle();
<T> T createRedVehicle();
}
public class CarFactory implements VehicleFactory {
@Override
public BlackCar createBlackVehicle() {
return new BlackCar();
}
@Override
public WhiteCar createWhiteVehicle() {
return new WhiteCar();
}
@Override
public GrayCar createGrayVehicle() {
return new GrayCar();
}
@Override
public RedCar createRedVehicle() {
return new RedCar();
}
}
public class BusFactory implements VehicleFactory {
@Override
public BlackBus createBlackVehicle() {
return new BlackBus();
}
@Override
public WhiteBus createWhiteVehicle() {
return new WhiteBus();
}
@Override
public GrayBus createGrayVehicle() {
return new GrayBus();
}
@Override
public RedBus createRedVehicle() {
return new RedBus();
}
}
public class TaxiFactory implements VehicleFactory {
@Override
public BlackTaxi createBlackVehicle() {
return new BlackTaxi();
}
@Override
public WhiteTaxi createWhiteVehicle() {
return new WhiteTaxi();
}
@Override
public GrayTaxi createGrayVehicle() {
return new GrayTaxi();
}
@Override
public RedTaxi createRedVehicle() {
return new RedTaxi();
}
}
public class VehicleCreator {
public static void main(String args[]) {
VehicleFactory carFactory = new CarFactory();
BlackCar blackCar = carFactory.createBlackVehicle();
blackCar.specification();
blackCar.showOwner();
WhiteCar whiteCar = carFactory.createWhiteVehicle();
whiteCar.specification();
whiteCar.showOwner();
GrayCar grayCar = carFactory.createGrayVehicle();
grayCar.specification();
grayCar.showOwner();
RedCar redCar = carFactory.createRedVehicle();
redCar.specification();
redCar.showOwner();
VehicleFactory busFactory = new BusFactory();
BlackBus blackBus = busFactory.createBlackVehicle();
blackBus.specification();
blackBus.showBusNo();
WhiteBus whiteBus = busFactory.createWhiteVehicle();
whiteBus.specification();
whiteBus.showBusNo();
GrayBus grayBus = busFactory.createGrayVehicle();
grayBus.specification();
grayBus.showBusNo();
RedBus redBus = busFactory.createRedVehicle();
redBus.specification();
redBus.showBusNo();
VehicleFactory taxiFactory = new TaxiFactory();
BlackTaxi blackTaxi = taxiFactory.createBlackVehicle();
blackTaxi.specification();
blackTaxi.showCompany();
WhiteTaxi whiteTaxi = taxiFactory.createWhiteVehicle();
whiteTaxi.specification();
whiteTaxi.showCompany();
GrayTaxi grayTaxi = taxiFactory.createGrayVehicle();
grayTaxi.specification();
grayTaxi.showCompany();
RedTaxi redTaxi = taxiFactory.createRedVehicle();
redTaxi.specification();
redTaxi.showCompany();
}
}
This is a black car
It's Tom's car
This is a white car
It's Smith's car
This is a gray car
It's Annie's car
This is a red car
It's Zhao's car
This is a black bus
Bus No. 50
This is a white bus
Bus No. 9
This is a gray bus
Bus No. 187
This is a red bus
Bus No. 19
This is a black taxi
This taxi is belong to Didi Limited.
This is a white taxi
This taxi is belong to Nantong Taxi Company
This is a gray taxi
This taxi is belong to Hello Chuxing Company
This is a red taxi
This taxi is belong to Ma Anshan taxi Company
本文介绍了一种使用工厂模式创建不同颜色的车辆实例的设计方法,并通过具体的Java代码实现了汽车、巴士和出租车的不同特性和创建过程。

被折叠的 条评论
为什么被折叠?



