DEV Community

Cover image for You don't know OOP, the heater does♨️
Vıɔk A Hıƃnıʇɐ C.
Vıɔk A Hıƃnıʇɐ C.

Posted on • Edited on

You don't know OOP, the heater does♨️

In an enchanted forest, where logic and magic danced hand in hand, there lived a Magic Box. Inside, a structured chaos reigned, filled with objects, each with a unique identity, a state of mind, and a very particular behavior. Each one knew its purpose in life, but was incapable of performing a complex function on its own. They were a community of simple souls in search of a common goal. ❄️

On a bitterly cold winter day, when the chill penetrated the very wooden bones of the box, the Resistor, an object with the gift of generating heat, realized a fundamental truth: its gift was useless without the power of energy. Then, the Battery, a source of life overflowing with magical electrons, offered to help. With a gesture of generosity, it granted the Resistor the ability to inherit ⬇️ its energy. The Resistor, now powered, began to glow with a faint reddish warmth. But soon a new problem arose: the heat accumulated and there was nothing to disperse it into the air.

The Fan, a dreamy object with restless blades, volunteered. However, to move and spread that precious heat, it also needed to inherit ⬇️ the Battery's energy. The Battery, with its collaborative spirit, agreed. Together, these three objects decided to unite their forces and create a new entity, a larger and more powerful class they called "Heater." 🏭

But when they activated the system, confusion reigned. The Fan did not move. It was still, silent. 🔇 After a quick investigation, they discovered the problem: the power functionality that the Battery had directly granted was useless for the Fan. The Resistor worked happily with Direct Current (DC), the very essence of the Battery. But the Fan, with a more complex design, needed Alternating Current (AC) to spin its motor. It was a conflict of identities.

The Battery, wise and flexible, devised a brilliant solution. It decided not to give its energy directly, but to implement a contract, an "Interface" 🤝, a promise to supply energy. Through this agreement, each object could polymorph 🎭, meaning it could take the form of energy it needed. The Resistor interpreted the interface's energy as DC and kept on heating. The Fan interpreted it as AC and, with a sigh of relief, began to spin, spreading heat throughout the Magic Box. Polymorphism had saved the day!

Everything was working perfectly, but the Heater, the higher-ranking class, noticed something unsettling. It could feel the ambient heat, but it didn't know the exact temperature at which the Resistor was working. Worried, it asked:

—Resistor, what temperature are you at? I need to control your state to prevent overheating.

The Resistor, with a hint of pride in its metallic voice, replied:

—I'm sorry, sir. My temperature is a private matter, a detail of my design. I have it all encapsulated 📦. You can enjoy the heat I produce, but you cannot manipulate my internal thermostat.

The Heater, surprised, reflected:
—I understand the need for privacy; encapsulation protects your integrity. But I can see and regulate the Fan's speed. To fulfill my function of maintaining a pleasant climate, I need the same capability with you. I need a public interface to control your temperature.

The Resistor understood. Protection shouldn't be a wall that prevents collaboration, but rather a door with a lock. So, it decided to expose its encapsulated properties in a controlled manner, creating public methods for the Heater to safely read and adjust its temperature, without violating its internal integrity. The Heater, now in full control, could masterfully regulate the system, achieving the perfect temperature. ⚖️

Peace and warmth reigned for hours, until suddenly, a chill ran through the Magic Box. The Battery, which had given its all, emitted one final flicker and was completely drained. Exhaustion was total. Things were getting very, very cold. 🥶

But just as hope began to freeze, an object everyone had forgotten came to life: the Electrical Outlet 🔌, which until then had remained inactive, frozen in a corner. With a click, it offered its help.

—I can provide you with unlimited energy from the castle's main grid —it hummed energetically—. But I need the system to adapt.

The Heater, intrigued, asked how that was possible. The Outlet explained the concept of abstraction 🏛️:

—I don't need to know how the Resistor or the Fan work internally. I only need the Heater, the class that encompasses them, to understand that it can receive power through me. I will be the new source. My complexity (the voltage, the grid's current type) remains hidden from you all. I only expose the action 'deliverEnergy()'.

The Heater understood perfectly. It implemented the power functionality through the Outlet, who sent a surge of energy that revived the Battery. The cycle of life (and heat) could continue. Thanks to abstraction, the objects communicated without needing to know each other's complex internal secrets. 🔄

And so, the Resistor (with its encapsulated but controlled state), the Fan (with its energy polymorphism), the Battery (with its inheritance of power), and the Outlet (with its great abstraction), all united under the Heater class, managed to survive the harsh winter, creating an atmosphere of perfect and stable temperature. The Magic Box became the coziest home in the entire forest. 🤩

Detail of the magical concepts 🚀

In terms of the spells of Object-Oriented Programming, the story perfectly illustrates its four fundamental pillars:

  • Encapsulation 📦: The Resistor kept its temperature as a secret internal state, protected from the outside. Only when necessary and in a controlled manner (through public getter/setter methods), it exposed this information, allowing interaction without compromising its integrity. It's data protection.
  • Inheritance ⬇️: The Resistor and the Fan received (inherited) the ability to have energy from the Battery. By being part of the Heater class, they also inherited the context and the common goal of generating and dispersing heat.
  • Polymorphism 🎭: The Battery defined a contract (Interface) to supply energy. The Resistor and the Fan implemented that contract in completely different ways (DC vs. AC), responding to the same message ("take my energy") uniquely. One interface, multiple behaviors.
  • Abstraction 🏛️: The Heater class represents a general idea, hiding the complexity of its parts. The Outlet, for its part, is the perfect example: it hides the complex details of the electrical grid and only presents a simple operation ("deliverEnergy") that any class can use without understanding its internal workings.

In summary, this OOP Heater story is an enchanted journey to understand the key ideas of object-oriented programming. By giving it a more magical and narrative tone, it becomes more appealing to all audiences, transforming abstract concepts into characters with lives of their own. We hope this adventure inspires more people to learn about OOP and improve their skills to create their own magical worlds of code. 😎🚀

And then, when everything seemed perfect, a ray of light pierced the Magic Box. Familiar voices were heard in the distance... "I'm the avenger of the night!"... "It's morphin' time!"... Batman and the Power Rangers had arrived! And... to be continued!! 🤣

Top comments (0)