<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>webdeveloper.franzis.de &#187; PHP 5.x</title>
	<atom:link href="http://webdeveloper.franzis.de/category/php-5x/feed/" rel="self" type="application/rss+xml" />
	<link>http://webdeveloper.franzis.de</link>
	<description>Webdevelopment und Webdesign</description>
	<lastBuildDate>Wed, 08 Sep 2010 15:53:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Einfache Plugins für WordPress 3 erstellen</title>
		<link>http://webdeveloper.franzis.de/php-5x/einfache-plugins-fur-wordpress-erstellen/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/einfache-plugins-fur-wordpress-erstellen/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 15:40:01 +0000</pubDate>
		<dc:creator>Jonas Hellwig und Christian Gatzen</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS-Datei]]></category>
		<category><![CDATA[Filter-Hook]]></category>
		<category><![CDATA[Hauptordner]]></category>
		<category><![CDATA[Header]]></category>
		<category><![CDATA[Klasse .wpstyled]]></category>
		<category><![CDATA[PHP-Funktion]]></category>
		<category><![CDATA[PHP-Variable]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Plugin aktivieren]]></category>
		<category><![CDATA[Plugin-Datei]]></category>
		<category><![CDATA[Plugin-Hook]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=2660</guid>
		<description><![CDATA[Plugins fremder Programmierer zu installieren, ist vergleichsweise einfach. Interessant und weit in die Materie von WordPress eingreifend, ist es allerdings, Plugins selbst herzustellen. Im Folgenden erklären wir Ihnen, wie Sie ein einfaches Plugin selbst realisieren. Unser Plugin ersetzt immer ein bestimmtes Wort in Ihren Blog-Beiträgen durch einen bestimmten HTML-Code. Ziel ist es, dass jedes Mal, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Plugins fremder Programmierer zu installieren, ist vergleichsweise einfach. Interessant und weit in die Materie von WordPress eingreifend, ist es allerdings, Plugins selbst herzustellen. Im Folgenden erklären wir Ihnen, wie Sie ein einfaches Plugin selbst realisieren. Unser Plugin ersetzt immer ein bestimmtes Wort in Ihren Blog-Beiträgen durch einen bestimmten HTML-Code. Ziel ist es, dass jedes Mal, wenn Sie WP schreiben, auf der Seite <em>&lt;span class=&#8217;wpstyled&#8217;&gt;WordPress&lt;/span&gt;</em> ausgegeben wird.</p>
<p><span id="more-2660"></span></p>
<p style="text-align: justify;">Wir bedienen uns des Plugin-Hooks <em>the-content</em>, da der Inhalt durchsucht werden muss, um dort die Wörter zu ersetzen. Da Sie später einen &lt;span&gt; mit einer Klasse <em>wpstyled </em>ausgeben, haben Sie auch die Möglichkeit, die Ausgabe grafisch über CSS zu steuern. Zunächst erstellen Sie eine Datei mit dem Namen <em>wpErsetzen.php </em>und legen diese in den Hauptordner von <em>wp-content/plugins/</em>. Da Sie in diesem Fall nur eine Datei benötigen, können Sie auf einen Unterordner für das Plugin verzichten. WordPress stellt im Backend beim Aktivieren der Plugins diverse Informationen über den Autoren, das Plugin selbst und die Version bereit. Diese Informationen bezieht WordPress<br />
aus dem Header der Plugin-Datei. Unser Header könnte zum Beispiel so aussehen:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Plugin Name: WP-Text ersetzen
Plugin URI: http://www.domain.tld
Description: Dieses Plugin ersetzt den Text &quot;WP&quot; durch &lt;span
class='wpstyled'&gt;WordPress&lt;/span&gt;
Author: WordPress für Webdesigner
Version: 1.0
Author URI: http://www.domain.tld
*/</span></pre></div></div>

<p style="text-align: justify;">Danach folgt die eigentliche Programmierung des Plugins. Zunächst erstellen Sie eine Funktion mit dem Namen <em>wpErsetzen</em> und füllen sie mit nachfolgendem Code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> wpErsetzen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$suche</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'WP'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ersetze</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;span class=&quot;wpstyled&quot;&gt;WordPress&lt;/span&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$suche</span><span style="color: #339933;">,</span><span style="color: #000088;">$ersetze</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Zur Erklärung: Sie geben der Variablen <em>$suche</em> den Wert <em>WP</em>, nach dem gesucht werden soll. Der Variablen <em>ersetze</em> weisen Sie den einzusetzenden Codeblock zu. Anschließend suchen und ersetzen Sie den Inhalt von <em>$content</em> mit der PHP-Funktion <em>str_replace.$content</em>. Der wird uns später vom Filter-Hook selbst übergeben und enthält den Blog-Beitrag. Als Letztes fehlt noch der sogenannte Filter-Hook, den Sie direkt unter die Funktion schreiben können:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;the_content&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;wpErsetzen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p style="text-align: justify;">Der Filter wird immer dann ausgeführt, wenn die WordPress-Funktion <em>the-content </em>aufgerufen wird. Als Inhalt übergibt der Filter die Ausgabe von <em>the-content</em>, also den Inhalt des Blog-Beitrags oder einer statischen Seite, und nimmt den ersetzten Inhalt wieder auf, um ihn später auf der Seite auszugeben. Nun sind Sie auch schon fertig mit dem ersten Mini-Plugin und werden jetzt noch die Klasse <em>.wpstyled</em>, die Sie im ersetzten Text genutzt haben, in der CSS-Datei des Themes definieren:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>wpstyled <span style="color: #009900;">&#123;</span>
background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#ffff66;
</span>padding<span style="color: #339933;">:</span> 2px 3px<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Der Text <em>WordPress </em>wird nun auf allen Seiten mit einem gelben Hintergrund dargestellt. Noch sehen Sie im Frontend keine Änderungen. Sie müssen zunächst das Plugin aktivieren. Dazu gehen Sie genauso vor, wie mit einem heruntergeladenen externen Plugin. Auf der Plugin-Seite im Backend fi nden Sie jetzt das von uns neu hinzugefügte Plugin.</p>
<p style="text-align: center;"><a href="http://webdeveloper.franzis.de/wp-content/uploads/2010/08/wpplugin.png"><img class="aligncenter size-full wp-image-2661" title="wpplugin" src="http://webdeveloper.franzis.de/wp-content/uploads/2010/08/wpplugin.png" alt="" width="592" height="319" /></a></p>
<p style="text-align: justify;">Wenn Sie nun auf <em>Aktivieren </em>klicken und das Frontend aktualisieren, sehen Sie die von Ihnen durchgeführte automatische Textersetzung mit einem <em>WordPress</em>, das einen gelben Hintergrund hat, da wir im Text <em>WP </em>geschrieben haben.</p>
<p style="text-align: justify;"><a href="http://www.amazon.de/WordPress-f%C3%BCr-Webdesigner-Christian-Gatzen/dp/3645600256/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1281972849&amp;sr=8-1"><img class="aligncenter size-full wp-image-2667" title="60025-5" src="http://webdeveloper.franzis.de/wp-content/uploads/2010/08/60025-5.png" alt="" width="213" height="304" /></a></p>
<p>Dieser Text ist dem Buch “WordPress-Themes – Design und Implementierung  für WordPress 3” von Jonas Hellwig und Christian Gatzen entnommen, das  im August  2010 im Franzis Verlag erscheint. Sie können es<a href="http://www.amazon.de/s/275-5139150-7287023?ie=UTF8&amp;tag=firefox-de-21&amp;index=blended&amp;link_code=qs&amp;field-keywords=hellwig%20gatzen&amp;sourceid=Mozilla-search"> hier direkt</a> vorbestellen.</p>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/einfache-plugins-fur-wordpress-erstellen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Das Subject/Observer-Muster in PHP</title>
		<link>http://webdeveloper.franzis.de/php-5x/das-subjectobserver-muster-in-php/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/das-subjectobserver-muster-in-php/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 16:54:40 +0000</pubDate>
		<dc:creator>Matthias Kannengiesser</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[Beobachter]]></category>
		<category><![CDATA[Entwurfsmuster]]></category>
		<category><![CDATA[Interface]]></category>
		<category><![CDATA[Observer]]></category>
		<category><![CDATA[Schnittstelle]]></category>
		<category><![CDATA[Subject]]></category>
		<category><![CDATA[Subject/Observer]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=1576</guid>
		<description><![CDATA[Das Subject/Observer-Entwurfsmuster gehört zur Gruppe der Verhaltensmuster. Dieses Entwurfsmuster ist aufgeteilt in einen Beobachter (Observer) und ein Subjekt (Subject), das überwacht werden soll. Das Entwurfsmuster definiert dabei eine Eins-zu-n-Abhängigkeit zwischen einem Subjekt und beliebig vielen Beobachtern. Wenn sich der Zustand des Objekts ändert, werden die unabhängigen Objekte automatisch benach­richtigt.
Die Konventionen für das Subject/Observer-Modell
Subject

Definiert die Schnittstelle für [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Das <em>Subject/Observer</em>-Entwurfsmuster gehört zur Gruppe der Verhaltensmuster. Dieses Entwurfsmuster ist aufgeteilt in einen Beobachter (<em>Observer</em>) und ein Subjekt (<em>Subject</em>), das überwacht werden soll. Das Entwurfsmuster definiert dabei eine <em>Eins-zu-n</em>-Abhängigkeit zwischen einem Subjekt und beliebig vielen Beobachtern. Wenn sich der Zustand des Objekts ändert, werden die unabhängigen Objekte automatisch benach­richtigt.</p>
<p style="text-align: justify;"><a href="http://webdeveloper.franzis.de/wp-content/uploads/2010/02/phpoop05_07.jpg"><img class="alignleft size-full wp-image-1577" title="phpoop05_07" src="http://webdeveloper.franzis.de/wp-content/uploads/2010/02/phpoop05_07.jpg" alt="" width="566" height="271" /></a><span id="more-1576"></span>Die Konventionen für das Subject/Observer-Modell</p>
<h5>Subject</h5>
<ul>
<li>Definiert die Schnittstelle für das zu überwachende Objekt.</li>
<li>Definiert die Schnittstelle für die Überwacherobjekte.</li>
</ul>
<h5>Observer</h5>
<ul>
<li>Definiert die Schnittstelle für die Überwacherobjekte.</li>
</ul>
<h5>ConcreteSubject</h5>
<ul>
<li>Enthält die konkrete Objekt-Implementierung des zu überwachenden Objekts.</li>
<li>Enthält die konkrete Objekt-Implementierung für die Überwacherobjekte.</li>
</ul>
<h5>ConcreteObserver</h5>
<p style="text-align: justify;">Um ein Subject/Observer-Entwurfsmuster in PHP zu implementieren, führen Sie folgende Schritte durch:</p>
<p style="text-align: justify;">1.  Definieren Sie eine Schnittstelle für die Observer, die eine Methode bereitstellt, mit der die Beobachter über eine Statusänderung informiert werden können.</p>
<p style="text-align: justify;">2.  Definieren Sie eine Schnittstelle für die zu beobachtenden Objekte (Subjects). Diese Schnittstelle muss das Hinzufügen sowie das Entfernen von Observern erlauben und eine Methode notify() fordern, mit der die Observer über eine Statusänderung informiert werden.</p>
<p style="text-align: justify;">3.  Implementieren Sie im zu beobachtenden Objekt die Subject-Schnittstelle.</p>
<p style="text-align: justify;">4.  Fügen Sie in den Methoden, die den Zustand des Objekts ändern, den Aufruf der <em>notify()</em>-Methode ein.</p>
<p>5.  Implementieren Sie konkrete Objekte.</p>
<p>Wenn Sie diese Schritte befolgen, steht dem Einsatz von <em>Subject/Observer</em>-Entwurfs­mustern nichts im Wege.</p>
<p><strong>Beispieldatei PersonalLift.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Fahrstuhl.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Subject.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> PersonalLift implements Fahrstuhl<span style="color: #339933;">,</span> Subject 
<span style="color: #009900;">&#123;</span>  
    protected <span style="color: #000088;">$hersteller</span><span style="color: #339933;">;</span> 
    protected <span style="color: #000088;">$model</span><span style="color: #339933;">;</span>   
    protected <span style="color: #000088;">$meter</span><span style="color: #339933;">;</span>
&nbsp;
    protected <span style="color: #000088;">$generatorAktiv</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    protected <span style="color: #000088;">$observers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$hersteller</span><span style="color: #339933;">,</span> <span style="color: #000088;">$model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$meter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hersteller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$hersteller</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$model</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$meter</span><span style="color: #339933;">;</span>       
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generatorAktiv</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
        	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stopGenerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startGenerator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generatorAktiv</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fahren<span style="color: #009900;">&#40;</span><span style="color: #000088;">$meter</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generatorAktiv</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
        	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meter</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$meter</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopGenerator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generatorAktiv</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getHersteller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hersteller</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getModel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>  
&nbsp;
	 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMeter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meter</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>  
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> attach<span style="color: #009900;">&#40;</span>Observer <span style="color: #000088;">$observer</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">observers</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$observer</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> detach<span style="color: #009900;">&#40;</span>Observer <span style="color: #000088;">$observer</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>		
		<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">observers</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
		  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">observers</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$observer</span><span style="color: #009900;">&#41;</span> 
		  <span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">observers</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>		
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> notify<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">observers</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$observer</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
        	<span style="color: #000088;">$observer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Beispieldatei Fahrstuhl.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> Fahrstuhl 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startGenerator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fahren<span style="color: #009900;">&#40;</span><span style="color: #000088;">$meter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopGenerator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMeter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getHersteller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getModel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Beispieldatei Subject.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> Subject 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> attach<span style="color: #009900;">&#40;</span>Observer <span style="color: #000088;">$observer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> detach<span style="color: #009900;">&#40;</span>Observer <span style="color: #000088;">$observer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> notify<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Beispieldatei Observer.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> Observer 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #009900;">&#40;</span>Subject <span style="color: #000088;">$subject</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Beispieldatei ErsteWartungObserver.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Observer.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ErsteWartungObserver implements Observer 
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$wartungMeter</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$meter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wartungMeter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$meter</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #009900;">&#40;</span>Subject <span style="color: #000088;">$lift</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$lift</span> instanceof Fahrstuhl<span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMeter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wartungMeter</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Die erste Wartung ist fällig, da &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wartungMeter</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; Meter überschritten wurden.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>			
            <span style="color: #000088;">$lift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">detach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Beispieldatei StandardWartungObserver.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Observer.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> StandardWartungObserver implements Observer 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$interval</span><span style="color: #339933;">;</span>
	 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$naechsteWartung</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$startWert</span><span style="color: #339933;">,</span> <span style="color: #000088;">$interval</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">naechsteWartung</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$startWert</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">interval</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$interval</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #009900;">&#40;</span>Subject <span style="color: #000088;">$lift</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$lift</span> instanceof Fahrstuhl<span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMeter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">naechsteWartung</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Die Standardwartung ist fällig (&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">naechsteWartung</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; Meter).<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">naechsteWartung</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">naechsteWartung</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">interval</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Beispieldatei &#8211; Anwendung</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PersonalLift.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'ErsteWartungObserver.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pLift</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PersonalLift<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'OTIS'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'High-8-Personenlift'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ersteWartung</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ErsteWartungObserver<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ersteWartung</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">startGenerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fahren</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gefahren: <span style="color: #009933; font-weight: bold;">%d</span> Meter<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMeter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fahren</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gefahren: <span style="color: #009933; font-weight: bold;">%d</span> Meter<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMeter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stopGenerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">startGenerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fahren</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gefahren: <span style="color: #009933; font-weight: bold;">%d</span> Meter<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMeter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pLift</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stopGenerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Ausgabe</strong></p>
<pre>
Gefahren: 3500 Meter
Die erste Wartung ist fällig, da 10000 Meter überschritten wurden.
Gefahren: 11500 Meter
Gefahren: 21500 Meter
</pre>
<p><a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung"><img class="alignleft size-full wp-image-1268" title="6299" src="http://webdeveloper.franzis.de/wp-content/uploads/2009/12/6299.jpg" alt="" width="213" height="304" /></a></p>
<p>Dieser Abschnitt wurde dem Buch “PHP 5 – Objektorientierte Programmierung” von Matthias Kannengiesser entnommen, das im Franzis Verlag erschienen ist. Sie können es <a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung">hier </a>direkt bestellen.</p>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/das-subjectobserver-muster-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Das Composite-Entwurfsmuster in PHP</title>
		<link>http://webdeveloper.franzis.de/php-5x/das-composite-entwurfsmuster-in-php/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/das-composite-entwurfsmuster-in-php/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 17:10:24 +0000</pubDate>
		<dc:creator>Matthias Kannengiesser</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Composite-Entwurfsmuster]]></category>
		<category><![CDATA[Kindobjekt]]></category>
		<category><![CDATA[Methodenaufruf]]></category>
		<category><![CDATA[Objektbaim]]></category>
		<category><![CDATA[Schnittstelle]]></category>
		<category><![CDATA[Strukturmuster]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=1533</guid>
		<description><![CDATA[Das Composite-Entwurfsmuster gehört zur Gruppe der Strukturmuster. Das Composite-Entwurfsmuster fügt mehrere Objekte zu einer Baumstruktur zusammen und ermöglicht es, sie von außen wie ein einzelnes zu verwenden.

Um ein Composite-Entwurfsmuster in PHP zu implementieren, führen Sie folgende Schritte durch:
1.  Definieren Sie eine Schnittstelle für ein einzelnes Objekt des Baums, falls sie nicht bereits zuvor definiert wurde.
2.  [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Das <em>Composite</em>-Entwurfsmuster gehört zur Gruppe der Strukturmuster. Das Composite-Entwurfsmuster fügt mehrere Objekte zu einer Baumstruktur zusammen und ermöglicht es, sie von außen wie ein einzelnes zu verwenden.</p>
<p style="text-align: justify;"><a href="http://webdeveloper.franzis.de/wp-content/uploads/2010/02/phpoop05_06.jpg"><img class="alignleft size-full wp-image-1534" title="phpoop05_06" src="http://webdeveloper.franzis.de/wp-content/uploads/2010/02/phpoop05_06.jpg" alt="" width="555" height="381" /></a><span id="more-1533"></span></p>
<p style="text-align: justify;">Um ein Composite-Entwurfsmuster in PHP zu implementieren, führen Sie folgende Schritte durch:</p>
<p style="text-align: justify;">1.  Definieren Sie eine Schnittstelle für ein einzelnes Objekt des Baums, falls sie nicht bereits zuvor definiert wurde.</p>
<p style="text-align: justify;">2.  Implementieren Sie eine neue Klasse, die diese Schnittstelle erfüllt. Fügen Sie dieser Klasse eine weitere Methode hinzu, an die Sie beliebige Objekte übergeben können, die die Schnittstelle erfüllen. Speichen Sie die übergebenen Objekte als Array in einer Objekteigenschaft.</p>
<p style="text-align: justify;">3.  Implementieren Sie sämtliche von der Schnittstelle geforderten Methoden so, dass diese über die Kindobjekte iterieren und den Methodenaufruf der Reihe nach an jedes der Kinder weiterdelegieren.</p>
<p style="text-align: justify;">4.  Erzeugen Sie beliebige Objekte, um diese zu einem Baum zusammenzufügen.</p>
<p style="text-align: justify;">5.  Verwenden Sie in Ihrer Anwendung nun diesen Objektbaum anstelle eines einzel­nen Objekts.</p>
<p style="text-align: justify;">Wenn Sie diese Schritte befolgen, steht dem Einsatz von <em>Composite</em>-Entwurfsmustern nichts im Wege.</p>
<p style="text-align: justify;">Mithilfe des folgenden Anwendungsbeispiels können Sie mehrere Filme in einer Filmliste verwalten. Die Klasse <em>EinFilm </em>stellt jeweils ein konkretes Objekt dar. Die Klasse <em>Diver­seFilme </em>ist eine Gruppe von <em>EinFilm</em>-Objekten.</p>
<p style="text-align: justify;">Beide Klassen können Filminformationen über den Filmtitel und Filmregie ausgeben. EinFilm enthält lediglich Informationen über einen Film, <em>DiverseFilme </em>enthält Infor­ma­tionen über die Filme in der Filmliste.</p>
<p style="text-align: justify;">Beide Klassen verfügen über die Methoden <em>hinzufuegen() </em>und <em>entfernen()</em>. Diese sind jedoch lediglich in der Klasse <em>DiverseFilme </em>einsetzbar. Die <em>EinFilm</em>-Klasse liefert <em>FALSE</em>, falls diese Methoden verwendet werden sollten.</p>
<h5>Beispiel – FilmArchiv.php</h5>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
abstract <span style="color: #000000; font-weight: bold;">class</span> FilmArchiv 
<span style="color: #009900;">&#123;</span>
	abstract protected <span style="color: #000000; font-weight: bold;">function</span> getFilmInfo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vorherigerFilm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	abstract protected <span style="color: #000000; font-weight: bold;">function</span> getFilmAnzahl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	abstract protected <span style="color: #000000; font-weight: bold;">function</span> setFilmAnzahl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$neuer_zaehler</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	abstract protected <span style="color: #000000; font-weight: bold;">function</span> hinzufuegen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$einFilm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	abstract protected <span style="color: #000000; font-weight: bold;">function</span> entfernen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$einFilm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h5>Beispiel – EinFilm.php</h5>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FilmArchiv.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> EinFilm <span style="color: #000000; font-weight: bold;">extends</span> FilmArchiv 
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$titel</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$regie</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$titel</span><span style="color: #339933;">,</span> <span style="color: #000088;">$regie</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">titel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$titel</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">regie</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$regie</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFilmInfo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$film_auswahl</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$film_auswahl</span><span style="color: #009900;">&#41;</span> 
	  <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">titel</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; von &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">regie</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
	  <span style="color: #b1b100;">else</span> 
	  <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFilmAnzahl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setFilmAnzahl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$neuer_zaehler</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> hinzufuegen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$einFilm</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> entfernen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$einFilm</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h5>Beispiel – DiverseFilme.php</h5>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FilmArchiv.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DiverseFilme <span style="color: #000000; font-weight: bold;">extends</span> FilmArchiv 
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$filmListe</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$filmAnzahl</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFilmAnzahl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filmAnzahl</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setFilmAnzahl<span style="color: #009900;">&#40;</span><span style="color: #000088;">$neuer_zaehler</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filmAnzahl</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$neuer_zaehler</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFilmInfo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$film_auswahl</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>	
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$film_auswahl</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filmAnzahl</span><span style="color: #009900;">&#41;</span> 
	  <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filmListe</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$film_auswahl</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
	  <span style="color: #b1b100;">else</span> 
	  <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> hinzufuegen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$einFilm</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filmListe</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$einFilm</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> entfernen<span style="color: #009900;">&#40;</span><span style="color: #000088;">$einFilm</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span><span style="color: #000088;">$counter</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
	  <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$einFilm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filmListe</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$counter</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
		  <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$counter</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> 
		  <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filmListe</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filmListe</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> – <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h5>Beispiel – Anwendung</h5>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'FilmArchiv.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'EinFilm.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DiverseFilme.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
&nbsp;
<span style="color: #000088;">$ersterFilm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EinFilm<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;King Kong&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;James Cameron&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Erster Film – Filminfo: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ersterFilm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$zweiterFilm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EinFilm<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Star Wars&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;George Lucas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Zweiter Film – Filminfo: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$zweiterFilm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$dritteFilm</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EinFilm<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Indiana Jones&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Steven Spielberg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Dritter Film – Filminfo: &quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dritteFilm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;***Filmliste***&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$filme</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DiverseFilme<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$filmeCount</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hinzufuegen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ersterFilm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Erster Film in der Filmliste – Filminfo: &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filmeCount</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$filmeCount</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hinzufuegen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$zweiterFilm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Zweiter Film in der Filmliste – Filminfo: &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filmeCount</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$filmeCount</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hinzufuegen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dritteFilm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Dritter Film in der Filmliste – Filminfo: &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filmeCount</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$filmeCount</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entfernen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ersterFilm</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Ersten Film aus Filmliste entfernen – Filme Anzahl: &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmAnzahl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Übrige Filme aus der Filmliste – Filminfo zu 1: &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Übrige Filme aus der Filmliste – Filminfo zu 2: &lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFilmInfo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;br&gt;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h5>Ausgabe</h5>
<pre>
Erster Film – Filminfo: King Kong von James Cameron
Zweiter Film – Filminfo: Star Wars von George Lucas
Dritter Film – Filminfo: Indiana Jones von Steven Spielberg
***Filmliste***
Erster Film in der Filmliste – Filminfo:
King Kong von James Cameron
Zweiter Film in der Filmliste – Filminfo:
Star Wars von George Lucas
Dritter Film in der Filmliste – Filminfo:
Indiana Jones von Steven Spielberg
Ersten Film aus Filmliste entfernen – Filme Anzahl:
2
Übrige Filme aus der Filmliste – Filminfo zu 1:
Star Wars von George Lucas
Übrige Filme aus der Filmliste – Filminfo zu 2:
Indiana Jones von Steven Spielberg
</pre>
<p><a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung"><img class="alignleft size-full wp-image-1268" title="6299" src="http://webdeveloper.franzis.de/wp-content/uploads/2009/12/6299.jpg" alt="" width="213" height="304" /></a></p>
<p>Dieser Abschnitt wurde dem Buch “PHP 5 – Objektorientierte Programmierung” von Matthias Kannengiesser entnommen, das im Franzis Verlag erschienen ist. Sie können es <a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung">hier </a>direkt bestellen.</p>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/das-composite-entwurfsmuster-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Das Factory-Entwurfsmuster in PHP</title>
		<link>http://webdeveloper.franzis.de/php-5x/das-factory-entwurfsmuster-in-php/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/das-factory-entwurfsmuster-in-php/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 17:05:34 +0000</pubDate>
		<dc:creator>Matthias Kannengiesser</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[abstrakte Klasse]]></category>
		<category><![CDATA[abstrakte Methode]]></category>
		<category><![CDATA[Entwurfsmuster]]></category>
		<category><![CDATA[Erzeugungsmuster]]></category>
		<category><![CDATA[Fabrik]]></category>
		<category><![CDATA[Fabrikmethode]]></category>
		<category><![CDATA[Factory]]></category>
		<category><![CDATA[Factory-Methode]]></category>
		<category><![CDATA[Instanziierung]]></category>
		<category><![CDATA[Koppelung]]></category>
		<category><![CDATA[lose Koppelung]]></category>
		<category><![CDATA[Objekt]]></category>
		<category><![CDATA[Unterklasse]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=1473</guid>
		<description><![CDATA[Die Factory-Methode gehört zur Gruppe der Erzeugungsmuster. Das Entwurfsmus­ter definiert eine Schnittstelle zur Erzeugung von Objekten. Es verlagert dabei die eigentliche Instanzierung in Unterklassen und lässt die Unterklassen entscheiden, welche konkreten Implementierungen verwendet werden.


Konventionen der Factory-Methode:
Um ein Factory-Methoden-Entwurfsmuster in PHP zu implementieren, führen Sie folgende Schritte durch:
1.  Implementieren Sie eine abstrakte Klasse, in der Sie eine [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Die <em>Factory-Methode</em> gehört zur Gruppe der Erzeugungsmuster. Das Entwurfsmus­ter definiert eine Schnittstelle zur Erzeugung von Objekten. Es verlagert dabei die eigentliche Instanzierung in Unterklassen und lässt die Unterklassen entscheiden, welche konkreten Implementierungen verwendet werden.</p>
<p style="text-align: justify;"><a href="http://webdeveloper.franzis.de/wp-content/uploads/2010/01/phpoop05_05.jpg"><img class="alignleft size-full wp-image-1474" title="phpoop05_05" src="http://webdeveloper.franzis.de/wp-content/uploads/2010/01/phpoop05_05.jpg" alt="" width="592" height="290" /></a></p>
<p style="text-align: justify;"><span id="more-1473"></span></p>
<p style="text-align: justify;"><strong>Konventionen der Factory-Methode:</strong></p>
<p>Um ein Factory-Methoden-Entwurfsmuster in PHP zu implementieren, führen Sie folgende Schritte durch:</p>
<p>1.  Implementieren Sie eine abstrakte Klasse, in der Sie eine oder mehrere abstrakte Methoden deklarieren, die die Schnittstelle zum Erzeugen von Objekten vorgeben.</p>
<p>2.  Fügen Sie dieser Klasse weitere Methoden hinzu, deren Logik bei allen konkreten Implementierungen identisch ist. Sie können in diesen Methoden bereits auf die abstrakte Fabrikmethode zugreifen.</p>
<p>3.  Bilden Sie beliebig viele Unterklassen, in denen Sie verschiedene Implementierungen der abstrakten Methode einfügen.</p>
<p>4.  Verwenden Sie nun diese konkreten Unterklassen, um die tatsächlichen Objekte zu instanzieren und Ihren Applikationscode von den konkreten Implementierungen zu lösen.</p>
<p>Wenn Sie diese Schritte befolgen, steht dem Einsatz von Factory-Methoden-Entwurfs­mustern nichts im Wege.</p>
<p style="text-align: justify;">Mithilfe des folgenden Anwendungsbeispiels können Sie einen »virtuellen« Verlag erzeugen. Dieser ist in der Lage, beliebig viele Bücher zu produzieren.</p>
<p style="text-align: justify;"><strong>Beispiel &#8211; Buch-Klasse (buch.php)</strong></p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Produkte.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Buch implements Produkte <span style="color: #009900;">&#123;</span>
&nbsp;
	protected <span style="color: #000088;">$verlag</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$titel</span><span style="color: #339933;">;</span>   
	protected <span style="color: #000088;">$preis</span><span style="color: #339933;">;</span>	
	protected <span style="color: #000088;">$verkauft</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$verpackt</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$verlag</span><span style="color: #339933;">,</span> <span style="color: #000088;">$titel</span><span style="color: #339933;">,</span> <span style="color: #000088;">$preis</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verlag</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$verlag</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">titel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$titel</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">preis</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$preis</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> berechneMwst<span style="color: #009900;">&#40;</span><span style="color: #000088;">$steuersatz</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">preis</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> 
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>			
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">preis</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">preis</span> <span style="color: #339933;">+</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">preis</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$steuersatz</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> verpacken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verpackt</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verpackt</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getVerlag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verlag</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTitel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">titel</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPreis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">preis</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getVerpackt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verpackt</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</p>
<p style="text-align: justify;"><strong>Beispiel &#8211; Produkte-Interface (Produkte.php)</strong></p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> Produkte 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> berechneMwst<span style="color: #009900;">&#40;</span><span style="color: #000088;">$steuersatz</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> verpacken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	 
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getVerlag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getTitel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPreis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getVerpackt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</p>
<p style="text-align: justify;"><strong>Beispiel &#8211; AbstractHersteller.php</strong></p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
abstract <span style="color: #000000; font-weight: bold;">class</span> AbstractHersteller 
<span style="color: #009900;">&#123;</span>
&nbsp;
    protected <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> verkaufeBuch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$titel</span><span style="color: #339933;">,</span> <span style="color: #000088;">$preis</span><span style="color: #339933;">,</span> <span style="color: #000088;">$steuersatz</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$produkt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">produziereBuch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$titel</span><span style="color: #339933;">,</span> <span style="color: #000088;">$preis</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$produkt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verpacken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
        <span style="color: #000088;">$produkt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">berechneMwst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$steuersatz</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$produkt</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    abstract protected <span style="color: #000000; font-weight: bold;">function</span> produziereBuch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$titel</span><span style="color: #339933;">,</span> <span style="color: #000088;">$preis</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</p>
<p style="text-align: justify;"><strong>Beispiel &#8211; BuchHersteller.php</strong></p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'AbstractHersteller.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Buch.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> BuchHersteller <span style="color: #000000; font-weight: bold;">extends</span> AbstractHersteller 
<span style="color: #009900;">&#123;</span>
&nbsp;
    protected <span style="color: #000000; font-weight: bold;">function</span> produziereBuch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$titel</span><span style="color: #339933;">,</span> <span style="color: #000088;">$preis</span><span style="color: #009900;">&#41;</span> 
	 <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$produkt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Buch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$titel</span><span style="color: #339933;">,</span> <span style="color: #000088;">$preis</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$produkt</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</p>
<p style="text-align: justify;"><strong>Beispiel &#8211; Anwendung</strong></p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'BuchHersteller.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$hersteller</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BuchHersteller<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Franzis'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$flash8Buch</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$hersteller</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">verkaufeBuch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Flash 8'</span><span style="color: #339933;">,</span> <span style="color:#800080;">45.00</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.07</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;***Buch gekauft***&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Produktart: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$flash8Buch</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Verlag: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flash8Buch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getVerlag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Titel: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flash8Buch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTitel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Preis: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flash8Buch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPreis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; (inkl 7% MwSt.)&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$flash8Buch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getVerpackt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Verpackungszustand: Verpackt!&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Verpackungszustand: nicht Verpackt!&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</p>
<p style="text-align: justify;"><strong>Ausgabe:</strong></p>
<p style="text-align: justify;">
<pre>
***Buch gekauft***
Produktart: Buch
Verlag : Franzis
Titel : Flash 8
Preis : 48.15 (inkl 7% MwSt.)
Verpackungszustand : Verpackt!
</pre>
</p>
<p style="text-align: justify;">Sie können ohne weiteres noch einen Hersteller hinzufügen. Sie müssen hierfür lediglich noch eine Klasse von <em>Abstract­Hersteller </em>ableiten.</p>
<p style="text-align: justify;">Weitere Hersteller können somit ohne Weiteres implemen­tiert und der Anwendung hinzugefügt werden. Sie haben ganz einfach das Erzeugen der <em>Buch</em>-Objekte aus dem Code entfernt und stattdessen hinter einer Schnittstelle gekapselt. Dabei verwenden Sie lediglich die Schnittstelle der Klasse <em>AbstractHerstel­ler</em>. Sie müssen nicht wissen, welche Klassen dabei instanziert werden und welcher Code dazu notwendig ist.</p>
<p style="text-align: justify;"><a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung"><img class="alignleft size-full wp-image-1268" title="6299" src="http://webdeveloper.franzis.de/wp-content/uploads/2009/12/6299.jpg" alt="" width="213" height="304" /></a></p>
<p style="text-align: justify;">Dieser Abschnitt wurde dem Buch “PHP 5 – Objektorientierte Programmierung” von Matthias Kannengiesser entnommen, das im Franzis Verlag erschienen ist. Sie können es <a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung">hier </a>direkt bestellen.</p>
<p style="text-align: justify;">
<p style="text-align: justify;">
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/das-factory-entwurfsmuster-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Singleton-Muster in PHP</title>
		<link>http://webdeveloper.franzis.de/php-5x/singleton-muster-in-php/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/singleton-muster-in-php/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 16:00:05 +0000</pubDate>
		<dc:creator>Matthias Kannengiesser</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[Erzeugungsmuster]]></category>
		<category><![CDATA[Konstruktor]]></category>
		<category><![CDATA[private-Deklaration]]></category>
		<category><![CDATA[Protokollobjekt]]></category>
		<category><![CDATA[Singleton]]></category>
		<category><![CDATA[statische Klasseneigenschaft]]></category>
		<category><![CDATA[statische Methode]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=1457</guid>
		<description><![CDATA[Das Singleton-Entwurfsmuster ist ein in der Softwareentwicklung eingesetztes Muster und gehört zur Gruppe der Erzeugungsmuster. Es stellt sicher, dass zu einer Klasse lediglich genau ein Objekt erzeugt werden kann, und ermöglicht einen globalen Zugriff auf dieses Objekt. In der Praxis wird diese Technik verwendet, um ressourcenschonend zu programmie­ren. Verbindungen zu Ressourcen, die lediglich einmalig vorhanden [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Das <em>Singleton</em>-Entwurfsmuster ist ein in der Softwareentwicklung eingesetztes Muster und gehört zur Gruppe der Erzeugungsmuster. Es stellt sicher, dass zu einer Klasse lediglich genau ein Objekt erzeugt werden kann, und ermöglicht einen globalen Zugriff auf dieses Objekt. In der Praxis wird diese Technik verwendet, um ressourcenschonend zu programmie­ren. Verbindungen zu Ressourcen, die lediglich einmalig vorhanden sind, wie beispiels­weise Dateien, werden über Singleton-Klassen verwaltet.</p>
<p><a href="http://webdeveloper.franzis.de/wp-content/uploads/2010/01/phpoop05_04.jpg"><img src="http://webdeveloper.franzis.de/wp-content/uploads/2010/01/phpoop05_04.jpg" alt="" title="phpoop05_04" width="490" height="214" class="alignleft size-full wp-image-1465" /></a></p>
<p style="text-align: justify;"><span id="more-1457"></span></p>
<p>Weitere Anwendungsbeispiele für Singleton-Muster sind:</p>
<ul>
<li>Ein zentrales Protokollobjekt, das Ausgaben in eine Datei schreibt.</li>
<li style="text-align: justify;">Datenbankzugriffe, die mit einer Datenbank verbunden werden sollen und lediglich auf ein Verbindungsobjekt zurückgreifen.</li>
</ul>
<p>Die folgende Singleton-Klasse soll Ihnen den Einstieg in die Welt der Entwurfsmuster erleichtern.</p>
<p><strong>Beispiel für ein Singleton-Entwurfsmuster in PHP:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Singleton 
<span style="color: #009900;">&#123;</span>
&nbsp;
   static <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Kein Meldung im Objekt'</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __clone<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Singleton<span style="color: #339933;">::</span><span style="color: #000088;">$instance</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#123;</span>
           Singleton<span style="color: #339933;">::</span><span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #009900;">&#125;</span>
       <span style="color: #b1b100;">return</span> Singleton<span style="color: #339933;">::</span><span style="color: #000088;">$instance</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setText<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">text</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">text</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Beispiel für eine Singleton-Anwendung in PHP:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Einchecken
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$single</span> <span style="color: #339933;">=</span> Singleton<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$single</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Sie sind herzlich Willkommen!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Auschecken 
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
   <span style="color: #009900;">&#123;</span>
       <span style="color: #000088;">$single</span> <span style="color: #339933;">=</span> Singleton<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$single</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Auf wiedersehen!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$single</span> <span style="color: #339933;">=</span> Singleton<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$single</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$passagier</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Einchecken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$single</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$passagier</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Auschecken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$single</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Ausgabe:</strong></p>
<pre>
Keine Meldung im Objekt
Sie sind herzlich Willkommen!
Auf Wiedersehen!
</pre>
<p style="text-align: justify;">Das Beispiel nutzt in jeder Klasse, die ein Singleton verwendet, immer wieder dasselbe Objekt, da eine erneute Erzeugung im Rahmen eines einfachen Methodenaufrufs völlig sinnlos wäre – man hätte am Ende lediglich eine Anzahl verwaister Objekte im Speicher.</p>
<p style="text-align: justify;">Darüber hinaus wird durch die <em>private</em>-Deklaration für den Konstruktor (<em>__construct</em>) und die <em>__clone()</em>-Methode verhindert, dass doch noch mehr Objekte erzeugt werden.</p>
<p style="text-align: justify;"><strong>Singleton-Konventionen:<br />
</strong></p>
<p>Um ein Singleton in PHP zu implementieren, sind folgende Schritte durchzuführen:</p>
<p>1.  Deklarieren Sie eine statische Klasseneigenschaft, die das Exemplar der Klasse spei­chert.</p>
<p>2.  Implementieren Sie eine statische Methode, die das gespeicherte Exemplar zurück­gibt und gegebenenfalls dieses Exemplar erstellt, falls es noch nicht vorhanden ist.</p>
<p>3.  Stellen Sie sicher, dass keine weiteren Instanzen durch Verwendung des <em>new</em>-Operators möglich sind, indem Sie den Konstruktor als private oder protected deklarieren.</p>
<p>4.  Stellen Sie sicher, dass das Exemplar der Klasse nicht geklont werden kann, indem Sie die <em>__clone()</em>-Methode als private deklarieren.</p>
<p>Wenn Sie diese Schritte befolgen, steht dem Einsatz von Singleton-Entwurfsmustern nichts im Wege.</p>
<p><a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung"><img class="alignleft size-full wp-image-1268" title="6299" src="http://webdeveloper.franzis.de/wp-content/uploads/2009/12/6299.jpg" alt="" width="213" height="304" /></a></p>
<p>Dieser Abschnitt wurde dem Buch “PHP 5 – Objektorientierte Programmierung” von Matthias Kannengiesser entnommen, das im Franzis Verlag erschienen ist. Sie können es <a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung">hier </a>direkt bestellen.</p>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/singleton-muster-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entwurfsmuster in PHP &#8211; Teil V</title>
		<link>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-v/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-v/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 16:25:52 +0000</pubDate>
		<dc:creator>Matthias Kannengiesser</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[Befehl]]></category>
		<category><![CDATA[Beobachter]]></category>
		<category><![CDATA[Besucher]]></category>
		<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Dolmetscher]]></category>
		<category><![CDATA[Interpreter]]></category>
		<category><![CDATA[Iterator]]></category>
		<category><![CDATA[Mediator]]></category>
		<category><![CDATA[Memento]]></category>
		<category><![CDATA[Schablonenmethode]]></category>
		<category><![CDATA[State]]></category>
		<category><![CDATA[Strategie]]></category>
		<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Subject/Observer]]></category>
		<category><![CDATA[Template-Methode]]></category>
		<category><![CDATA[Verhaltensmuster]]></category>
		<category><![CDATA[Vermittler]]></category>
		<category><![CDATA[Visitor]]></category>
		<category><![CDATA[Zustand]]></category>
		<category><![CDATA[Zuständigkeitskette]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=1451</guid>
		<description><![CDATA[Mithilfe der in Teil III und Teil IV dieser Reihe vorgestellten Erzeugungs- und Strukturmuster sind Sie in der Lage, effizient Objekte zu erzeugen und diese zu komplexen Strukturen zusammenzusetzen. Verhaltensmuster stel­len die letzte Gruppe dar und sorgen für die Musterimplementierung. Diese Muster befassen sich mit dem Verhalten und der Interaktion der verschiedenen Objekte. Sie beschreiben [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Mithilfe der in <a href="http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-iii/">Teil III</a> und <a href="http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-iv/">Teil IV</a> dieser Reihe vorgestellten Erzeugungs- und Strukturmuster sind Sie in der Lage, effizient Objekte zu erzeugen und diese zu komplexen Strukturen zusammenzusetzen. Verhaltensmuster stel­len die letzte Gruppe dar und sorgen für die Musterimplementierung. Diese Muster befassen sich mit dem Verhalten und der Interaktion der verschiedenen Objekte. Sie beschreiben dabei nicht nur die beteiligten Klassen und Objekte, sondern darüber hin­aus auch die Art und Weise, wie diese zur Laufzeit miteinander interagieren.</p>
<p style="text-align: justify;"><span id="more-1451"></span></p>
<p style="text-align: justify;"><strong>Pattern: Chain of Responsibility (Zuständigkeitskette)</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Vermeidet die Koppelung des Auslösers einer Anfrage an seinen Empfän­ger, indem mehr als ein Ob­jekt die Möglichkeit erhält, die Anfrage zu erledigen.</p>
<p style="text-align: justify;"><strong>Auswirkungen:</strong> Das Pattern verkettet die empfangenen Objekte und leitet die Anfrage von Objekt zu Objekt, bis ein Objekt sie erledigt. Jeder Teilnehmer der Kette kennt lediglich seinen Nachfolger. Die Kettenstruktur ist dynamisch aufbau­bar und änderbar. Es gibt keine Empfangsgarantie, Anfragen daher auch können unbehandelt bleiben.</p>
<p style="text-align: justify;"><strong>Pattern: Command (Befehl)</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Kapselt einen Auftrag als Objekt.</p>
<p><strong>Auswirkungen:</strong> Das Objekt, das eine Anfrage schickt, muss nicht wissen, wie diese abgear­beitet wird. Anfragen können in eine Warteschlange ge­stellt oder rückgängig gemacht wer­den. Anfragen können parametrisiert oder erweitert werden.</p>
<p><strong>Pattern: Interpreter (Dolmetscher)</strong></p>
<p><strong>Ziel:</strong> Erstellt für eine gegebene Sprache eine Grammatik und interpretiert die Sätze in der Sprache.</p>
<p style="text-align: justify;"><strong>Auswirkungen:</strong> Bestehende Grammatiken sind ein­fach zu ändern und zu erweitern. Die Regeln der Grammatik werden durch Klassen repräsentiert. Durch Anpassen der zugehörigen Klasse kann eine Regeländerung über­nommen werden. Zusätzliche Operationen sind leicht hinzuzufügen.</p>
<p style="text-align: justify;"><strong>Pattern: Iterator</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Ermöglicht sequenziellen Zugriff auf die Elemente eines Objekts, ohne dessen Struktur zu offenbaren.</p>
<p style="text-align: justify;"><strong>Auswirkungen:</strong> Das Pattern vereinfacht die Schnittstelle des Aggregats. Ermöglicht die parallele Iteration des Aggregats. Ermöglicht unterschiedliche Arten der Iteration über dasselbe Aggregat.</p>
<p style="text-align: justify;"><strong>Pattern: Mediator (Vermittler)</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Definiert ein Objekt, welches das Zusammenspiel einer Menge von Objekten in sich kapselt.</p>
<p style="text-align: justify;"><strong>Auswirkungen:</strong> Vermittler fördern lose Koppelung, indem sie verhindern, dass Objekte aufeinander explizit Bezug neh­men. Sie ermöglichen ihnen, das Zusam­menspiel der Objekte voneinander unabhängig zu variieren.</p>
<p style="text-align: justify;"><strong>Pattern: Memento</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Erfasst und externalisiert den internen Zustand eines Objekts, ohne seine Kapse­lung zu verletzen, sodass das Objekt später in diesen Zu­stand zurückversetzt werden kann.</p>
<p><strong>Auswirkungen:</strong> Externe Speicherung von Zuständen, ohne interne Details zu verraten. Performance-intensive Zustands­wechsel.</p>
<p><strong>Pattern: Subject/Observer (Beobachter)</strong></p>
<p><strong>Ziel:</strong> Definiert 1:n-Abhängigkeit zwischen Subjekt und Beobachterobjekten</p>
<p><strong>Auswirkungen:</strong> Das Subjekt kann ohne Beobachter ein­gesetzt werden. Lose Koppelung zwischen Subjekt und Beobachtern. Einfache Operationen können kaskadierende Aktionen in den Beobachtern auslösen.</p>
<p><strong>Pattern: State (Zustand)</strong></p>
<p><strong>Ziel:</strong> Ermöglicht es einem Objekt, sein Verhalten zu ändern, wenn sein interner Zustand sich ändert.</p>
<p><strong>Auswirkungen:</strong> Zustandsabhängiges wird Verhalten in eine eigene Klassenhierarchie ausgelagert. Zustandsänderungen werden expli­zit durchgeführt. Relativ leichte Erweiterbarkeit.</p>
<p><strong>Pattern: Strategy (Strategie)</strong></p>
<p><strong>Ziel:</strong> Definiert eine Familie von Algorithmen, kapselt jeden einzelnen und macht sie austauschbar.</p>
<p><strong>Auswirkungen:</strong> Polymorphismus statt Fallunter­scheidung (<em>if-</em>, <em>elseif </em>etc.). Auswahl verschiedener Implemen­tationen desselben Verhaltens. Relativ leichte Erweiterbarkeit.</p>
<p><strong>Pattern: Template-Methode (Schablonenmethode)</strong></p>
<p><strong>Ziel:</strong> Definiert die Schritte eines Algorithmus und überlässt die Implementierung der Schritte den Unterklassen.</p>
<p><strong>Auswirkungen:</strong> Erhöht die Wiederverwendbarkeit von Code. Herausfaktorieren gemeinsamen Verhaltens.</p>
<p><strong>Pattern: Visitor (Besucher)</strong></p>
<p><strong>Ziel: </strong>Fügt neue Operationen einer Objektstruktur hinzu und kapselt diese in einer Klasse.</p>
<p><strong>Auswirkungen:</strong> Verhindert, dass zusammengehörige Operationen über mehrere Klassen verteilt sind. Ermöglicht Zugriff auf Daten, die ansonsten verborgen bleiben. Erschwert das Hinzufügen neuer Elemente zur Datenstruktur.</p>
<p><strong>Was Entwurfsmuster nicht sind:</strong></p>
<p style="text-align: justify;">Für eine Vielzahl von Entwicklern sind Entwurfsmuster aus der Anwendungsentwicklung nicht mehr wegzudenken. Sollten auch Sie sich näher mit Entwurfsmustern auseinan­dersetzen wollen, dann gilt es Folgendes zu beachten, um Missverständnisse von vorn­herein zu vermeiden:</p>
<ul>
<li style="text-align: justify;"><strong><em>Entwurfsmuster sind keine Algorithmen</em></strong> – Algorithmen lösen Probleme (Suchen, Sortie­ren, etc.) und bieten weniger Flexibilität in der Implementierung.</li>
<li style="text-align: justify;"><strong><em>Entwurfsmuster sind kein Allheilmittel</em> </strong>– Erfindungsreichtum ist bei der Anwendung von Entwurfsmustern immer noch gefragt.</li>
<li style="text-align: justify;"><em><strong>E</strong><strong>ntwurfsmuster sind keine Frameworks</strong></em> – Frameworks setzen sich als wiederver­wendba­rer Code zusammen, Entwurfsmuster enthalten lediglich Beispiele von Code. Frameworks werden für festgelegte Anwendungsbereiche eingesetzt, Ent­wurfsmuster hingegen können überall eingesetzt werden.</li>
</ul>
<p style="text-align: justify;"><a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung"><img class="alignleft size-full wp-image-1268" title="6299" src="http://webdeveloper.franzis.de/wp-content/uploads/2009/12/6299.jpg" alt="" width="213" height="304" /></a></p>
<p style="text-align: justify;">Dieser Abschnitt wurde dem Buch “PHP 5 – Objektorientierte Programmierung” von Matthias Kannengiesser entnommen, das im Franzis Verlag erschienen ist. Sie können es <a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung">hier </a>direkt bestellen.</p>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-v/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entwurfsmuster in PHP &#8211; Teil IV</title>
		<link>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-iv/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-iv/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 16:04:18 +0000</pubDate>
		<dc:creator>Matthias Kannengiesser</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[Adapter]]></category>
		<category><![CDATA[Bridge]]></category>
		<category><![CDATA[Brücke]]></category>
		<category><![CDATA[Composite]]></category>
		<category><![CDATA[Decorator]]></category>
		<category><![CDATA[Facade]]></category>
		<category><![CDATA[Flyweight]]></category>
		<category><![CDATA[Proxy]]></category>
		<category><![CDATA[Strukturmuster]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=1439</guid>
		<description><![CDATA[Strukturmuster befassen sich mit der Komposition von unterschiedlichen Objekten, um eine größere und flexiblere Struktur zu entwickeln, die leicht um neue Funktionen erweitert werden kann.

Pattern: Adapter
Ziel: Passt eine Schnittstelle an die vom Client erwartete Schnitt­stelle an. Objekte können an­dere Objekte nutzen, indem man ein Adapter-Objekt da­zwischenschaltet.
Auswirkungen: Ursprünglich nicht kompatible Klassen werden kompatibel. Die Zielklasse kann [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;"><em>Strukturmuster </em>befassen sich mit der Komposition von unterschiedlichen Objekten, um eine größere und flexiblere Struktur zu entwickeln, die leicht um neue Funktionen erweitert werden kann.</p>
<p style="text-align: justify;"><span id="more-1439"></span></p>
<p style="text-align: justify;"><strong>Pattern: Adapter</strong></p>
<p><strong>Ziel:</strong> Passt eine Schnittstelle an die vom Client erwartete Schnitt­stelle an. Objekte können an­dere Objekte nutzen, indem man ein Adapter-Objekt da­zwischenschaltet.</p>
<p style="text-align: justify;"><strong>Auswirkungen: </strong>Ursprünglich nicht kompatible Klassen werden kompatibel. Die Zielklasse kann trotz unterschiedli­cher Nutzung unverändert bleiben. Schnittstellen werden entkoppelt. Die Wiederverwendbarkeit ist erhöht. Die Spezialisierung der adaptierten Klassen ist schwerer. Die Verwendung des Musters kann zu recht unterschiedlichem Aufwand führen.</p>
<p style="text-align: justify;"><strong>Pattern: Bridge (Brücke)</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Entkoppelt eine Abstraktion von ihrer Implementierung, sodass beide unabhängig voneinander variiert werden können. Bildet eine Brücke zwischen zwei Klassenhierarchien.</p>
<p style="text-align: justify;"><strong>Auswirkungen:</strong> Die Implementierung kann dynamisch ausgetauscht werden. Trennung von Interface- und Implementierungshierarchien.</p>
<p style="text-align: justify;"><strong>Pattern: Composite (Kompositum)</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Fügt mehrere Objekte zu einer Baumstruktur zusammen, um Kompositionen von Objekten einheitlich zu behandeln.</p>
<p style="text-align: justify;"><strong>Auswirkungen:</strong> Neue Komponenten können leicht eingefügt werden. Der Entwurf wird sehr schnell allgemein.</p>
<p style="text-align: justify;"><strong>Pattern: Decorator (</strong><strong>Dekorateur)</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Erweitert Objekte zur Laufzeit um neue Funktionalitäten.</p>
<p style="text-align: justify;"><strong>Auswirkungen:</strong> Die Klassen werden schlanker. Das Muster schafft größere Flexibilität als der Einsatz von Vererbung. Die Anzahl der Klassen in einer Anwen­dung wird erhöht.</p>
<p style="text-align: justify;"><strong>Pattern: Facade (Fassade)</strong></p>
<p><strong>Ziel:</strong> Bietet eine abstrakte Schnitt­stelle, die die Verwendung eines Subsystems vereinfacht.</p>
<p><strong>Auswirkungen: </strong>Reduziert die Anzahl der Klassen, die vom Client verwendet werden müssen. Fördert lose Koppelung zwischen den Subsystemen und den Klassen, die diese nutzen.</p>
<p><strong>Pattern: Flyweight (Fliegengewicht)</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Nutzt Gemeinsamkeiten von Objekten und lagert diese in zusätzliche Objekte aus, um eine große Anzahl von Objekten effi­zient verwenden zu können.</p>
<p style="text-align: justify;"><strong>Auswirkungen:</strong> Führt zur Redundanzreduzierung in der Datenhaltung. Führt zu kompakten und speicher­sparenden Objekten.</p>
<p style="text-align: justify;"><strong>Pattern: Proxy (Stellvertreter)</strong></p>
<p style="text-align: justify;"><strong>Ziel:</strong> Kontrolliert den Zugriff auf ein Objekt mithilfe eines Stellver­treters. Zugriff auf ein Objekt auf einem anderen Server (Remote-Proxy). Erzeugen des Objekts beim ers­ten Zugriff (virtueller Proxy). Durchführen von Verwaltungs­aufgaben (Schutz-Proxy).</p>
<p><strong>Auswirkung</strong>: Führt Indirektion beim Zugriff auf Objekte ein.</p>
<p style="text-align: justify;"><strong>Tipp:</strong> Sobald Sie mit den Entwurfsmustern dieser Gruppe vertraut sind, stehen Ihnen bereits die wich­tigsten Werkzeuge zur Verfügung, um durch Komposition eine flexible Architektur von unterschiedlichen Objekten zu erschaffen.</p>
<p style="text-align: justify;"><a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung"><img class="alignleft size-full wp-image-1268" title="6299" src="http://webdeveloper.franzis.de/wp-content/uploads/2009/12/6299.jpg" alt="" width="213" height="304" /></a></p>
<p style="text-align: justify;">Dieser Abschnitt wurde dem Buch “PHP 5 – Objektorientierte Programmierung” von Matthias Kannengiesser entnommen, das im Franzis Verlag erschienen ist. Sie können es <a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung">hier </a>direkt bestellen.</p>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-iv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Entwurfsmuster in PHP &#8211; Teil III</title>
		<link>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-iii/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-iii/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 12:02:29 +0000</pubDate>
		<dc:creator>Matthias Kannengiesser</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[Abstract Factory]]></category>
		<category><![CDATA[abstrakte Fabrik]]></category>
		<category><![CDATA[Builder]]></category>
		<category><![CDATA[Builder-Muster]]></category>
		<category><![CDATA[Entwurfsmuster]]></category>
		<category><![CDATA[Erbauer]]></category>
		<category><![CDATA[Erzeugungsmuster]]></category>
		<category><![CDATA[Factory]]></category>
		<category><![CDATA[Factory-Methode]]></category>
		<category><![CDATA[Factory-Muster]]></category>
		<category><![CDATA[Gang of Four]]></category>
		<category><![CDATA[GoF]]></category>
		<category><![CDATA[Prototyp]]></category>
		<category><![CDATA[Singleton]]></category>
		<category><![CDATA[Strukturmuster]]></category>
		<category><![CDATA[Verhaltensmuster]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=1429</guid>
		<description><![CDATA[Der primäre Nutzen eines Entwurfsmusters liegt in der Beschreibung einer Lösung für eine bestimmte Klasse von Problemen. Darüber hinaus ergibt sich ein wesentlicher Vorteil aus der Tatsache, dass jedes Muster einen Namen besitzt. Dies vereinfacht die Diskussion unter Softwareentwicklern, da man abstrahiert über eine Softwarestruktur sprechen kann. So sind Entwurfsmuster zunächst einmal sprachunabhängig.

Wenn der Einsatz [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Der primäre Nutzen eines Entwurfsmusters liegt in der Beschreibung einer Lösung für eine bestimmte Klasse von Problemen. Darüber hinaus ergibt sich ein wesentlicher Vorteil aus der Tatsache, dass jedes Muster einen Namen besitzt. Dies vereinfacht die Diskussion unter Softwareentwicklern, da man abstrahiert über eine Softwarestruktur sprechen kann. So sind Entwurfsmuster zunächst einmal sprachunabhängig.</p>
<p style="text-align: justify;"><span id="more-1429"></span></p>
<p style="text-align: justify;">Wenn der Einsatz von Entwurfsmustern dokumentiert wird, ergibt sich ein weiterer Vorteil: Durch Dokumentation des Musters wird ein Bezug her­gestellt zur dort vor­han­denen Diskussion des Problemkontexts und der Vor- und Nachteile der Lösung.</p>
<p style="text-align: justify;">Moderne Hochsprachen wie Java, aber durchaus auch PHP, unterstützen einige der gängigen Entwurfsmuster bereits mit bestimmten Sprachmitteln, sodass man sich in der Praxis vor allem bei der Nutzung moderner Sprachen im Prozess der objektorientierten Analyse (OOA) und des objekt­orientierten Designs (OOD) der Entwurfsmuster bedient, die dort unter Umständen noch immer implementationsneutral in der <em>unified modeling language</em> (UML) ange­wendet werden.</p>
<p style="text-align: justify;">Im Lauf der Zeit wurden Lösungen für bestimmte Probleme gefunden, die erfolgreich eingesetzt wurden und somit einen Katalog mit insgesamt 23 Entwurfsmustern bildeten. Laut der Gang of Four (GoF) lassen sich sämtliche Entwurfsmuster in drei grundlegenden Muster­gruppen zusammenfassen:</p>
<ul>
<li style="text-align: justify;"><strong><em>Creational Patterns</em></strong> (Erzeugungsmuster) lösen die Probleme beim Erzeugen von Objekten.</li>
<li style="text-align: justify;"><strong><em>Structural Patterns</em></strong> (Strukturmuster) befassen sich mit der Komposition von ver­schiedenen Objekten, um eine größere Struktur zu entwickeln.</li>
<li><strong><em>Behavioral Patterns</em></strong> (Verhaltensmuster) regeln die Inter­aktion zwischen verschie­denen Objekten.</li>
</ul>
<p style="text-align: justify;">Zu den bekanntesten Entwurfsmustern gehören unter anderem der <em>Iterator</em><em>-Pattern</em> und der <em>Singleton</em><em>-Pattern</em>.</p>
<p style="text-align: justify;"><strong>Hinweis:</strong> Muster sollen den Entwickler bei seiner Arbeit unterstützen. Die Aufgabe des Ent­wicklers besteht somit nicht mehr darin, das Rad neu zu erfinden, sondern das passende Rad zu verwenden.</p>
<p style="text-align: justify;"><strong>Gültigkeitsbereich der Muster</strong></p>
<p style="text-align: justify;">Strukturen, denen ein klassenbasiertes Muster zu Grunde liegt, sind durch Klassenver­erbung entstanden und somit zur Übersetzungszeit festgelegt.</p>
<p style="text-align: justify;">Analog hierzu nutzen objektbasierte Muster die Objektkomposition. Die von ihnen ver­wendeten Strukturen entstehen zur Laufzeit und sind dynamisch. Dieser Unterschied ist stets zu beachten.</p>
<p style="text-align: justify;"><strong>Gruppe der Erzeugungsmuster</strong></p>
<p>Erzeugungsmuster werden verwendet, um Objekte zu konstruieren.</p>
<p><strong>Pattern: Singleton.</strong></p>
<p>Stellt sicher, dass von einer Klasse ausschließlich eine In­stanz existiert, und stellt einen globalen Zugriffspunkt für diese Instanz zur Verfügung.</p>
<p><strong>Auswirkungen:</strong> Verhindert, dass mehr als eine In­stanz erzeugt werden kann. Ermöglicht Zugriffskontrolle auf die Instanz. Reduziert die Verwendung globaler Variablen.</p>
<p><strong>Pattern: Prototyp.</strong></p>
<p>Bestimmt die Arten zu erzeu­gender Objekte durch die Ver­wendung eines prototypischen Exemplars und erzeugt neue Objekte durch Kopieren dieses Prototypen.</p>
<p><strong>Auswirkungen:</strong> Reduziert die Verwendung globaler Variablen. Dynamische veränderbare Objekter­zeugung. Aggregation statt Vererbung. Klonen statt Instanzierung.</p>
<p><strong>Pattern: Builder (Erbauer).</strong></p>
<p>Klassenhierarchien von Erbau­ern implementieren verschie­dene Bauvarianten.</p>
<p><strong>Auswirkungen:</strong> Fördert die Programmierung gegen Schnittstellen. Anzahl der Klassen in einer Anwen­dung wird erhöht.</p>
<p><strong>Pattern: Factory-Methode (Fabrikmethode)</strong></p>
<p style="text-align: justify;">Delegiert die Erzeugung von Objekten an Unterklassen. Defi­niert eine Schnittstelle zur Er­zeu­gung eines Objekts, legt aber erst in den Unterklassen fest, welche Klasse zu instanzieren ist.</p>
<p style="text-align: justify;"><strong>Auswirkungen:</strong> Fördert die Programmierung gegen Schnittstellen. Ermöglicht das Einfügen speziali­sierter Klassen. Anzahl der Klassen in einer Anwen­dung wird erhöht.</p>
<p style="text-align: justify;"><strong>Pattern: Abstract-Factory (abstrakte Fabrik)</strong></p>
<p>Definiert Schnittstellen für die Erzeugung von Familien verwandter Objekte, ohne sich auf konkrete Klassen festlegen zu müssen.</p>
<p><strong>Auswirkungen:</strong> Stellt sicher, dass Objekte lediglich zusammen mit den Objekten ver­wendet werden, mit denen sie kompatibel sind. Das Hinzufügen neuer Produkte ist auf­wendig.</p>
<p style="text-align: justify;">Im nächsten Teil dieser Reihe wird es um die Strukturmuster gehen. Ein weiterer Teil der Reihe wird sich mit den Verhaltensmustern beschäftigen.</p>
<p style="text-align: justify;"><a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung"><img class="alignleft size-full wp-image-1268" title="6299" src="http://webdeveloper.franzis.de/wp-content/uploads/2009/12/6299.jpg" alt="" width="213" height="304" /></a></p>
<p style="text-align: justify;">Dieser Abschnitt wurde dem Buch “PHP 5 – Objektorientierte Programmierung” von Matthias Kannengiesser entnommen, das im Franzis Verlag erschienen ist. Sie können es <a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung">hier </a>direkt bestellen.</p>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-iii/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Entwurfsmuster in PHP &#8211; Teil II</title>
		<link>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-ii/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-ii/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 16:13:26 +0000</pubDate>
		<dc:creator>Matthias Kannengiesser</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[Black-Box-Wiederverwendung]]></category>
		<category><![CDATA[Delegation]]></category>
		<category><![CDATA[Klassenvererbung]]></category>
		<category><![CDATA[Objektkomposition]]></category>
		<category><![CDATA[White-Box-Wiederverwendung]]></category>
		<category><![CDATA[Wiederverwendung]]></category>
		<category><![CDATA[Zustandsmuster]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=1418</guid>
		<description><![CDATA[In diesem Teil der Reihe geht es um Techniken wie die Klassenvererbung und Objektkomposition sowie die Delegation und deren Bedeutung für die objektorientierte Entwicklung. Die Klassenvererbung funktioniert vom Prinzip her sehr einfach: Hier erbt eine Unterklasse die Implementierung und den Typ ihrer Oberklasse.

Dem abgeleiteten Objekt können neue Variablen und Methoden hinzuge­fügt werden, somit ist eine [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">In diesem Teil der Reihe geht es um Techniken wie die Klassenvererbung und Objektkomposition sowie die Delegation und deren Bedeutung für die objektorientierte Entwicklung. Die <strong>Klassenvererbung </strong>funktioniert vom Prinzip her sehr einfach: Hier erbt eine Unterklasse die Implementierung und den Typ ihrer Oberklasse.</p>
<p><span id="more-1418"></span></p>
<p style="text-align: justify;">Dem abgeleiteten Objekt können neue Variablen und Methoden hinzuge­fügt werden, somit ist eine Wiederverwendung realisiert. Diese Technik der Wieder­ver­wen­dung ist auch als <em>White-Box-Wiederverwendung</em> bekannt, da die Struktur des Eltern­objekts eingebunden wird und somit bekannt ist.</p>
<p style="text-align: justify;">Die <strong>Objektkomposition </strong>hingegen basiert auf der Technik, Objekte bestehender Klassen in eine Struktur einzubeziehen, beispielsweise durch Aggregation. Da hier lediglich die Schnittstelle des Objekts bekannt ist, wird diese Art von Wiederverwendung auch als <em>Black-Box-Wiederverwendung</em> bezeichnet.</p>
<p style="text-align: justify;">Die Klassenvererbung ist Teil der Programmiersprache und einfach zu realisieren. Die geerbten Methoden können beibehalten oder überschrieben werden, die Variablen werden ergänzt, genutzt oder ignoriert. Hier lässt sich schon erahnen, dass die Klassen­vererbung zu unsauberem Programmierstil verleiten kann.</p>
<p style="text-align: justify;">Die Vorteile der Objektkomposition liegen ganz klar in der Flexibilität. Beziehungen zwischen Objekten lassen sich, im Gegensatz zu denen zwischen Klassen, zur Laufzeit dynamisch erzeugen und verwerfen. Darüber hinaus sind die beteiligten Objekte ledig­lich durch die Schnittstelle bekannt und können somit gegen eine ähnliche Schnittstelle ausgetauscht werden, ohne dass auf die unterschiedlichen Implementierungen Rücksicht genommen werden muss – sofern die Spezifikation erfüllt bleibt. Für die Nutzung der Wiederverwendung durch Objektkomposition spricht auch, dass die Klassenvererbung die Klassenhierarchie verkompliziert und es dabei zu weiteren Abhängigkeiten kommt: Jede Ver­änderung der Implementierung oder der Struktur einer Oberklasse wirkt sich auf die abgeleitete Unterklasse aus.</p>
<p style="text-align: justify;">Der Vergleich zwischen Klassenvererbung und Objektkomposition ist eigentlich nicht notwendig, da beide Techniken zur Anwendung kommen müssen. Schließlich muss eine Klassenhierarchie existieren, um überhaupt Objekte zur Objektkomposition ableiten zu können. Da jedoch die Erfahrung lehrt, dass selten der kompliziertere Weg der Objekt­kom­position gewählt wird, sollte jeder Entwickler die Möglichkeiten und Konsequenzen beider Techniken kennen. Das Wissen um den richtigen Einsatz beider Techniken bedarf einer gewissen Erfahrung, die letzten Endes den Experten ausmacht.</p>
<p style="text-align: justify;"><strong>Tipp: </strong>Das Durcharbeiten der Entwurfsmuster ist vor allem für unerfahrene Entwickler sinn­voll, um für die Feinheiten des objektorientierten Softwareentwurfs »sensibilisiert« zu werden.</p>
<p style="text-align: justify;"><strong>Delegation</strong></p>
<p style="text-align: justify;">Mithilfe der Delegation von Anfragen kann auch mittels Objektkomposition ein Ver­halten simuliert werden, welches andernfalls lediglich über Vererbung realisiert werden kann.</p>
<p style="text-align: justify;">Um dies zu erklären, verwende ich ein Beispiel, welches bereits auf ein Ent­wurfsmuster zurückgreift. Es handelt es sich dabei um das Zustandsmuster (<em>State-Pattern</em>).</p>
<p style="text-align: justify;">In einer Software wird eine Datei mittels eines Objekts repräsentiert. Da sich der Zustand einer Datei ändern kann (<em>offen/geschlossen</em>), muss sich auch das Verhalten des entsprechenden Objekts ändern. Eine Möglichkeit, dies in einem objektorientierten Entwurf zu implementieren, besteht darin, eine abstrakte Klasse namens <em>Datei </em>einzuführen, von der eine konkrete Klasse namens <em>OffeneDatei </em>und eine andere namens <em>GeschlosseneDatei </em>abgeleitet werden. So werden die abstrakten Metho­den wie beispielsweise <em>oeffnen()</em> oder <em>schliessen() </em>überschrieben und das unter­schied­liche Verhalten ist realisiert.</p>
<p style="text-align: justify;"><a href="http://webdeveloper.franzis.de/wp-content/uploads/2010/01/phpoop05_01.jpg"><img class="alignleft size-full wp-image-1419" title="phpoop05_01" src="http://webdeveloper.franzis.de/wp-content/uploads/2010/01/phpoop05_01.jpg" alt="" width="383" height="274" /></a></p>
<p style="text-align: justify;">In dieser starren Hierarchie stellt die Änderung des Dateizustandes eine Hürde dar. Ein Objekt müsste die Klasse wechseln, was nicht möglich ist. Man müsste ein Objekt der jeweils gewünschten Klasse erzeugen, die Variablen umkopieren etc. Und selbst dann bekommt man in einem komplexen Entwurf eventuell noch Probleme mit vorhandenen Referenzen und wird so zu weiteren Änderungen gezwungen.</p>
<p style="text-align: justify;">Der Lösungsansatz des Zustandsmusters besteht darin, ein Dateiobjekt einzuführen, welches diejenigen Daten bzw. Operationen enthält, die kontextunabhängig sind. Darüber hinaus erhält dieses Objekt eine Referenz auf ein Zustandsobjekt, in welchem die zustandsabhängigen Operationen implementiert sind.</p>
<p><strong>Hinweis: </strong>Die verschiedenen Zustandsklassen stammen dabei von einer abstrakten Klasse ab.</p>
<p><a href="http://webdeveloper.franzis.de/wp-content/uploads/2010/01/phpoop05_02.jpg"><img class="alignleft size-full wp-image-1422" title="phpoop05_02" src="http://webdeveloper.franzis.de/wp-content/uploads/2010/01/phpoop05_02.jpg" alt="" width="459" height="207" /></a></p>
<p style="text-align: justify;">Nun ist die Existenz eines Dateiobjekts unabhängig von seinem Zustand gesichert. Lediglich bei Veränderungen des Zustands sind Veränderungen in der Objektstruktur nötig. Diese Struktur ist zur Laufzeit dynamisch und hat somit das übergeordnete Ziel des objektorientierten Entwurfs erreicht.</p>
<p><a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung"><img class="alignleft size-full wp-image-1268" title="6299" src="http://webdeveloper.franzis.de/wp-content/uploads/2009/12/6299.jpg" alt="" width="213" height="304" /></a></p>
<p>Dieser Abschnitt wurde dem Buch “PHP 5 – Objektorientierte Programmierung” von Matthias Kannengiesser entnommen, das im Franzis Verlag erschienen ist. Sie können es <a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung">hier </a>direkt bestellen.</p>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entwurfsmuster in PHP &#8211; Teil I</title>
		<link>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-i/</link>
		<comments>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-i/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 17:00:19 +0000</pubDate>
		<dc:creator>Matthias Kannengiesser</dc:creator>
				<category><![CDATA[PHP 5.x]]></category>
		<category><![CDATA[Beziehung zwischen Objekten]]></category>
		<category><![CDATA[Entwurfsmuster]]></category>
		<category><![CDATA[Klasse]]></category>
		<category><![CDATA[Klassenbildung]]></category>
		<category><![CDATA[Klassenvererbung]]></category>
		<category><![CDATA[Objekt]]></category>
		<category><![CDATA[Schnittstelle]]></category>
		<category><![CDATA[Schnittstellengestaltung]]></category>
		<category><![CDATA[Schnittstellenvererbung]]></category>
		<category><![CDATA[Vererbung]]></category>
		<category><![CDATA[Vererbungshierarchie]]></category>

		<guid isPermaLink="false">http://webdeveloper.franzis.de/?p=1408</guid>
		<description><![CDATA[Die Idee der Entwurfsmuster in der Softwareentwicklung wurde aus der Architektur über­nommen. Der Architekt Christopher Alexander hatte in den 1970er Jahren eine Sammlung von Entwurfsmustern zusammengestellt. Die wesentliche Aufgabe dieser Muster war, die Bewohner der zu bauenden Strukturen in den Entwurfsprozess einzu­binden.

Grundlagen
Erich Gamma, Mitte der 1990er Jahre Wissenschaftler an der Hochschule in Zürich, promovierte über die [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Die Idee der Entwurfsmuster in der Softwareentwicklung wurde aus der Architektur über­nommen. Der Architekt Christopher Alexander hatte in den 1970er Jahren eine Sammlung von Entwurfsmustern zusammengestellt. Die wesentliche Aufgabe dieser Muster war, die Bewohner der zu bauenden Strukturen in den Entwurfsprozess einzu­binden.</p>
<p style="text-align: justify;"><span id="more-1408"></span></p>
<p style="text-align: justify;"><strong>Grundlagen</strong></p>
<p style="text-align: justify;"><em>Erich Gamma</em>, Mitte der 1990er Jahre Wissenschaftler an der Hochschule in Zürich, promovierte über die Übertragung dieser Methode auf die Softwareentwicklung. Im Anschluss ging er nach Amerika, wo er zusammen mit Richard Helm, Ralph Johnson und John Vlissides das Buch »Design Patterns – Elements of Reusable Design« heraus­brachte, in dem 23 Entwurfsmuster beschrieben sind. Dieses Autorenquartett ist auch unter dem Spitznamen »Gang of Four« (Viererbande) bekannt. <em>GoF</em> wird gelegentlich auch als Verweis für besagtes Buch verwendet. Anders als Christopher Alexander, der seine Muster vor allem für Laien geschrieben hat, richten sich die GoF-Muster an Entwickler und nicht an Benutzer.</p>
<p style="text-align: justify;">Grundsätzlich ist der objektorientierte Soft­wareentwurf wesentlich aufwendiger als der prozedurale Entwurf. Der Entwickler muss zahlreiche Festlegungen treffen, die sich nicht direkt aus der Problemstellung ableiten. Um Ihnen dies nochmals zu verdeutlichen, sollten Sie folgende Zusammenstellung betrach­ten:</p>
<ul>
<li style="text-align: justify;"><strong><em>Bildung von Klassen</em></strong> – Hier muss der Entwickler die Daten und die auf sie anzuwenden­den Operationen in Klassen abbilden. Im Rahmen der Fragestellung der Objektgranularität ist in erster Linie festzulegen, wie detailliert die Aufteilung der Daten und Methoden auf die verschiedenen Klassen erfolgen soll.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong><em>Vererbungshierarchie</em></strong> – Die gebildeten Klassen verbindet eine zu gestaltende Ver­erbungshierarchie. Der Entwickler hat zu entscheiden, welche Klassen sinnvoller­weise voneinander erben sollen. Darüber hinaus sind ggf. abstrakte Klassen zu bilden. Die Vererbungshierarchie wird beim Softwareentwurf oft auch als »Ver­wandschaftsbeziehung« bezeichnet.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong><em>Schnittstellengestaltung</em></strong> – Im Rahmen der Schnittstellengestaltung muss festgelegt wer­den, welche Daten ein Objekt einer Klasse mithilfe welcher Methoden welchen anderen Objekten anderer Klassen zur Verfügung stellen soll.</li>
</ul>
<ul>
<li style="text-align: justify;"><strong><em>Beziehungen zwischen Objekten</em></strong> – Neben Verwandschaftsbeziehungen der Klassen muss es selbstverständlich auch Beziehungen zwischen den Objekten der Klassen geben. Ein Objekt kann beispielsweise ein anderes Objekt erzeugen, besitzen oder eine Referenz auf dieses verwalten. Diese Beziehungen sind ebenso wichtig wie die Vererbungsbeziehungen, da durch sie erst ein guter, objektorientierter Softwareent­wurf mit flexibler Struktur entsteht.</li>
</ul>
<p style="text-align: justify;"><strong>Hinweis:</strong> Sie sollten darauf achten, die einzelnen Festlegungen nicht in der angegebe­nen Reihenfolge, sondern vielmehr parallel zueinander festzulegen.</p>
<p style="text-align: justify;">Diesem Mehraufwand, der sich während der Entwicklungsarbeit ergibt, steht ein enormer Nutzen gegenüber, der die Popularität objektorientierter Entwicklung be­gründet. Objektorientierte Entwürfe sind flexibler. Veränderungen in der Problemstel­lung sind leicht zu realisieren, das Gleiche gilt für Erweiterungen. Diese Flexibilität beruht hauptsächlich darauf, dass der Entwickler bei seiner Arbeit wiederverwendbare Entwürfe entwickelt hat, die durch diverse Techniken verändert werden können.</p>
<p style="text-align: justify;">Der gedankliche Brückenschlag zu den Entwurfsmustern gelingt, wenn man erkennt, dass verschiedene Softwarebestandteile ähnliche oder sogar gleiche Strukturen enthal­ten. Wenn man diese Strukturen hinreichend abstrahiert, kann man sie als Muster bezeichnen.</p>
<p style="text-align: justify;">Die Technik der Gang Of Four besteht daher nicht nur im Entwurf von Mustern, son­dern in der Art, sie zu beschreiben und somit anwendbar zu machen.</p>
<p style="text-align: justify;"><strong>Hinweis:</strong> Vollkommen analog hierzu wurden in der Vergangenheit auch bewährte Algorith­men katalogisiert, veröffentlicht und standardisiert. Sortieralgorithmen sind hierfür ein typi­sches Beispiel.</p>
<p style="text-align: justify;"><strong>Objektorientierung für Fortgeschrittene</strong></p>
<p style="text-align: justify;">Das Verständnis für die Grundsätze und Techniken der objektorientierten Programmie­rung, wie beispielsweise der Kapselung oder Vererbung, bereitet normalerweise keine großen Schwierigkeiten. Damit ist es jedoch noch nicht getan. Die eigentliche Leistung des Entwicklers besteht darin, die bereitgestellten Techniken sinnvoll einzusetzen. Im Folgenden werde ich wichtige Aspekte der objektorien­tierten Entwicklung aufgreifen, die für Verständnis und Gebrauch der Entwurfs­muster wichtig sind.</p>
<p style="text-align: justify;"><strong>Unterschiedung zwischen Klassenvererbung und Schnittstellenvererbung</strong></p>
<p>Diese Unterscheidung wird in verschiedenen Programmiersprachen unterschiedlich reali­siert bzw. nicht realisiert.</p>
<p style="text-align: justify;">Bei der Klassenvererbung erhält das abgeleitete Objekt sämtliche Variablen sowie die Methoden. Analog hierzu erhält das abgeleitete Objekt bei der Schnittstellenvererbung ausschließlich die Signaturen der Methoden, nicht aber deren Implementierung. Dies wird auch als <em>Subtyping</em> bezeichnet, da eine Klasse, die die abgeleitete Schnittstelle imple­mentiert, vom selben Typ wie die beerbte Schnittstelle ist und somit sämt­liche ihrer Anfragen erfüllt.</p>
<p style="text-align: justify;">In den Mustern und in objektorientierten Programmiersprachen, die keinen expliziten Unterschied bei der Vererbung machen, wird die Schnittstellenvererbung durch das Erben von abstrakten Klassen nachgebildet.</p>
<p style="text-align: justify;">Der nächste Teil dieser Reihe widmet sich Techniken wie der Klassenvererbung und Objektkomposition sowie der Delegation und deren Bedeutung für die objektorientierte Entwicklung.</p>
<p><a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung"><img class="alignleft size-full wp-image-1268" title="6299" src="http://webdeveloper.franzis.de/wp-content/uploads/2009/12/6299.jpg" alt="" width="213" height="304" /></a></p>
<p>Dieser Abschnitt wurde dem Buch “PHP 5 – Objektorientierte Programmierung” von Matthias Kannengiesser entnommen, das im Franzis Verlag erschienen ist. Sie können es <a href="http://www.franzis.de/programmieren/php-mysql/php5-objektorientierte-programmierung">hier </a>direkt bestellen.</p>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloper.franzis.de/php-5x/entwurfsmuster-in-php-teil-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
