📅Week-5 (Day -1) - Understanding the Proxy Design Pattern: Virtual, Protection, and Remote Proxies Explained

Payal KumariPayal Kumari
5 min read

NOTE: - I started my 8-week system design journey with Coder Army. I will be journaling every day, recording what I learn, reflecting on it, and sharing it with my network to help newcomers to system design.


💠 What is the Proxy Design Pattern?

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it. It’s like a personal assistant who filters or manages your meetings. ☎️🙅‍♂️

(Hindi: Proxy Pattern ek aisa design pattern hai jo kisi object ke liye ek representative ya agent banata hai — jo us object ka access control karta hai.)

💠 Why Do We Need a Proxy?

  • Sometimes, direct access to an object is too expensive, unsafe, or just not possible.

  • Proxies help in handling:

    • 💰 Expensive resource usage

    • 🔐 Security and protection

    • 🌍 Remote objects access

💠 Types of Proxy

1️⃣ Virtual Proxy – Lazy Loading

Use Case: Delays the creation of an expensive object until it’s needed.

Example: Imagine a photo gallery app. You don’t want to load all high-res images at once — instead, use a proxy that loads them when scrolled into view.

(Hindi: Agar koi image bahut heavy hai toh usse turant load karne ke bajaye tab load karo jab user use dekhe — yeh kaam karta hai virtual proxy.)

interface IImage {
    void display();
}

class RealImage implements IImage {
    private String filename;

    public RealImage(String file) {
        this.filename = file;
        System.out.println("[RealImage] Loading image from disk: " + filename);
    }

    @Override
    public void display() {
        System.out.println("[RealImage] Displaying " + filename);
    }
}

class ImageProxy implements IImage {
    private RealImage realImage;
    private String filename;

    public ImageProxy(String file) {
        this.filename = file;
        this.realImage = null;
    }

    @Override
    public void display() {
        if (realImage == null) {
            realImage = new RealImage(filename);
        }
        realImage.display();
    }
}

public class VirtualProxy {
    public static void main(String[] args) {
        IImage image1 = new ImageProxy("sample.jpg");
        image1.display();
    }
}

2️⃣ Protection Proxy – Access Control

Use Case: Controls access to sensitive objects based on permissions.

Example: Admin dashboard. Only admins can delete data — others can only view. Protection Proxy checks user role before allowing actions.

(Hindi: Jaise bank ke admin panel mein sirf authorised log hi kuch edit kar sakte hain — normal users nahi. Yeh kaam karta hai protection proxy.)

interface IDocumentReader {
    void unlockPDF(String filePath, String password);
}

class RealDocumentReader implements IDocumentReader {
    @Override
    public void unlockPDF(String filePath, String password) {
        System.out.println("[RealDocumentReader] Unlocking PDF at: " + filePath);
        System.out.println("[RealDocumentReader] PDF unlocked successfully with password: " + password);
        System.out.println("[RealDocumentReader] Displaying PDF content...");
    }
}

class User {
    public String name;
    public boolean premiumMembership;

    public User(String name, boolean isPremium) {
        this.name = name;
        this.premiumMembership = isPremium;
    }
}

class DocumentProxy implements IDocumentReader {
    private RealDocumentReader realReader;
    private User user;

    public DocumentProxy(User user) {
        this.realReader = new RealDocumentReader();
        this.user = user;
    }

    @Override
    public void unlockPDF(String filePath, String password) {
        if (!user.premiumMembership) {
            System.out.println("[DocumentProxy] Access denied. Only premium members can unlock PDFs.");
            return;
        }
        realReader.unlockPDF(filePath, password);
    }
}

public class ProtectionProxy {
    public static void main(String[] args) {
        User user1 = new User("Rohan", false);
        User user2 = new User("Rashmi", true);

        System.out.println("== Rohan (Non-Premium) tries to unlock PDF ==");
        IDocumentReader docReader = new DocumentProxy(user1);
        docReader.unlockPDF("protected_document.pdf", "secret123");

        System.out.println("\n== Rashmi (Premium) unlocks PDF ==");
        docReader = new DocumentProxy(user2);
        docReader.unlockPDF("protected_document.pdf", "secret123");
    }
}

3️⃣ Remote Proxy – Object Over the Internet

Use Case: Represents an object that exists remotely (e.g., on a different server).

Example: When your app calls an API hosted in the US while you're in India. The Remote Proxy acts as a local placeholder.

(Hindi: Jaise Zoom call mein aapka friend US mein hai, lekin aap usse proxy ke through local screen par dekh rahe ho.)

interface IDataService {
    String fetchData();
}

class RealDataService implements IDataService {
    public RealDataService() {
        // Imagine this connects to a remote server or loads heavy resources.
        System.out.println("[RealDataService] Initialized (simulating remote setup)");
    }

    @Override
    public String fetchData() {
        return "[RealDataService] Data from server";
    }
}

// Remote proxy
class DataServiceProxy implements IDataService {
    private RealDataService realService;

    public DataServiceProxy() {
        realService = new RealDataService();
    }

    @Override
    public String fetchData() {
        System.out.println("[DataServiceProxy] Connecting to remote service...");
        return realService.fetchData();
    }
}

public class RemoteProxy {
    public static void main(String[] args) {
        IDataService dataService = new DataServiceProxy();
        dataService.fetchData();
    }
}

💠Real-World Use Cases

  • 🎮 Lazy loading in games (Virtual Proxy)

  • 🔐 Secure APIs or Admin UIs (Protection Proxy)

  • 🛰️ Remote object communication (Remote Proxy)

(Hindi: Proxy ka use har jagah hota hai — APIs, games, secure dashboards, aur remote connections mein!)

💠 Benefits of Proxy Pattern

  • Lazy initialization of heavy resources

  • Improved security and access control

  • Enables distributed computing

  • Cleaner client-side code

💠Recap

Proxy Pattern kya karta hai?

Kisi object ka access control karta hai ek mediator ya representative ke through.

Types of Proxy

  1. Virtual Proxy → Jab zarurat ho tab load karo

  2. Protection Proxy → Sirf authorized users ko access do

  3. Remote Proxy → Door ke object ko local bana do

(Hindi: Proxy Pattern ek aise concept hai jo aapke project ko smart aur efficient banata hai.)

Week - 5 (Day-1) Completed ✅ System Design

NOTE : - A big thanks to my mentors Rohit Negi Sir and Aditya Sir for launching this amazing 8-week course absolutely free on YouTube via CoderArmy9 :- youtube.com/@CoderArmy9 . 🙌

👉 Share this blog with your connections! Let’s keep learning, growing, and supporting one another on this journey. 🚀

✍️ Payal Kumari 👩‍💻

Jai Hind 🇮🇳 | #CoderArmy #LearningInPublic #SystemDesign #TechForAll #MentorshipMatters #8weeksLLdChallenge #LowLevelDesign #Code #LLD #OOP

0
Subscribe to my newsletter

Read articles from Payal Kumari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Payal Kumari
Payal Kumari

I'm a passionate full-stack developer with a strong foundation in the MERN stack—building and maintaining scalable web applications using React.js, Node.js, and Next.js. My journey in open source began with Hacktoberfest 2023, where I made four impactful pull requests that sparked a love for collaborative coding, global learning, and open knowledge sharing. Since then, I’ve contributed to and mentored projects in top open source programs like GSSoC’24, SSOC’24, and C4GT’24. As a Google Gen AI Exchange Hackathon ’24 Finalist and Google’s Women Techmakers (WTM) Ambassador, I’ve been privileged to support diverse communities in building meaningful tech solutions. My work as a Top 50 Mentor for GSSoC ’24 reflects my commitment to nurturing new talent in tech. Beyond development, I serve as a Student Career Guide, Profile Building Expert & Evangelist at Topmate.io, where I conduct workshops, guide students through resume building and career strategy, and help mentees navigate open source and tech careers. Recognized among the Top 5% of mentors and featured on “Topmate Discover,” I take pride in making mentorship accessible and impactful. My technical voice has also been acknowledged by LinkedIn, where I’ve earned the Top Voice badge seven times in domains like web development, programming, and software engineering. In addition, I hold LinkedIn Golden Badges for Research Skills, Interpersonal Skills, Critical Thinking, and Teamwork—signaling a well-rounded approach to both individual contribution and team collaboration. Graduating with an MCA from Chandigarh University in 2023, I’ve continued to fuel my curiosity by writing technical articles and sharing practical MERN stack insights across platforms. Whether it’s building polished UIs, optimizing backend performance, or guiding a mentee through their first pull request, I’m driven by the power of community and continuous learning. Let’s connect! I'm open to collaborations, mentorship, or building something impactful together. Reach out to me at kumaripayal7488@gmail.com or visit my profile on Topmate.io.