Design (LLD) TrueCaller - Machine Coding

Table of contents

Cakkpture.PNG

TrueCaller is a phone number lookup and spam blocking service that allows users to identify unknown phone numbers and block unwanted calls. In order to design the Low-level design (LLD) for TrueCaller, several key components would need to be considered.

  1. Phone Number Database: A large database of phone numbers and associated information, such as contact names and caller ID information, would be necessary to provide the core functionality of the service. The database would need to be able to handle a large number of queries and updates in real-time, and would likely be implemented using a distributed database system.

  2. Caller ID Lookup: A system for quickly looking up phone numbers in the database and returning the associated contact name and other information would be necessary. This could be implemented using a hash table or a search tree, such as a B-tree, to allow for fast lookups.

  3. Spam Blocking: A system for identifying and blocking unwanted calls would need to be implemented. This could be done by using machine learning algorithms to analyze call patterns and other data to identify spam calls, or by integrating with third-party spam blocking services.

  4. User Interface: A user-friendly interface for searching for phone numbers, adding and updating contact information, and managing spam blocking settings would be necessary. This could be implemented as a web interface or as a mobile app.

  5. Privacy and Security: Strong security measures would be necessary to protect user data and ensure that the service complies with privacy regulations. This could include encryption of sensitive data, secure authentication and authorization, and regular security audits.

  6. Scaling: The service will be handling a large number of requests and updates, so the system should be designed in a way that it can easily scale horizontally and vertically as the user base grows.

This is a general overview of the key components that would be necessary to implement a TrueCaller-like service. The actual implementation will depend on the specific requirements and constraints of the project, and may include additional components or different design choices.

Code

// Singleton Pattern for TrueCaller system
public class TrueCaller {
    private static TrueCaller instance;
    private PhoneBook phoneBook;
    private List<PhoneNumber> blockedNumbers;
    private List<Observer> observers;

    private TrueCaller() {
        phoneBook = new PhoneBook();
        blockedNumbers = new ArrayList<>();
        observers = new ArrayList<>();
    }

    // Singleton instance getter
    public static synchronized TrueCaller getInstance() {
        if (instance == null) {
            instance = new TrueCaller();
        }
        return instance;
    }

    // Observer Pattern: Register observer
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    public void unregisterObserver(Observer observer) {
        observers.remove(observer);
    }

    // Notify all observers about blocked/unblocked actions
    private void notifyObservers(String message) {
        for (Observer observer : observers) {
            observer.update(message);
        }
    }

    public void makeCall(PhoneNumber phoneNumber) {
        Command callCommand = new CallCommand(phoneBook, blockedNumbers, phoneNumber);
        callCommand.execute();
    }

    public void blockNumber(PhoneNumber phoneNumber) {
        Command blockCommand = new BlockCommand(blockedNumbers, phoneNumber);
        blockCommand.execute();
        notifyObservers("The number " + phoneNumber.getFormattedNumber() + " has been blocked.");
    }

    public void unblockNumber(PhoneNumber phoneNumber) {
        Command unblockCommand = new UnblockCommand(blockedNumbers, phoneNumber);
        unblockCommand.execute();
        notifyObservers("The number " + phoneNumber.getFormattedNumber() + " has been unblocked.");
    }

    public List<Contact> searchPhoneBook(SearchStrategy strategy, String query) {
        return strategy.search(phoneBook, query);
    }
}

// Factory Method Pattern for creating contacts and phone numbers
class ContactFactory {
    public static Contact createContact(String name, PhoneNumber phoneNumber, String email, String address) {
        Contact contact = new Contact(name, phoneNumber);
        contact.setEmail(email);
        contact.setAddress(address);
        return contact;
    }

    public static PhoneNumber createPhoneNumber(String countryCode, String areaCode, String number) {
        return new PhoneNumber(countryCode, areaCode, number);
    }
}

// Observer Pattern for notifications
interface Observer {
    void update(String message);
}

class User implements Observer {
    private String name;

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

    @Override
    public void update(String message) {
        System.out.println("Notification for " + name + ": " + message);
    }
}

// Strategy Pattern for searching contacts
interface SearchStrategy {
    List<Contact> search(PhoneBook phoneBook, String query);
}

class SearchByName implements SearchStrategy {
    @Override
    public List<Contact> search(PhoneBook phoneBook, String name) {
        return phoneBook.searchContacts(name);
    }
}

class SearchByPhoneNumber implements SearchStrategy {
    @Override
    public List<Contact> search(PhoneBook phoneBook, String phoneNumber) {
        return phoneBook.searchByPhoneNumber(phoneNumber);
    }
}

// Command Pattern for handling user actions
interface Command {
    void execute();
}

class CallCommand implements Command {
    private PhoneBook phoneBook;
    private List<PhoneNumber> blockedNumbers;
    private PhoneNumber phoneNumber;

    public CallCommand(PhoneBook phoneBook, List<PhoneNumber> blockedNumbers, PhoneNumber phoneNumber) {
        this.phoneBook = phoneBook;
        this.blockedNumbers = blockedNumbers;
        this.phoneNumber = phoneNumber;
    }

    @Override
    public void execute() {
        if (blockedNumbers.contains(phoneNumber)) {
            System.out.println("The number " + phoneNumber.getFormattedNumber() + " is blocked, call cannot be made.");
        } else {
            List<Contact> searchResults = phoneBook.searchByPhoneNumber(phoneNumber.getNumber());
            if (searchResults.size() > 0) {
                Contact contact = searchResults.get(0);
                System.out.println("Calling " + contact.getName() + " at " + phoneNumber.getFormattedNumber());
            } else {
                System.out.println("The number " + phoneNumber.getFormattedNumber() + " is not in your phone book, call cannot be made.");
            }
        }
    }
}

class BlockCommand implements Command {
    private List<PhoneNumber> blockedNumbers;
    private PhoneNumber phoneNumber;

    public BlockCommand(List<PhoneNumber> blockedNumbers, PhoneNumber phoneNumber) {
        this.blockedNumbers = blockedNumbers;
        this.phoneNumber = phoneNumber;
    }

    @Override
    public void execute() {
        blockedNumbers.add(phoneNumber);
        System.out.println("The number " + phoneNumber.getFormattedNumber() + " has been blocked.");
    }
}

class UnblockCommand implements Command {
    private List<PhoneNumber> blockedNumbers;
    private PhoneNumber phoneNumber;

    public UnblockCommand(List<PhoneNumber> blockedNumbers, PhoneNumber phoneNumber) {
        this.blockedNumbers = blockedNumbers;
        this.phoneNumber = phoneNumber;
    }

    @Override
    public void execute() {
        blockedNumbers.remove(phoneNumber);
        System.out.println("The number " + phoneNumber.getFormattedNumber() + " has been unblocked.");
    }
}

// Supporting Classes
public class PhoneNumber {
    private String countryCode;
    private String areaCode;
    private String number;

    public PhoneNumber(String countryCode, String areaCode, String number) {
        this.countryCode = countryCode;
        this.areaCode = areaCode;
        this.number = number;
    }

    public String getFormattedNumber() {
        return "+" + countryCode + " " + areaCode + " " + number;
    }

    public String getNumber() {
        return number;
    }
}

public class Contact {
    private String name;
    private PhoneNumber phoneNumber;
    private String email;
    private String address;

    public Contact(String name, PhoneNumber phoneNumber) {
        this.name = name;
        this.phoneNumber = phoneNumber;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public PhoneNumber getPhoneNumber() {
        return phoneNumber;
    }
}

public class PhoneBook {
    private List<Contact> contacts;

    public PhoneBook() {
        contacts = new ArrayList<>();
    }

    public void addContact(Contact contact) {
        contacts.add(contact);
    }

    public void removeContact(Contact contact) {
        contacts.remove(contact);
    }

    public List<Contact> searchContacts(String name) {
        List<Contact> searchResults = new ArrayList<>();
        for (Contact contact : contacts) {
            if (contact.getName().contains(name)) {
                searchResults.add(contact);
            }
        }
        return searchResults;
    }

    public List<Contact> searchByPhoneNumber(String phoneNumber) {
        List<Contact> searchResults = new ArrayList<>();
        for (Contact contact : contacts) {
            if (contact.getPhoneNumber().getNumber().equals(phoneNumber)) {
                searchResults.add(contact);
            }
        }
        return searchResults;
    }
}

Did you find this article valuable?

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