π 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.
π₯οΈ 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;
Use Username as "Tharindu" to access all functions
Use the password as "deltaCodex8754" to access all functions