开发者

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开发者_JAVA百科 basing the whole logic on if else statements ) ?

Any ideas guys ?

Thanks.


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
}


I would use explicit interface implementation. It hides implementation of interface when object is accessed by its type. The implementation is accessible only when accessing by 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
    }
}


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 person use the interface to access the car.


make class Car implemented interfaces IEngine,ImaterReading and so. Give each entity only specifyc interface access. say a drive got IMilage access only, a mechanic IMilage and IEngine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜