Space City Architect Polymorphic Design

PythonPythonBeginner
Practice Now

Introduction

Welcome to the year 2100, where humanity has reached the stars and established thriving space cities among the cosmos. In this futuristic scenario, you are a pioneering Space City Architect tasked with creating innovative solutions to adapt to the ever-evolving needs of these celestial metropolises.

As an architect responsible for designing robust building blocks of a space city, you'll need to employ the advanced concept of polymorphism. This programming principle will allow your creations to interact and communicate seamlessly with various space city systems using a common interface, despite their underlying differences.

Your objective in this lab is to harness the power of Python polymorphism to create flexible and maintainable code for the city's infrastructure components. Through this, you'll ensure the city's growth and adaptability for centuries to come. Prepare to be at the forefront of space urban development, architect!


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") subgraph Lab Skills python/polymorphism -.-> lab-271580{{"`Space City Architect Polymorphic Design`"}} end

Understanding Polymorphism in Python

In this step, you'll explore the basics of Python polymorphism. Polymorphism enables us to define methods in the child class with the same name as defined in their parent class. As a result, the child class inherits its properties and behaviors. However, if you need to make certain adjustments or additions, polymorphism allows for changes without altering the parent class structure.

Begin by creating a base class that represents the interface for interacting with different city system modules.

Next, add the following content to your ~/project/infrastructure.py:

## infrastructure.py
class CitySystem:
    def power_on(self):
        raise NotImplementedError("Subclass must implement this abstract method")

class TransportationSystem(CitySystem):
    def power_on(self):
        print("Transportation System is now activated.")

class WasteManagementSystem(CitySystem):
    def power_on(self):
        print("Waste Management System is now activated.")

## This function represents the city initialization sequence
def initiate_city_systems(systems):
    for system in systems:
        system.power_on()

## Let’s see polymorphism in action
if __name__ == "__main__":
    city_systems = [TransportationSystem(), WasteManagementSystem()]
    initiate_city_systems(city_systems)

To test that the systems correctly initiate with polymorphism, run the following command:

python3 ~/project/infrastructure.py

You should see output indicating that both the Transportation and Waste Management Systems have been activated:

Transportation System is now activated.
Waste Management System is now activated.

Adding New Systems

Now that you understand how polymorphism works, it's time to expand our space city's functionality. Your task is to add a new system for ResourceAllocation without disrupting the existing codebase.

In the same infrastructure.py file, append a new class called ResourceAllocationSystem with its own power_on method that prints a unique message when activated.

class ResourceAllocationSystem(CitySystem):
    def power_on(self):
        print("Resource Allocation System is now activated.")

Don't forget to update the city_systems array and include an instance of the newly created ResourceAllocationSystem, and then test the initialization sequence. The final step is to execute the script:

python3 ~/project/infrastructure.py

The information below should be displayed on your terminal:

Transportation System is now activated.
Waste Management System is now activated.
Resource Allocation System is now activated.

Summary

In this lab, we ventured into the realm of Python Polymorphism, discovering its vital role in creating versatile and maintainable code structures for complex applications like a futuristic space city's infrastructure. We started with basic concepts, practiced by designing a universal power-on sequence for different systems, and progressed to expand our city's capabilities by seamlessly integrating new systems—thanks to polymorphism.

By embracing Python's object-oriented features, we've enhanced our skills as developers and Space City Architects, laying the groundwork for modular, adaptable software that stands the test of interstellar time.

Other Python Tutorials you may like