Object-Oriented Programming with TypeScript — Abstraction

Gabriel Meireles
dev.meireles
Published in
3 min readApr 4, 2022
Object-Oriented Programming with TypeScript — Abstraction
Photo by Luca Bravo on Unsplash

What is Abstraction?

Abstraction is one of the concepts of Object-Oriented Programming aka OOP, it’s the capability to remove unnecessary or even complex characteristics from something aiming to keep just the crucial aspects. I know it really looks like: WTH!? But let’s exemplify and simplify it, I won’t use the car example, I promise!

How about a soda fountain machine? It might be a good example of abstraction. To get a soda from a soda fountain machine you don’t need to know that in a soda fountain machine made your drink on-the-fly when you press the dispenser tab. A pressurized carbon dioxide tank and a water pump send pressurized carbon dioxide gas and cold water to a carbonator, where it’s mixed and the gas dissolves into the water, then a bit of flavoured syrup and voilà, here’s your drink.[1]

In this case what matters for us is just the capability to push the button and wait for our drink, that’s all, so in this abstraction scenario “push this button to get a drink” is the essential characteristic. In other hand presuming you’re a engineer and there are some calls saying that the button is stuck sometimes and you need to fix it, so far just knowing how to get a drink is insufficient to fix the machine so you need to go deeply and know how a soda fountain machine works to be able to deal with that.

Let’s code

Still using the soda fountain machine example let’s write some code, we need to transform this machine in a class, so far what we know is: there’s a unique button that when pushed drop some soda:

L#4 — Creating a SodaMachine class that’s a representation of the soda fountain machine blueprint
L#8 — Setting a doPush method that’ll drop soda when pushed
L#13 — Using the soda constant to instantiate theSodaMachine class
L#14 — Calling the doPush method from SodaMachine class instantiated as soda

Let’s think we still constructing our soda machine and we need to add more buttons, so in this new version our machine won’t contains just one button but now we can offer two more flavours, three in a total.
Taking into account that now we’ve more information about our machine, let’s assume that the flavours are: Cola, Lemon and Orange, so let’s code!

L#8 — Adding a ESodaFlavours enum to help the machine to identify the chosen flavour
L#8 — Adding a flavour param for doPush method that should be a item from ESodaFlavours enum
L#25, L#26, L#27 — Simulating three different pushed button scenarios

So far so good! Well done guys!

Our machine is almost complete, we just need to add some ice, so we’ll add a new button that’s really different from the rest, when pushed it should drop some ice cubes:

L#18 — Moving from doPush to dropSoda
L#25 — Adding dropIce method to deal with ice cubes requests
L#33, L#33 — Simulating a scenario where the user request a soda with ice

We finally have our soda fountain machine constructed and ready to release!
Briefly, abstraction refers to the act of representing essential aspects, omitting unnecessary characteristics, as we did previously, abstraction is an useful key-concept of OOP that helped us transform a real world machine in blueprint.

[1] — How Do Soda Fountain Machines Work? https://wonderopolis.org/wonder/how-are-fountain-drinks-different-from-bottled-drinks#:~:text=The%20soda%20you%20get%20from,gas%20dissolves%20into%20the%20water.

--

--