Design a Social Media Platform like Facebook

May 26, 2026
lldjava

On this page

Design a Social Media Platform like Facebook

Problem Statement

Design a social media platform where users can create accounts, make posts, follow other users, like and comment on posts, and view a personalized feed. The system should handle basic social interactions and generate a feed based on user connections.

Asked In Companies

Meta Google LinkedIn Twitter Amazon Microsoft Netflix Pinterest Salesforce Adobe Oracle Flipkart


Functional Requirements

  • Users should be able to register and login
  • Users should be able to create posts
  • Users should be able to follow/unfollow other users
  • Users should be able to like and comment on posts
  • Users should be able to view a feed of posts from followed users
  • Feed should show latest posts first

Objects Required

  • User
  • Post
  • Comment
  • SocialGraph (Follow system)
  • FeedService

User Class

import java.util.*;

public class User {

    private String userId;
    private String name;

    private Set<User> following = new HashSet<>();
    private List<Post> posts = new ArrayList<>();

    public User(String userId, String name) {
        this.userId = userId;
        this.name = name;
    }

    public void follow(User user) {
        following.add(user);
    }

    public void unfollow(User user) {
        following.remove(user);
    }

    public void createPost(Post post) {
        posts.add(post);
    }

    public Set<User> getFollowing() {
        return following;
    }

    public List<Post> getPosts() {
        return posts;
    }

    public String getName() {
        return name;
    }
}

The constructor initializes a user with a unique id and name.

The follow() method adds another user to the following list, enabling their posts to appear in the feed.

The unfollow() method removes a user from the following list, stopping their posts from appearing in the feed.

The createPost() method allows a user to publish a new post and store it in their personal post list.


Post Class

import java.time.LocalDateTime;
import java.util.*;

public class Post {

    private String postId;
    private User author;
    private String content;
    private LocalDateTime timestamp;

    private Set<User> likes = new HashSet<>();
    private List<Comment> comments = new ArrayList<>();

    public Post(String postId, User author, String content) {
        this.postId = postId;
        this.author = author;
        this.content = content;
        this.timestamp = LocalDateTime.now();
    }

    public void like(User user) {
        likes.add(user);
    }

    public void addComment(Comment comment) {
        comments.add(comment);
    }

    public LocalDateTime getTimestamp() {
        return timestamp;
    }

    public User getAuthor() {
        return author;
    }

    public String getContent() {
        return content;
    }
}

The constructor creates a post with author, content, and automatically assigns timestamp.

The like() method adds a user to the like set, ensuring a user can like a post only once.

The addComment() method attaches a comment to the post for discussion.


Comment Class

import java.time.LocalDateTime;

public class Comment {

    private String commentId;
    private User user;
    private String message;
    private LocalDateTime timestamp;

    public Comment(String commentId, User user, String message) {
        this.commentId = commentId;
        this.user = user;
        this.message = message;
        this.timestamp = LocalDateTime.now();
    }
}

The constructor initializes a comment with user, message, and timestamp.

The class represents user-generated responses on posts.


FeedService Class

import java.util.*;

public class FeedService {

    public List<Post> getFeed(User user) {

        List<Post> feed = new ArrayList<>();

        for (User u : user.getFollowing()) {
            feed.addAll(u.getPosts());
        }

        feed.sort((a, b) ->
                b.getTimestamp().compareTo(a.getTimestamp())
        );

        return feed;
    }
}

The getFeed() method builds a timeline for a user by collecting posts from all followed users.

It sorts posts in reverse chronological order so that the newest content appears first.


SocialMediaPlatform Class

import java.util.*;

public class SocialMediaPlatform {

    private List<User> users = new ArrayList<>();

    public User registerUser(String userId, String name) {
        User user = new User(userId, name);
        users.add(user);
        return user;
    }

    public Post createPost(User user, String postId, String content) {
        Post post = new Post(postId, user, content);
        user.createPost(post);
        return post;
    }
}

The platform class manages user registration and post creation.

The registerUser() method creates and stores a new user in the system.

The createPost() method allows a user to publish a post and links it to their account.


Main Class

public class Main {

    public static void main(String[] args) {

        SocialMediaPlatform platform = new SocialMediaPlatform();

        User u1 = platform.registerUser("U1", "Alice");
        User u2 = platform.registerUser("U2", "Bob");

        u1.follow(u2);

        platform.createPost(u2, "P1", "Hello from Bob!");

        FeedService feedService = new FeedService();

        System.out.println("Feed for Alice:");
        for (Post post : feedService.getFeed(u1)) {
            System.out.println(post.getContent());
        }
    }
}

The main method demonstrates a simple flow where users register, follow each other, create posts, and generate a feed.


Class Diagram

UseruserId : Stringname : Stringfollowing : Set<User>posts : List<Post>follow(User) : voidunfollow(User) : voidcreatePost(Post) : voidPostpostId : Stringauthor : Usercontent : Stringtimestamp : LocalDateTimelikes : Set<User>comments : List<Comment>like(User) : voidaddComment(Comment) : voidCommentcommentId : Stringuser : Usermessage : Stringtimestamp : LocalDateTimeFeedServicegetFeed(User) : List<Post>SocialMediaPlatformregisterUser(String, String) : UsercreatePost(User, String, String) : Postfollows

Also See