New Recording 4.m4a

UML Diagram

image.png

Example

// Subject interface
interface Internet {
    void connectTo(String serverHost) throws Exception;
}

// Real Subject
class RealInternet implements Internet {
    @Override
    public void connectTo(String serverHost) {
        System.out.println("Connecting to " + serverHost);
    }
}

// Proxy class
class ProxyInternet implements Internet {
    private RealInternet realInternet = new RealInternet();
    private static final List<String> blockedSites = new ArrayList<>();

    static {
        blockedSites.add("malicious.com");
        blockedSites.add("phishing.com");
        blockedSites.add("unsecure.com");
    }

    @Override
    public void connectTo(String serverHost) throws Exception {
        if (blockedSites.contains(serverHost.toLowerCase())) {
            throw new Exception("Access Denied: " + serverHost + " is blocked.");
        }
        realInternet.connectTo(serverHost);
    }
}

// Client class
public class ProxyDesignPatternExample {
    public static void main(String[] args) {
        Internet internet = new ProxyInternet();

        try {
            internet.connectTo("example.com");
            internet.connectTo("malicious.com");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}