[From the last episode: Abstraction is a way to simplify details.]
We’re going to dive into the world of softwareIn this context, "software" refers to functions in an IoT device that are implemented by running instructions through some kind of processor. It's distinct from "hardware," where functions are built into a silicon chip or some other component. a little bit. Hopefully not too nerdy, but what we’re going to talk about is important when thinking about how different IoTThe Internet of Things. A broad term covering many different applications where "things" are interconnected through the internet. devices talk to each other.
We saw that we can simplify the details of how something works using abstractionFor our purposes, it’s the removal of all unnecessary details, leaving only what’s required for operation.. Software programmers have always used abstraction, and, in some cases, it becomes an official software design style.
Creating Objects in the House
It may sound crazy for software, but many programmers think in terms of creating objectsWhen talking about programs, these are “things” that can be created in a way that encapsulates the details, letting them be accessed from outside through an interface.. Yes, they’re not building anything physical; they’re writing a program that will run on some kind of computer. But the notion of creating and managing objects through a style called object-oriented programmingA programming style where the programmer creates and manipulates objects to achieve a goal. is a really powerful way of building software that is flexible and less likely to have bugs. (It’s impossible to guarantee no bugs…)
This comes through decomposing complex problems down into components and treating each component as an object. Let’s do an imaginary exercise, pretending that we’re going to try to control an entire house and everything in it in one program. We’ll assume that we can do anything we want to do automatically through a software program. Said a different way, we’ll be writing code that controls the house.
Well, we start by defining the house as an object. There are lots of details of what’s inside the house, which we’ll get to, but, from the outside, there’s not a lot you do to the house itself. Mostly, you may need to lock, unlock, open, or close the house. Let’s play with that.
This house will most likely be entered through a door. A door is then defined as another object, and the house contains one (or more). The door might be metal, wood, screen, sliding, hinged… lots of possibilities. But in terms of what you might want to do to a door under normal circumstances, there are really only four things: lock, unlock, open, and close.
Too Many Details
If we’re writing our software, we can write code that covers all the different types of door. But here’s the thing: for those four things that we do with a door, we don’t need to know the details if the software is going to do the work for us. All we need to know is lock, unlock, open, and close. The details of the “door code” will have to say whether, for example, you slide the door or swing the door to open or close it. But for someone who doesn’t care, we just want to be able to say, “Open the door” and have it happen.
Thinking this through a bit more, you know, we really only open and close doors. We lock and unlock locks. Why does that make a difference? Because some doors have no lock, some have one lock, and some have a lock and a deadbolt. Some have keypads; others use old-fashioned keys. Again, more details.
So we could define a lock as an object. It has only two instructions: lock and unlock. Let’s take the case of a door with two locks. To unlock it properly, we have to unlock both the main lock and the deadbolt. So our “door code” could issue instructions like, “Unlock the main lock” and “Unlock the deadbolt.”
“Just Unlock the Door!”
Now… we might not want to care exactly how any door gets unlocked, so we still say we unlock the door — but when we do that, the “door code” knows that the door has two locks, and it translates the instruction, “Unlock the door” into two lower-level instructions: “Unlock the main lock” and “Unlock the deadbolt.”
We can keep going, defining, for instance, windows as objects. The house will have some windows; the door may even have a window. Some are casement windows; some can’t be opened at all; some slide left and right. Some can be locked. Again, we probably have lock, unlock, open, and close as things we can do to the window. And so on for the rest of the house in as much or little detail as we want.
In the next post, we’ll see how to make use of this kind of abstraction.
Leave a Reply