Skip to content

Instantly share code, notes, and snippets.

@AdamSheaffer
Last active July 15, 2019 02:34
Show Gist options
  • Save AdamSheaffer/ec09a8b8a1edabbfc4d2ca7f8719abd7 to your computer and use it in GitHub Desktop.
Save AdamSheaffer/ec09a8b8a1edabbfc4d2ca7f8719abd7 to your computer and use it in GitHub Desktop.
Intro to C# interfaces and the merits of composition over inheritance

Classy Electronics

You've just started working for a boutique electronic company and you've been tasked with modeling their new line of record players.

Step 1:

The company's most basic product line is simple turntables that can support rpm speeds of 33, 45, and 78 Create a TurnTable class with these properties and methods:

Properties
  • A double called CurrentSpeed
  • A boolean property called IsPlaying that tracks whether the motor is running
Methods
  • An Play method that sets the IsPlaying property to true
  • A Stop method that sets the IsPlaying property to false

Step 2:

The company has decided to start a line of integrated turntables. They'd like a new product that has speakers built right into the record players This product has the same turntable built in, so create a subclass of Turntable with these additional properties and Methods:

Properties
  • An int called Volume
Methods
  • A method called VolumeUp that increments Volume by 1
  • A method called VolumeDown that decrements Volume by 1

Step 3:

Research has shown that these all-in-one record players are a big hit. The company wants to introduce a new product. It still has the turntable and speakers, but this time they want to have a built in AM/FM radio. Create a subclass of your previous class with the following properties and methods:

Properties
  • A double called CurrentFrequency
  • A List of doubles called FavoriteStations
Methods
  • A method called AddFavorite that adds a new station to FavoriteStations
  • A method called RemoveFavorite that removes a station from FavoriteStations

Step 4:

These all-in-one products are selling like hotcakes! But R&D has determined that customers are also wanting built-in CD players. Create yet another subclass that has the following properties and methods:

Properties
  • An integer value called TotalTracks
  • An integer value called CurrentTrack
Methods
  • A method called NextTrack that increments CurrentTrack by 1
  • A method called PreviousTrack that decrements CurrentTrack by 1

Step 5:

Turns out a lot of people still listen to CDs! But those same customers don't always listen to vinyl. So the company would like to release a product that is just a CD player with built-in speakers. Create a class that has a CD player and speakers but doesn't have the radio or turntable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment