Proxy pattern: Difference between revisions

Content deleted Content added
m Python: modern Python 3
Xenyph (talk | contribs)
Example: Added JavaScript example, normalised some formatting
Line 65:
{{Wikibooks|Computer Science Design Patterns|Proxy|Proxy implementations in various languages}}
 
=== PHP ===
<syntaxhighlight lang="php">
<?php
 
interface Image
{
public function displayImage();
}
 
// On System A
class RealImage implements Image
{
private string $filename = null;
 
public function __construct(string $filename)
{
$this->filename = $filename;
$this->loadImageFromDisk();
}
 
/**
* Loads the image from the disk
*/
private function loadImageFromDisk()
{
echo "Loading {$this->filename}" . \PHP_EOL;
}
 
/**
* Displays the image
*/
public function displayImage()
{
echo "Displaying {$this->filename}" . \PHP_EOL;
}
}
 
// On System B
class ProxyImage implements Image
{
private ?Image $image = null;
private string $filename = null;
 
public function __construct(string $filename)
{
$this->filename = $filename;
}
 
/**
* Displays the image
*/
public function displayImage()
{
if ($this->image === null) {
$this->image = new RealImage($this->filename);
}
$this->image->displayImage();
}
}
 
 
$image1 = new ProxyImage("HiRes_10MB_Photo1");
$image2 = new ProxyImage("HiRes_10MB_Photo2");
 
$image1->displayImage(); // Loading necessary
$image1->displayImage(); // Loading unnecessary
$image2->displayImage(); // Loading necessary
$image2->displayImage(); // Loading unnecessary
$image1->displayImage(); // Loading unnecessary
</syntaxhighlight>
 
The program's output is:
Loading HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Loading HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo1
 
=== C# ===
Line 503 ⟶ 425:
</syntaxhighlight>
 
Output
The program's output is:
Loading HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Line 510 ⟶ 432:
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo1
 
===JavaScript===
 
<syntaxhighlight lang=JavaScript>
// Driver class
class Driver {
constructor (age) {
this.age = age
}
}
 
// Car class
class Car {
drive () {
console.log('Car has been driven!')
}
}
 
// Proxy car class
class ProxyCar {
constructor (driver) {
this.car = new Car()
this.driver = driver
}
 
drive () {
if (this.driver.age <= 16) {
console.log('Sorry, the driver is too young to drive.')
} else {
this.car.drive()
}
}
}
 
// Run program
const driver = new Driver(16)
const car = new ProxyCar(driver)
car.drive()
 
const driver2 = new Driver(25)
const car2 = new ProxyCar(driver2)
car2.drive()
 
</syntaxhighlight>
 
Output
Sorry, the driver is too young to drive.
Car has been driven!
 
=== PHP ===
<syntaxhighlight lang="php">
<?php
 
interface Image
{
public function displayImage();
}
 
// On System A
class RealImage implements Image
{
private string $filename = null;
 
public function __construct(string $filename)
{
$this->filename = $filename;
$this->loadImageFromDisk();
}
 
/**
* Loads the image from the disk
*/
private function loadImageFromDisk()
{
echo "Loading {$this->filename}" . \PHP_EOL;
}
 
/**
* Displays the image
*/
public function displayImage()
{
echo "Displaying {$this->filename}" . \PHP_EOL;
}
}
 
// On System B
class ProxyImage implements Image
{
private ?Image $image = null;
private string $filename = null;
 
public function __construct(string $filename)
{
$this->filename = $filename;
}
 
/**
* Displays the image
*/
public function displayImage()
{
if ($this->image === null) {
$this->image = new RealImage($this->filename);
}
$this->image->displayImage();
}
}
 
 
$image1 = new ProxyImage("HiRes_10MB_Photo1");
$image2 = new ProxyImage("HiRes_10MB_Photo2");
 
$image1->displayImage(); // Loading necessary
$image1->displayImage(); // Loading unnecessary
$image2->displayImage(); // Loading necessary
$image2->displayImage(); // Loading unnecessary
$image1->displayImage(); // Loading unnecessary
</syntaxhighlight>
 
Output
Loading HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Loading HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo1
 
Line 561 ⟶ 611:
car.drive()
</syntaxhighlight>
 
Output
Sorry, the driver is too young to drive.
Car has been driven!
 
===Rust===
Line 572 ⟶ 626:
impl ICar for Car {
fn drive(&self) {
println!("brumCar brumhas been driven!");
}
}
Line 592 ⟶ 646:
self.real_car.drive();
} else {
println!("noSorry, brumthe brumdriver foris youtoo young to drive.")
}
}
Line 609 ⟶ 663:
mod tests {
use super::*;
 
#[test]
fn test_can_drivetest_underage() {
let car = Car::new();
let proxy_car = ProxyCar::new(1716, &car);
proxy_car.drive();
}
 
#[test]
fn test_underagetest_can_drive() {
let car = Car::new();
let proxy_car = ProxyCar::new(1617, &car);
proxy_car.drive();
}
}
</syntaxhighlight>
 
Output
Sorry, the car is to young for you to drive.
Car has been driven!
 
==See also==