πŸ” Proxy Design Pattern: Database Access Control

πŸ“˜ Scenario:

Imagine you are building a user management system with access to critical database operations like Insert, Update, Delete, and Select. However, you want to restrict access so that only authorized users can perform these actions. For example, only an admin with the correct username (Tharindu) and password (deltaCodex8754) should be able to modify the data.

Without proper control, unauthorized users could execute sensitive commands. That’s where the Proxy pattern helps!

⚠️ Problem:

πŸ’‘ Proxy Pattern Solution:

We create a ProxyDatabase class that wraps around the real RealDatabase and acts as a gatekeeper. It performs authentication before delegating database operations. This keeps the core database logic clean and adds an extra layer of security.

Proxy Pattern UML

πŸ–₯️ Output:

[System] βœ… Login successful.
[ProxyDatabase] βœ… Authenticated. Delegating insert().
[RealDatabase] Inserting data into the database.
[ProxyDatabase] βœ… Authenticated. Delegating update().
[RealDatabase] Updating data in the database.
[ProxyDatabase] βœ… Authenticated. Delegating delete().
[RealDatabase] Deleting data from the database.
[ProxyDatabase] βœ… Authenticated. Delegating select().
[RealDatabase] Selecting data with query: SELECT * FROM users;
    
πŸ”— View Code on GitHub

πŸ” Proxy DB Authentication

Use Username as "Tharindu" to access all functions

Use the password as "deltaCodex8754" to access all functions

[Console Output]