OOP & Candy Factory
In the introduction, we shared that Object Oriented Programming (OOP) is a paradigm for how we can organize code. Without seeing examples, that definition just feels… unhelpful. In this section, we won’t dig into code quite yet, but we’ll do some thinking and list making to set ourselves up to write code in the next section!
Candy Factory
Let’s think about some components of the candy factory that makes the conversation hearts. We may make some stuff up, but this analogy should help us build context for what OOP is.
- The factory has machinery that is designed to make LOTS of chocolate candies (think snickers, twix, kit-kat, etc…)
- All candy should be chocolate-based
- The size and type of candy could change
With the information above, in theory, the manager at the factory should be able to say “make me a fun-size snickers”, and that should be possible.
That example about the Candy Factory tie directly to concepts of OOP. We will use three main technical terms today. They are below, with the connection to the Candy Factory example:
class
- A class is like a blueprint or template. The factory machines are designed to make a certain sized heart shape candy with a certain material - that is the template for ALL candies. We could probably name it the Candy class.instance
- An instance is an object that is made from the class template. The fun-sized snickers that the manager requested, then later held in her hand, are instances of the Candy class.attribute
- An attribute is a specific characteristic about an instance that has the potential of varying from other instances. With our candies, size and type would be attributes, because they weren’t programmed into the template, but were extra information the manager was able to give.
Classes and Objects - At Home!
Pick an object that you see sitting around in your home. It could be a candle, water bottle, glove, anything!
Jot down the following, and be ready to share out with the group:
- What type of object is it? i.e., what might the class name be?
- Are there multiple instances of it in your home, or in the world? List a few.
- What are some of its attributes?
Are you still wondering how in the world this connects to writing software? That’s what we’ll discuss next!