Deputy (draft sample)

from Wikipedia, the free encyclopedia

The proxy also deputy called, is a design pattern in the field of software development , which the category of structural patterns ( English structural design patterns ) belongs. The pattern transfers the control of an object to an upstream representative object. It is a design pattern of the so-called Gang of Four .

A proxy in its most general form is a class that acts as an interface to a so-called subject. This subject can be, for example, a network connection, a large object in memory, a file, or another resource. As a representative of this subject, the proxy can control the creation of the subject and access to it.

use

The proxy has proven useful in various use cases. Depending on the use, a distinction is made between different types of substitute objects:

A remote proxy is a local representative for an object in a different address space . It is used, for example, in network applications or in DCOM .

A virtual substitute is used to delay complex operations until they are actually needed. Typical of such expensive operations are the creation or modification of a complex object.

A protective proxy is used to enforce access rights to an object . This is particularly useful when different accessing objects should have different access rights to the object to be protected. A specific example of protection proxies are kernel proxies that control access to operating system objects.

Substitutes are also used to link further operations to the actual access to the object. The object remains independent of these operations. The term Smart References has become established for this type of representative . Counting references and persistence operations are typical use cases.

UML diagram

UML diagram Stellvertreter.png

client

The client represents the object that accesses the real subject through the representative.

Deputy

Outwardly, the representative offers an interface that is identical to the real subject. He manages a reference to this and is possibly also responsible for its creation and deletion. Further responsibilities arise from the type of deputy.

subject

The subject defines the common interface between the representative and the real subject. This enables the use of proxies instead of real subjects.

Real subject

The real subject is the object represented by the representative.

Examples

password protection

Password protection of some methods within a class, e.g. B. Class Konto(with methods einzahlenand auszahlen).

The proxy is a new class ( KontoMitPasswort) → association with the old Kontoclass. The methods in the proxy class ask the user for a password and then call the methods of the class Konto(if the password is correct).

Remote access

Java RMI is a way of accessing remote objects (i.e. those running in a different JVM ), whereby access does not differ from that of local objects. This is achieved by so-called stubs and skeletons , which implement the interface of the respective communication partner in accordance with the proxy design pattern and forward the method call to them (usually via a network).

object oriented programing

In the object-oriented environment , the deputy thus allows the object initialization to be separated from the object creation. This reduces the cost of accessing an object and creates local independence and transparency.

Another application example: combination with flyweight

In situations in which several copies of a complex object must exist, the proxy design pattern can be combined with what is known as the flyweight design pattern in order to reduce memory requirements. Typically only one instance of the complex object is generated, as well as several smaller proxy objects that refer to this object and act as an interface or representative. All operations on the proxy objects are forwarded to the original object. If there are no more instances of the proxy, the original object can also be removed from the memory.

Programming example for needs evaluation in PHP

// Subjekt-Interface: Der Klient hängt nur von dieser Abstraktion ab.

interface Bild {
    public function getBreite();
    public function getHoehe();
    public function getPfad();

    public function Inhalt();
}

// gemeinsame Elemente des echten Subjekts und des Stellvertreters werden hier zusammengefasst.

abstract class AbstraktesBild implements Bild {
    protected $_Breite;
    protected $_Hoehe;
    protected $_Pfad;
    protected $_Daten;

    public function getBreite() {
        return $this->_Breite;
    }

    public function getHoehe() {
        return $this->_Hoehe;
    }

    public function getPfad() {
        return $this->_Pfad;
    }
}

// echtes Subjekt

class EchtesBild extends AbstraktesBild {
    public function __construct($Pfad) {
        $this->_Pfad = $Pfad;
        list ($this->_Breite, $this->_Hoehe) = getimagesize($Pfad);
        $this->_Daten = file_get_contents($Pfad);
    }

    public function Inhalt() {
        return $this->_Daten;
    }
}

// Stellvertreter. Lädt das Bild erst bei Bedarf.

class BildStellvertreter extends AbstraktesBild {
    public function __construct($Pfad) {
        $this->_Pfad = $Pfad;
        list ($this->_Breite, $this->_Hoehe) = getimagesize($Pfad);
    }
    protected function _BedarfsLaden() {
        if ($this->_echtesBild === null) {
            $this->_echtesBild = new EchtesBild($this->_Pfad);
        }
    }
    public function Inhalt() {
        $this->_BedarfsLaden();
        return $this->_echtesBild->Inhalt();
    }
}

// Klient

class Klient {
    public function HtmlImg(Bild $img) {
        return '<img src="' . $img->getPfad() . '" alt="" width="50" height="50" />';
    }
}

function Test() {
    $Pfad = 'http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png';
    $klient = new Klient();

    $image = new EchtesBild($Pfad);
    echo $klient->HtmlImg($image), "\n";

    $proxy = new BildStellvertreter($Pfad);
    echo $klient->HtmlImg($proxy), "\n";
}

Test();

Related design patterns

Web links

Commons : alternate (design pattern)  - collection of images, videos and audio files

Individual evidence

  1. Erich Gamma , Richard Helm , Ralph Johnson , John Vlissides : Design pattern . 5th edition. Addison-Wesley, 1996, ISBN 3-8273-1862-9 , pp. 254 .