Question 4 4 : Dependency Inversion Principle ( 2 5 ( 2 5 marks + 1 0
Fantastic news! We've Found the answer you've been seeking!
Question:
Question 4: Dependency Inversion Principle (25 marks +10 optional marks)
Dependency Inversion Principle: High−level modules should not depend upon low level modules. Both should depend upon abstraction.
Suppose a car management system builds a new feature that enables customers to control cars remotely. In order to implement the new functionality, we create a Car class and a RemoteControl class. The Car class will allow users to lock the car and unlock the car. The RemoteControl class will let users click the remote device which is called the function lock and unlock the car. The following code is an implementation of the system.
package Dependency_Inversion01.Bad;
public class Car {
private String color;
public Car(String color){
this.color = color;
}
public void lockCar(){
System.out.println("locked");
}
public void unLockCar(){
System.out.println("unlocked");
}
}
package Dependency_Inversion01.Bad;
public class RemoteControl {
private Car car;
public RemoteControl(Car car){
this.car = car;
}
public void ClickOff(){
car.lockCar();
}
public void clickOn(){
car.unLockCar();
}
}
1/ Explain why the design is violating the Dependency Inversion class is the Principle (15marks)?(Hint:define which high−level module and which one is low−level module)
2/ This violation becomes clear when the class to turn system asks us to enable customer to re−use the RemoteControl on and off other devices such as Motorbike.
− In this case, implementa Motorbike class and modify the RemoteControl class
so that it can (without using interface) control the Motorbike. (5marks)
− Explain which principle would be violated here?(5 marks)
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date: