OOP – object-oriented design principle abstraction

While reading abstraction, I came across the following statement

“Abstraction only captures the details of objects related to the current perspective”

For example.
From the driver’s point of view, the Car class would be

public class Car
{
void start();< br /> void applybrakes();
void changegear();
void stop();
}

From the perspective of a mechanic, the Car class will be

public class Car
{
void changeOil();
void adjustBrakes();
}

< p>My question,
When designing the system, do we design a user perspective (driver or mechanism) or can we design multiple user perspectives and further abstract them based on user types?

I hope my question is clear.

Thank you

Depending on your use case, you may need to design for multiple users. In your example, if your car will be used by mechanics and drivers, then you cannot ignore a group of users. In this case, you You can still use Interfaces to abstract the details.

You can design your object like this:

interface IDrivable {
void start( );
void applyBrakes();
void changeGear();
void stop();
}

interface IFixable {
void changeOil();
void adjustBrakes();
}

public class Car: IDrivable, IFixable {
// implement all the methods here
}

Now, when a mechanic wants a car, instead of giving him a Car object, you give him an IFixable object. Similarly, the driver obtains an IDrivable object. This can keep two groups of users at the same time Related abstraction.

class Driver {

private IDrivable car;

public Driver(IDrivable car) {
this.car = car;
}

public driveCar() {
this.car.start();
this.car.accelerate();< br />
//this is invalid because a driver should not be able to do this< br /> this.car.changeOil();
}
}

Similarly, mechanics will not be able to access the methods in the interface IDrivable.

You You can read more about interfaces here. Even though this is an MSDN link and uses C#, all major languages ​​support interfaces.

When reading abstraction, I came across The following statement

“The abstraction only captures the details of the object related to the current perspective”

For example.
From the driver’s perspective, the Car class would be< /p>

public class Car
{
void start();
void applybrakes();
void changegear();
void stop();
}

From the perspective of a mechanic, the Car class would be

public class Car
{
void changeOil();
void adjustBrakes();
}

My question,
When designing the system, we are designing a user Perspective (driver or mechanism) or can we design multiple user perspectives and further abstract them based on user types?

I hope my question is clear.

Thank you

Depending on your usage, you may need to Multiple user design. In your example, if your car will be used by mechanics and drivers, then you can’t ignore a group of users. In this case, you can still use Interfaces to abstract the details.

p>

You can design your object like this:

interface IDrivable {
void start();
void applyBrakes();< br /> void changeGear();
void stop();
}

interface IFixable {
void changeOil();
void adjustBrakes() ;
}

public class Car: IDrivable, IFixable {
// implement all the methods here
}

Now, as a mechanic When you want a car, you don’t give him a Car object, but an IFixable object. Similarly, the driver obtains an IDrivable object. This can retain the relevant abstractions of the two groups of users at the same time.

p>

class Driver {

private IDrivable car;

public Driver(IDrivable car) {
this.car = car;
}

public driveCar() {
this.car.start();
this.car.accelerate();

//this is invalid because a driver should not be able to do this
this.car.changeOil();
}
}

Similarly, mechanics will not be able to access the methods in the interface IDrivable.

You can read more about interfaces here. Even if this is an MSDN link and uses C#, all major languages ​​support Interface.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2784 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.