네, 이해했습니다. Car를 클래스에서 인터페이스로 변경하고, 그것을 구현하는 여러 클래스를 만드는 구현체 추출 기법을 적용해 보겠습니다. 이 과정을 단계별로 보여드리겠습니다.
먼저, 초기 Car 클래스부터 시작하여 단계별로 변경 과정을 보여드리겠습니다.
Car 클래스:public class Car {
private String model;
private int speed;
public Car(String model) {
this.model = model;
this.speed = 0;
}
public void accelerate() {
speed += 10;
}
public void brake() {
speed = Math.max(0, speed - 10);
}
public String getModel() {
return model;
}
public int getSpeed() {
return speed;
}
}
Car 클래스를 복사하여 ProductionCar 클래스 생성:public class ProductionCar {
private String model;
private int speed;
public ProductionCar(String model) {
this.model = model;
this.speed = 0;
}
public void accelerate() {
speed += 10;
}
public void brake() {
speed = Math.max(0, speed - 10);
}
public String getModel() {
return model;
}
public int getSpeed() {
return speed;
}
}
Car 클래스를 인터페이스로 변경:public interface Car {
void accelerate();
void brake();
String getModel();
int getSpeed();
}
ProductionCar가 Car 인터페이스를 구현하도록 수정:public class ProductionCar implements Car {
private String model;
private int speed;
public ProductionCar(String model) {
this.model = model;
this.speed = 0;
}
@Override
public void accelerate() {
speed += 10;
}
@Override
public void brake() {
speed = Math.max(0, speed - 10);
}
@Override
public String getModel() {
return model;
}
@Override
public int getSpeed() {
return speed;
}
}
ElectricCar, SportsCar):public class ElectricCar implements Car {
private String model;
private int speed;
private int batteryLevel;
public ElectricCar(String model) {
this.model = model;
this.speed = 0;
this.batteryLevel = 100;
}
@Override
public void accelerate() {
speed += 15;
batteryLevel -= 2;
}
@Override
public void brake() {
speed = Math.max(0, speed - 15);
batteryLevel -= 1;
}
@Override
public String getModel() {
return model;
}
@Override
public int getSpeed() {
return speed;
}
public int getBatteryLevel() {
return batteryLevel;
}
}
public class SportsCar implements Car {
private String model;
private int speed;
public SportsCar(String model) {
this.model = model;
this.speed = 0;
}
@Override
public void accelerate() {
speed += 20;
}
@Override
public void brake() {
speed = Math.max(0, speed - 20);
}
@Override
public String getModel() {
return model;
}
@Override
public int getSpeed() {
return speed;
}
}
public class CarTest {
public static void main(String[] args) {
Car productionCar = new ProductionCar("Sedan");
Car electricCar = new ElectricCar("Tesla Model 3");
Car sportsCar = new SportsCar("Ferrari");
testCar(productionCar);
testCar(electricCar);
testCar(sportsCar);
}
public static void testCar(Car car) {
System.out.println("Testing " + car.getModel());
car.accelerate();
System.out.println("Speed after acceleration: " + car.getSpeed());
car.brake();
System.out.println("Speed after braking: " + car.getSpeed());
System.out.println();
}
}
이렇게 구현체 추출 기법을 적용하여 Car 클래스를 인터페이스로 변경하고, 여러 구현 클래스를 만들었습니다. 이제 Car 인터페이스를 구현하는 ProductionCar, ElectricCar, SportsCar 등 다양한 종류의 자동차 클래스를 만들 수 있게 되었습니다.
이 구조의 장점은 다음과 같습니다:
Car 인터페이스를 사용하여 다양한 자동차 객체를 동일한 방식으로 다룰 수 있습니다.