Haunted House Mystery: Unveiling Secrets with the Proxy Pattern

Haunted House Mystery: Unveiling Secrets with the Proxy Pattern

Greetings, brave investigators and code enthusiasts,

Picture yourself in a spine-chilling haunted mansion, where enigmatic supernatural entities lurk. To unravel the mysteries within, you hire a paranormal investigator to act as a proxy between you and the otherworldly. Just like the Proxy Pattern serves as an intermediary, controlling access to the haunted house! ๐Ÿ‘ป๐Ÿฐ๐Ÿ’ป

Act 1: The Enigmatic Haunted Mansion - A World of Secrets

In the eerie realm of the haunted mansion, our intrepid investigator embarks on a quest to unveil its dark secrets. However, the mansion's supernatural entities must be approached with caution.

// Subject (Haunted Mansion)
interface HauntedMansion {
  revealSecrets(): void;
}

class ParanormalInvestigator implements HauntedMansion {
  revealSecrets() {
    console.log("Paranormal investigator reveals hidden secrets.");
  }
}

Our investigator uses the "ParanormalInvestigator" class to represent their role and connection with the haunted mansion.

Act 2: Enlisting the Proxy - Controlling Access

To maintain safety and control over the haunted mansion, our investigator employs the Proxy Pattern, creating an intermediary that controls access to the supernatural entities.

class MansionProxy implements HauntedMansion {
  private investigator: ParanormalInvestigator;

  constructor(investigator: ParanormalInvestigator) {
    this.investigator = investigator;
  }

  revealSecrets() {
    if (this.investigator) {
      console.log("Proxy grants access to the investigator.");
      this.investigator.revealSecrets();
    } else {
      console.log("Proxy denies access to unauthorized entities.");
    }
  }
}

Our investigator establishes the "MansionProxy" class, ensuring that only authorized entities can access the secrets within the haunted mansion.

Act 3: Unveiling Hidden Truths - Safely Accessing the Mansion

As our investigator delves deeper into the mysteries, they safely access the haunted mansion through the Proxy Pattern, maintaining control and ensuring safety.

const investigator = new ParanormalInvestigator();
const mansionProxy = new MansionProxy(investigator);

mansionProxy.revealSecrets(); // Proxy grants access to the investigator

const unauthorizedEntity = new ParanormalInvestigator();
const unauthorizedMansionProxy = new MansionProxy(unauthorizedEntity);

unauthorizedMansionProxy.revealSecrets(); // Proxy denies access to unauthorized entities

The Proxy Pattern empowers our investigator to unveil hidden truths within the haunted mansion while maintaining control over access.

Conclusion: Investigator's Code of Control

In the enigmatic world of the haunted mansion and code, the Proxy Pattern acts as an intermediary, ensuring safety and control. It's like having a paranormal investigator to safely access the supernatural.

Whether you're exploring mysteries or coding your next project, remember that the Proxy Pattern can help you control access and maintain safety. ๐Ÿ‘ป๐Ÿฐ๐Ÿ’ป

ย