Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Vending Machine App in Swift Modeling An Interface Laying the Groundwork

David Karlsson
David Karlsson
4,180 Points

Why use protocol for the VendingMachine instead of Class?

Hey, I'm confused as to why we'd make the VendingMachine a protocol and not a class. All the different types of VendingMachines will be VendingMachines as well and the only difference is the VendingSelection, wouldn't it make more sense if VendingMachine was a class and the different types were sub classes?

1 Answer

Ivan Penchev
Ivan Penchev
13,832 Points

This question (which you can google as Class vs Instance) is one that plagues OOP devs for years and for years to come :=) The short answer is: because is the better way to accommodate potential future changes.

I wonder if the example I should provide, should be directly on this project. Nah...lets make it a bit more abstract so that I spark some curiosity and some thinking.

Lets take a downloading example.

You has a Base class FileDownloadModel and have 3 subclasses AudioFileDownloadModel, VideoFileDownloadModel, ImageDownloadModel.

You have a DownloadManager that takes input as FileDownloadModel and uses its urlToDownload Property of model to download the file.

Later down the line you are told that, there is one more model coming but its like UserDownloadModel which subclass from User.

See now it becomes difficult to handle the such scenario where you will have to change a lot of code to incorporate downloading methods.

How protocol oriented programming will help you here:

  1. Create a protocol named DownloadingFileProtocol and add methods that you need for downloading file. eg. urlToDownload, pathToSave, extension etc.

  2. Implement the same protocol in FileDownloadModel and UserDownloadModel. See the benefit that you don't have to change a lot of code in UserDownloadModel. You will just implement the methods from DownloadingFileProtocol.

  3. See if again a new entity comes down the line, you will not change any code just implement the protocol methods.

  4. And now your DownloadManager can take input as DownloadingFileProtocol instead of a specific model and now you can make any model as Downloadable.

David Karlsson
David Karlsson
4,180 Points

Hm, well, it sounds to me like the FileDownloadModel Class would have a protocol for the download method and then just implement the download method differently for all the different file types. But maybe that's incorrect.

Ivan Penchev
Ivan Penchev
13,832 Points

David, in programing nothing is wrong if it works and you can maintain it :)

There are just different approaches :)

David Karlsson
David Karlsson
4,180 Points

Haha, yeah, I guess so but I was wondering about the reasoning behind the teacher choosing to do it this way when he taught us to do it differently in the previous course. Ah well, thanks for the input anyways.

Ivan Penchev
Ivan Penchev
13,832 Points

David Karlsson , I just went back trough the videos, cuz I have free time at work lol, and he actually says the same thing I wrote above, in the https://teamtreehouse.com/library/laying-the-groundwork video around 1.30 minute mark.

Hope that helps.

Also If my answer was somewhat near satisfactory , I would advice to mark it so, in order for future readers having it as a reference.

David Karlsson
David Karlsson
4,180 Points

Haha, well, he just says THAT he is going to use protocols instead of classes, he doesn't really explain why it's a better idea. Doesn't matter I guess, works both ways. I just thought protocols were more to describe stuff something is actually doing and classes is to describe what something is.

To take the analogy from previous courses: Airplanes and cars are both vehicles (class Vehicle) both are startable (protocol Startable) but only the airplane is flyable (protocol Flyable), but then we have birds that aren't a Vehicle at all, but is also Flyable.

All VendingMachines are the same (class VendingMachine) and the only difference is what they contain. If I were to do this task I would make both the VendingMachine and the VendingItem classes, I just wanted to know why that's not the right thing to do. Am I thinking about this all wrong?