Methods in SocialNetwork

Learn how to extend your SocialNetwork class with additional methods.

In our last lesson, we built a SocialNetwork class that simply stored Users and Posts. Now, let’s take our design further by giving SocialNetwork the ability to manage these objects.

Imagine a bustling city where thousands of people live and interact. Without a mayor or city planner, chaos would reign! Similarly, our SocialNetwork class isn’t just a passive container—it’s the mayor of Chirpy, actively managing users and posts to keep the platform organized.

You’re tasked with designing the SocialNetwork class for Chirpy, which must empower the platform with the following features:

  • Search for users: Allow the system to locate a user by their username.

  • Ensure unique usernames: Prevent duplicate usernames from being added.

  • Remove users: Enable the removal of a user from the network.

  • List all posts: Provide a way to display all posts made by users.

Question: Based on these requirements, how would you design the SocialNetwork class? Explain your method signatures and outline how each method would interact with the User and Post objects. Additionally, describe why centralizing these functionalities in a SocialNetwork class demonstrates the concept of composition and how it benefits the scalability and maintainability of the system.

Get hands-on with 1400+ tech skills courses.