Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement conditional encapsulation in C#

I was wondering how one can conditionally hide data in class. For instance , lets say I have a class called Car which has three fields : Engine , MeterReading and Mileage.

I have three other entities called : Driver , Mechanic and Passenger. Now what I want is that :

A Driver should only be able to access Mileage ( and not Engine and MeterReading)

A Mechanic should only be able to access Engine and Mileage( and not MeterReading)

A Passenger should only be able to access MeterReading ( and not Engine and Mileage )

What could be the best way to implement this ..( without basing the whole logic on if else statements ) ?

Any ideas guys ?

Thanks.

like image 550
Sennin Avatar asked Oct 14 '10 11:10

Sennin


People also ask

How can you implement encapsulation in C?

The process of implementing encapsulation can be sub-divided into two steps: The data members should be labeled as private using the private access specifiers. The member function which manipulates the data members should be labeled as public using the public access specifier.

How is encapsulation implemented using a class explain with example?

In encapsulation, a class's variables are hidden from other classes and can only be accessed by the methods of the class in which they are found. Encapsulation in Java is an object-oriented procedure of combining the data members and data methods of the class inside the user-defined class.

Can we use encapsulation in C?

This concept of encapsulation is used in C language for data hiding and protection. It can be implemented when the main calling program has an object, the object should be able to find the functions applicable and in the same way, they find the data.

What is encapsulation in C with example?

In general, encapsulation is a process of wrapping similar code in one place. In C++, we can bundle data members and functions that operate together inside a single class. For example, class Rectangle { public: int length; int breadth; int getArea() { return length * breadth; } };


1 Answers

The first idea that came to mind would be to have your Car class implement 3 different interfaces which each other class can use to interact with your Car class.

For example, (and my names can definitely be improved upon, but you should get the idea), the IDriverAccess interface could be as follows:

public interface IDriverAccess
{
  double Mileage { get; }
}

The IMechanicAccess interface could be as follows:

public interface IMechanicAccess
{
  EngineObject Engine { get; set; }

  double Mileage { get; }
}

And so on. Your car class can then implement these interfaces, but the classes for the driver, mechanic, & passenger will just use the interfaces to interact with the object.

public Car : IDriverAccess, IMechanicAccess, IPassengerAccess
{
  // implement the interfaces
}
like image 96
Eric Olsson Avatar answered Sep 28 '22 21:09

Eric Olsson