3

I have two different classes namely Car and Radio. How I can make sure that class Radio can be only used by class Car? Or is there better implementation here?

I'm coding in c#.

class Car
{
    // car has a radio
}  


class Radio
{
    // some properties

        // some methods
        void TurnOn(bool onOff) { }        
}

Thanks in advance.

6
  • 3
    What do you mean only be used by? You could make Radio a private class within Car. Commented Sep 1, 2011 at 15:35
  • lol, so u wrote the instruction as comment and another guy wrote the implementation as answer. U get my vote :) Commented Sep 1, 2011 at 15:38
  • @Shawn: perfectly acceptable ... Commented Sep 1, 2011 at 15:41
  • There may be a "better" way to do this but we'd need to know more information. Can a car only ever have the radio it came with? Is it not a requirement that a new radio can be fitted into your car? Depending on how you're conceptualising this will determine the best route to go. Commented Sep 1, 2011 at 15:41
  • @jamie - both can be applicable. I'll be glad to know how would you implement the latter of what you've said. Commented Sep 1, 2011 at 15:55

7 Answers 7

6
class Car {

  public Car() {
    myRadio = new Radio();
  }

  private class Radio {
    void TurnOn(bool onOff) {}
  }
}
Sign up to request clarification or add additional context in comments.

Comments

4

One way would be to enforce the dependency that a Radio must belong to a Car via the constructor:

class Radio
{
    Car owner;

    // constructor
    public Radio(Car car)
    {
        owner = car;
    }

    // some properties

    // some methods
    void TurnOn(bool onOff) { }        
}

Another way would be to nest the Radio object in the Car object. Typically, this would mean that Radio should not be directly accessed by any objects outside of Car. By nesting, this would look like:

class Car 
{

    private class Radio 
    {
    }

    // add methods to affect the Radio object
}

Comments

1
class Car
{
    class Radio
    {
        // some properties

        // some methods
        void TurnOn(bool onOff) { }        
    }        // car has a radio
}  

This makes it so that no class, other than Car, can see the Radio class. Only something IN car can now with it on or off.

Comments

0

Radio would be a logical modeling if you wanted to use the radio in other places. If you only want to use whats provided by Radio in Car then maybe its not deserving of a class yet and you are overdesigning here. If you do want to limit the Radio to be only usable in Car you could declare radio in the class of car. Its not great but its an answer :P

public class Car
{
    class Radio
    {
        public void TurnOn()
        {
            // do stuff
        }
    }

    public Car()
    {
                    r = new Radio();
        r.TurnOn();
    }
}

public class NotCar
{
    // i cant use radio
}

Comments

0

thats an odd question given Car and Radio concepts ( why cannot someone else use Radio) but answer is you can use Radio as nested type with private access like

class Car
{ 
  Radio m_Radio = new Radio();
  private class Radio {...}
}

Notice that this way you cannot expose Radio to outside world through any part of Car public interface so all work with Car.Radio will have to be done mediately through Car class, which sounds like a not very good design to me.

Comments

0

Try this

class Car 
{
   class Radio 
   {
      void TurnOn(bool onOff) { } 
   }
}

The class Radio will be private and accessible only by the class Car

Comments

0

I have read all the above answers, All good but I would like to try another approach to achieve a car with radio by considering a car which can either be fitted with Generic Radio OR a boss radio. I have used Interface , dependency injection principle, Polymorphism and Inheritance concept to achieve this. In this approach we have implemented the Generic and Boss radio from IRadio interface. Please see below piece of code for reference

namespace CarAndRadioProgramExample
{
    class Program
    {
        static void Main(string[] args)
        {
            GeneralRadio gRadio = new GeneralRadio();
            gRadio.MaximumVolume = 100;
            gRadio.MinimumVolume = 0;
            Car carWithGRadio = new Car(gRadio);
            carWithGRadio.SwitchOnCarRadio();

            BossRadio bRadio = new BossRadio();
            bRadio.MaximumVolume = 200;
            bRadio.MinimumVolume = 0;
            Car carWithBRadio = new Car(bRadio);
            carWithBRadio.SwitchOnCarRadio();

            Console.ReadLine();
        }
    }

    class Car
    {
        IRadio carRadio;
        public Car(IRadio radioObject)
        {
            carRadio = radioObject;
        }

        internal void SwitchOnCarRadio()
        {
            carRadio.RadioOn();
        }
    }

    interface IRadio
    {
        int MaximumVolume { get; set; }
        int MinimumVolume { get; set; }
        void RadioOn();
    }
   class GeneralRadio :IRadio
    {
        public int MaximumVolume
        {
            get;
            set;
        }
       public int MinimumVolume
        {
            get;
            set;
        }

        public void RadioOn()
        {
            Console.WriteLine("Switching On Generic Radio with maximum volume" + MaximumVolume + "and minimum volume" + MinimumVolume);
        }
    }

    class BossRadio : IRadio
    {
        public int MaximumVolume
        {
            get;
            set;
        }
        public int MinimumVolume
        {
            get;
            set;
        }

        public void RadioOn()
        {
            Console.WriteLine("Switching On Boss Radio with maximum volume" + MaximumVolume + "and minimum volume" + MinimumVolume);
        }
    }

}

When running the above code, the output going to be enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.