How to implement conditional encapsulation in C #

I was wondering how to conditionally hide data in a class. For example, let's say I have a class called Car that has three fields: Engine, MeterReading and Mileage.

I have three other objects: Driver, Mechanic and Passenger. Now I want to:

The driver should have access only to the space (and not to the engine and counter)

A mechanic should only have access to the engine and the space (not MeterReading)

The passenger should have access only to MeterReading (and not to the engine and mileage)

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

Any ideas guys?

Thanks.

+6
c # encapsulation conditional
source share
4 answers

I would use an explicit interface implementation . It hides the implementation of the interface when accessing an object by its type. Implementation is available only when accessed through the interface. In your example:

interface IUsableByPassenger { MeterReading MeterReading { get; set; } } interface IUsableByDriver { Mileage Mileage { get; set; } } interface IUsableByMechanic : IUsableByDriver { Engine Engine { get; set; } } class Car : IUsableByMechanic, IUsableByPassenger { Mileage IUsableByDriver.Mileage { // implement mileage access } Engine IUsableByMechanic.Engine { // implement engine access } MeterReading IUsableByPassenger.MeterReading { // implement engine access } } class Mechanic { public Mechanic(IUsableByMechanic usable) { // usable.MeterReading is not here } } 
+4
source share

The first idea that came to mind was for your Car class to implement 3 different interfaces that every other class can use to interact with your Car class.

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

 public interface IDriverAccess { double Mileage { get; } } 

The IMechanicAccess interface may be as follows:

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

And so on. Then your car class can implement these interfaces, but the classes for the driver, mechanic, and passenger will simply use the interfaces to interact with the object.

 public Car : IDriverAccess, IMechanicAccess, IPassengerAccess { // implement the interfaces } 
+12
source share

I would create these interfaces:

  public interface IDriviableCar { object Mileage { get; } } public interface IRepairableCar { object Engine { get; } } public interface IHirableCar { object MeterReader { get; } } public class Car : IDriviableCar, IRepairableCar, IHirableCar { private object _mileage; private object _engine; private object _meterReader; public object Mileage { get { return _mileage; } } public object Engine { get { return _engine; } } public object MeterReader { get { return _meterReader; } } } 

And let each user use the interface to access the machine.

+2
source share

make class The car has implemented the interfaces IEngine, ImaterReading and so on. Give each object only access to the selectc interface. They say that the disk was accessed only by IMilage, an IMilage mechanic and IEngine.

+2
source share

All Articles