public interface Engine {
boolean isStart();
}
class CarEngine implements Engine {
@Override
public boolean isStart() {
return true;
}
}
class Car {
public void start() {
Engine engine = new CarEngine();
if (engine.isStart()) {
System.out.println("Start!");
}
}
}
class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
public void start() {
if (engine.isStart()) {
System.out.println("Start!");
}
}
}
Car car = new Car(new CarEngine());
Car car = new Car();
car.setEngine(new CarEngine());
class Car {
private Engine engine = new CarEngine();
...
}