Object-Oriented Design for Facebook
UML Design for Facebook
Facebook is an online social networking service where users can connect with other users to post and read messages. Users access Facebook through their website interface or mobile apps.
System Requirements
We will focus on the following set of requirements while designing Facebook:
- Each member should be able to add information about their basic profile, work experience, education, etc.
- Any user of our system should be able to search other members, groups or pages by their name.
- Members should be able to send and accept/reject friend requests from other members.
- Members should be able to follow other members without becoming their friend.
- Members should be able to create groups and pages, as well as join already created groups, and follow pages.
- Members should be able to create new posts to share with their friends.
- Members should be able to add comments to posts, as well as like or share a post or comment.
- Members should be able to create privacy lists containing their friends. Members can link any post with a privacy list to make the post visible only to the members of that list.
- Any member should be able to send messages to other members.
- Any member should be able to add a recommendation for any page.
- The system should send a notification to a member whenever there is a new message or friend request or comment on their post.
- Members should be able to search through posts for a word.
Extended Requirement: Write a function to find a connection suggestion for a member.
Use Case Diagram
We have three main Actors in our system:
- Member: All members can search for other members, groups, pages, or posts, as well as send friend requests, create posts, etc.
- Admin: Mainly responsible for admin functions like blocking and unblocking a member, etc.
- System: Mainly responsible for sending notifications for new messages, friend requests, etc.
Here are the top use cases of our system:
- Add/update profile: Any member should be able to create their profile to reflect their work experiences, education, etc.
- Search: Members can search for other members, groups or pages. Members can send a friend request to other members.
- Follow or Unfollow a member or a page: Any member can follow or unfollow any other member or page.
- Send message: Any member can send a message to any of their friends.
- Create post: Any member can create a post to share with their friends, as well as like or add comments to any post visible to them.
- Send notification: The system will be able to send notifications for new messages, friend requests, etc.
Here is the use case diagram of Facebook:
Class Diagram
Here are the main classes of the Facebook system:
- Member: This will be the main component of our system. Each member will have a profile which includes their Work Experiences, Education, etc. Members will be connected to other members and they can follow other members and pages. Members will also have suggestions to send friend requests to other members.
- Search: Our system will support searching for other members, groups and pages by their names, and through posts for any word.
Problem
Design an object-oriented social networking system (Facebook-like) that supports user profiles, friendships/connections, posts, comments, groups, pages, messaging, privacy lists, notifications, and search. The system should support account management (members, admins), content creation (posts, comments), social interactions (follow/friend, groups, pages), and real-time notifications.
This file preserves the original diagrams and example code and reorganizes the content to the canonical OOD template.
Solution
1. Requirements Analysis
Functional Requirements:
- Create and update member profiles (work experience, education, places).
- Search members, groups, pages, and posts.
- Send and accept/reject friend requests and follow members/pages.
- Create posts, comments, and likes; add media to posts.
- Create/join groups and interact with group content.
- Send direct messages between members.
- Maintain privacy lists for posts and content visibility.
- Send notifications for messages, friend requests, comments, and other events.
Non-Functional Requirements:
- Scalability: handle large number of users and high write/read throughput.
- Availability: ensure low latency for feed and messaging.
- Extensibility: allow new social features to be added over time.
Constraints & Assumptions:
- Profiles may contain multiple work entries and education records.
- Notifications may be delivered via push, email, or in-app channels.
2. Use Case Diagram
Actors: Member, Admin, System
Use Cases: Add/Update Profile, Search, Follow/Unfollow, Send Message, Create Post, Send Notification
graph TD subgraph Facebook UC1(Add/Update Profile) UC2(Search Members/Pages/Posts) UC3(Follow/Unfollow) UC4(Send Message) UC5(Create Post) UC6(Send Notification) end Member --> UC1 Member --> UC2 Member --> UC3 Member --> UC4 Member --> UC5 System --> UC6
3. Class Diagram
Core classes and responsibilities:
- Account / Person / Member / Admin: user identity and roles.
- Profile / Work / Education: structured profile data.
- Post / Comment / Like: content and interactions.
- Group / Page: community and public pages.
- Message: direct messaging between users.
- PrivacyList: access control lists for posts.
- Search / SearchIndex: indexing and search.
- Notification: event notifications and delivery.
classDiagram class Account { +int id +AccountStatus status } class Person { +String name +Address address } class Member { +int memberId +Profile profile } class Profile { +List<Work> work_experiences +List<Education> educations } class Post { +int postId +String text +List<Comment> comments } class Comment { +int commentId +String text } class Group { +int groupId +List<Member> members } class Page { +int pageId +List<Member> followers } class Message { +int messageId +Member sender +Member recipient } class Notification { +sendNotification() } Account <|-- Member Member "1" -- "1" Profile : has Post "1" -- "0..*" Comment : comments Group "1" -- "0..*" Member : members

4. Activity Diagrams
Activity: Add work experience to profile
graph TD A[Member opens profile] --> B[Clicks add work experience] B --> C[Fills form and submits] C --> D[System validates and saves entry] D --> E[Profile updated]
Activity: Create a new post
graph TD P1[Member creates post] --> P2[Attach media (optional)] P2 --> P3[Choose audience / privacy list] P3 --> P4[Publish post] P4 --> P5[Notify followers]
5. High-Level Code Implementation
The original code examples are preserved and reorganized below into skeletons and concise snippets.
from enum import Enum
from datetime import datetime
from abc import ABC
class ConnectionInvitationStatus(Enum):
PENDING = 1
ACCEPTED = 2
REJECTED = 3
CANCELED = 4
class AccountStatus(Enum):
ACTIVE = 1
CLOSED = 2
CANCELED = 3
BLACKLISTED = 4
DISABLED = 5
class Address:
def __init__(self, street, city, state, zip_code, country):
self.__street_address = street
self.__city = city
self.__state = state
self.__zip_code = zip_code
self.__country = country
class Account(ABC):
def __init__(self, id, password, status=AccountStatus.ACTIVE):
self.__id = id
self.__password = password
self.__status = status
def reset_password(self):
pass
class Person(ABC):
def __init__(self, name, address, email, phone, account):
self.__name = name
self.__address = address
self.__email = email
self.__phone = phone
self.__account = account
class Member(Person):
def __init__(self, id, date_of_membership, name):
self.__member_id = id
self.__date_of_membership = date_of_membership
self.__name = name
self.__profile = Profile()
self.__member_follows = []
self.__member_connections = []
self.__page_follows = []
self.__member_suggestions = []
self.__connection_invitations = []
self.__group_follows = []
def send_message(self, message):
pass
def create_post(self, post):
pass
def send_connection_invitation(self, invitation):
pass
def search_member_suggestions(self):
pass
class Admin(Person):
def block_user(self, customer):
pass
def unblock_user(self, customer):
pass
def enable_page(self, page):
pass
def disable_page(self, page):
pass
class ConnectionInvitation:
def __init__(self, member_invited, name, status=ConnectionInvitationStatus.PENDING):
self.__member_invited = member_invited
self.__status = status
self.__date_created = datetime.date.today()
self.__date_updated = datetime.date.today()
def accept_connection(self):
pass
def reject_connection(self):
pass
class Profile:
def __init__(self, profile_picture=None, cover_photo=None, gender=None):
self.__profile_picture = profile_picture
self.__cover_photo = cover_photo
self.__gender = gender
self.__work_experiences = []
self.__educations = []
self.__places = []
self.__stats = []
def add_work_experience(self, work):
pass
def add_education(self, education):
pass
def add_place(self, place):
pass
class Work:
def __init__(self, title, company, location, date_from, date_to, description):
self.__title = title
self.__company = company
self.__location = location
self.__from = date_from
self.__to = date_to
self.__description = description
class Page:
def __init__(self, id, name, description, type, total_members):
self.__page_id = id
self.__name = name
self.__description = description
self.__type = type
self.__total_members = total_members
self.__recommendation = []
def get_recommendation(self):
return self.__recommendation
class Recommendation:
def __init__(self, id, rating, description):
self.__recommendation_id = id
self.__rating = rating
self.__description = description
self.__created_at = datetime.date.today()
class Group:
def __init__(self, id, name, description, total_members):
self.__group_id = id
self.__name = name
self.__description = description
self.__total_members = total_members
self.__members = []
def add_member(self, member):
pass
def update_description(self, description):
pass
class Post:
def __init__(self, id, text, total_likes, total_shares, owner):
self.__post_id = id
self.__text = text
self.__total_likes = total_likes
self.__total_shares = total_shares
self.__owner = owner
class Message:
def __init__(self, id, sent_to, body, media):
self.__message_id = id
self.__sent_to = sent_to
self.__message_body = body
self.__media = media
def add_member(self, member):
pass
class Comment:
def __init__(self, id, text, total_likes, owner):
self.__comment_id = id
self.__text = text
self.__total_likes = total_likes
self.__owner = owner
class Search(ABC):
def search_member(self, name):
pass
def search_group(self, name):
pass
def search_page(self, name):
pass
def search_post(self, word):
pass
class SearchIndex(Search):
def __init__(self):
self.__member_names = {}
self.__group_names = {}
self.__page_titles = {}
self.__posts = {}
def add_member(self, member):
if member.get_name() in self.__member_names:
self.__member_names.get(member.get_name()).add(member)
else:
self.__member_names[member.get_name()] = member
def add_group(self, group):
pass
def add_page(self, page):
pass
def add_post(self, post):
pass
def search_member(self, name):
return self.__member_names.get(name)
def search_group(self, name):
return self.__group_names.get(name)
def search_page(self, name):
return self.__page_titles.get(name)
def search_post(self, word):
return self.__posts.get(word)
## Extended Requirement
Let's try to write code for finding connection suggestions for a member.
There can be many strategies to search for connection suggestions; a common approach is a two-level deep BFS to find people who have many mutual connections with the user.
```python
# This section is for practicing this code