Design (LLD) Whatsapp Messenger - Machine Coding

Design (LLD) Whatsapp Messenger - Machine Coding

Features Required:

  1. User Registration and Authentication: Users should be able to create accounts, log in, and authenticate themselves to use the messenger.

  2. Contact Management: Users should be able to add and remove contacts from their contact list.

  3. Real-time Messaging: Users should be able to send and receive text messages in real-time.

  4. Group Chats: Users should be able to create, join, and participate in group chats.

  5. Multimedia Messaging: Users should be able to send and receive multimedia messages, such as images, videos, and documents.

  6. Message Status: Users should be able to see the status of their messages, such as delivered, read, etc.

  7. Message Encryption: Messages should be encrypted to ensure privacy and security.

  8. Message Search: Users should be able to search for specific messages based on keywords or sender.

  9. Push Notifications: Users should receive push notifications for new messages even when the app is in the background.

  10. Online Presence: Users should be able to see the online status of their contacts.

Design Patterns Involved or Used:

  1. Model-View-Controller (MVC) Pattern: The MVC pattern can be used to separate the messenger app into three components: the model (data and business logic), the view (user interface), and the controller (handles user interactions and manages the flow of data).

  2. Observer Pattern: The Observer pattern can be used to notify users about new messages and updates to message status.

  3. Factory Pattern: The Factory pattern can be used to create different types of message objects based on user requests, such as text messages, multimedia messages, etc.

  4. Singleton Pattern: The Singleton pattern can be used to ensure that only one instance of certain classes, such as the user authentication manager or the message manager, is created and shared across the application.

  5. Proxy Pattern: The Proxy pattern can be used to handle communication between the application and the messaging server, providing a level of indirection and encapsulation for network operations.

  6. Command Pattern: The Command pattern can be used to encapsulate and decouple actions, such as sending messages or deleting messages, from the specific objects or components that perform those actions.

  7. Publish-Subscribe Pattern: The Publish-Subscribe pattern can be used to implement the push notification system, where users subscribe to their chat threads to receive updates on new messages, and publishers send message updates to the subscribers.

  8. Decorator Pattern: The Decorator pattern can be used to add additional features or behaviors to message objects, such as message encryption.

  9. Strategy Pattern: The Strategy pattern can be used to implement different search algorithms for searching messages based on different criteria, such as keywords or sender.

  10. State Pattern: The State pattern can be used to manage the different states of message interactions, such as composing, sending, or deleting messages.

Code: Detailed Implementation of Classes Based on Each Design Pattern Mentioned Above

// User class
class User {
    private String userId;
    private String username;
    private String password;
    // Other attributes and methods

    public User(String userId, String username, String password) {
        this.userId = userId;
        this.username = username;
        this.password = password;

    // Getters and setters
    // Other user-related methods

// Message class
class Message {
    private String messageId;
    private User sender;
    private List<User> receivers;
    private String content;
    private LocalDateTime timestamp;
    private boolean isEncrypted;
    private boolean isDelivered;
    private boolean isRead;
    // Other attributes and methods

    public Message(String messageId, User sender, List<User> receivers, String content) {
        this.messageId = messageId;
        this.sender = sender;
        this.receivers = receivers;
        this.content = content;
        this.timestamp =;
        this.isEncrypted = false;
        this.isDelivered = false;
        this.isRead = false;

    // Getters and setters
    // Methods for marking message as delivered and read

// MessageManager class
class MessageManager {
    private List<Message> sentMessages;
    private List<Message> receivedMessages;
    // Other attributes and methods

    public MessageManager() {
        this.sentMessages = new ArrayList<>();
        this.receivedMessages = new ArrayList<>();

    public void sendMessage(Message message) {
        // Send the message and add it to the sent messages

    public void receiveMessage(Message message) {
        // Notify observers about new received message

    // Other message management methods

// MessageSearchStrategy interface (Strategy)
interface MessageSearchStrategy {
    List<Message> searchMessages(List<Message> messages, String keyword);

// KeywordSearchStrategy class (Strategy)
class KeywordSearchStrategy implements MessageSearchStrategy {
    public List<Message> searchMessages(List<Message> messages, String keyword) {
        // Implement keyword-based message search

// SenderSearchStrategy class (Strategy)
class SenderSearchStrategy implements MessageSearchStrategy {
    public List<Message> searchMessages(List<Message> messages, String sender) {
        // Implement sender-based message search

// SearchManager class
class SearchManager {
    private MessageSearchStrategy searchStrategy;

    public void setSearchStrategy(MessageSearchStrategy searchStrategy) {
        this.searchStrategy = searchStrategy;

    public List<Message> searchMessages(List<Message> messages, String keyword) {
        return searchStrategy.searchMessages(messages, keyword);

// PresenceObserver interface
interface PresenceObserver {
    void onPresenceChange(User user, boolean online);

// PresenceManager class (Singleton)
class PresenceManager {
    private static PresenceManager instance;
    private Map<User, Boolean> presenceMap;
    private List<PresenceObserver> observers;

    private PresenceManager() {
        this.presenceMap = new HashMap<>();
        this.observers = new ArrayList<>();

    public static synchronized PresenceManager getInstance() {
        if (instance == null) {
            instance = new PresenceManager();
        return instance;

    public void setPresence(User user, boolean online) {
        presenceMap.put(user, online);
        notifyObservers(user, online);

    public void addObserver(PresenceObserver observer) {

    public void removeObserver(PresenceObserver observer) {

    private void notifyObservers(User user, boolean online) {
        for (PresenceObserver observer : observers) {
            observer.onPresenceChange(user, online);

// MessageEncryptionDecorator class (Decorator)
class MessageEncryptionDecorator extends Message {
    private Message message;

    public MessageEncryptionDecorator(Message message) {
        super(message.getMessageId(), message.getSender(), message.getReceivers(), message.getContent());
        this.message = message;

    public String getContent() {
        // Implement message encryption

    public boolean isEncrypted() {
        return true;

// MessageState interface (State)
interface MessageState {
    void handleMessage(Message message);

// ComposingState class (State)
class ComposingState implements MessageState {
    public void handleMessage(Message message) {
        // Handle message actions in composing state (e.g., saving drafts, editing)

// SentState class (State)
class SentState implements MessageState {
    public void handleMessage(Message message) {
        // Handle message actions in sent state (e.g., marking as sent, archiving)

// ReceivedState class (State)
class ReceivedState implements MessageState {
    public void handleMessage(Message message) {
        // Handle message actions in received state (e.g., marking as read, replying)

// MessageStateContext class
class MessageStateContext {
    private MessageState currentState;

    public MessageStateContext() {
        this.currentState = new ComposingState();

    public void setState(MessageState state) {
        this.currentState = state;

    public void handleMessage(Message message) {

// Main Class
public class WhatsAppMessenger {
    public static void main(String[] args) {
        // Create users
        User user1 = new User("user1", "john.doe", "password1");
        User user2 = new User("user2", "alice.smith", "password2");

        // Create message objects
        Message message1 = new Message("msg1", user1, List.of(user2), "Hi Alice, how are you?");
        Message message2 = new Message("msg2", user2, List.of(user1), "Hi John, I'm doing well.");

        // Create message manager
        MessageManager messageManager = new MessageManager();

        // Receive messages

        // Search messages
        SearchManager searchManager = new SearchManager();
        searchManager.setSearchStrategy(new KeywordSearchStrategy());
        List<Message> searchResults = searchManager.searchMessages(messageManager.getReceivedMessages(), "John");

        // Add message encryption using decorators
        Message encryptedMessage = new MessageEncryptionDecorator(message2);
        encryptedMessage.getContent(); // Returns the encrypted content

In this code example, the User class represents a WhatsApp user, the Message class represents a message object, the MessageManager class manages the user's sent and received messages, the SearchManager class uses the Strategy pattern to implement different message search algorithms, the PresenceManager class manages the presence and online status of users, the MessageEncryptionDecorator class adds message encryption to messages using the Decorator pattern, and the MessageStateContext class manages the state of messages (composing, sent, received) using the State pattern.

Please note that this is a simplified example, and a complete implementation of WhatsApp involves more complex components, such as message storage and retrieval, message threading and conversation management, multimedia message handling, message synchronization across devices, integration with real-time communication protocols, push notification services, and user interface design for chatting and managing contacts. Additionally, WhatsApp is a large-scale messaging system that requires robust architecture and distributed infrastructure to handle millions of concurrent users and their messages efficiently.

Did you find this article valuable?

Support Subhahu Jain by becoming a sponsor. Any amount is appreciated!