<?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>Alexander Stelter - foo, bar, baz &#187; Pattern</title>
	<atom:link href="http://www.alexander-stelter.de/blog/category/programmierung/php/pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alexander-stelter.de/blog</link>
	<description>PHP, Javascript, Doctrine, Internet, Bücher, Schriesheim, Real-Life</description>
	<lastBuildDate>Fri, 20 Jan 2012 21:01:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>GLOBAL ohne $GLOBALS: Registry Pattern</title>
		<link>http://www.alexander-stelter.de/blog/32-global-ohne-globals-registry-pattern/</link>
		<comments>http://www.alexander-stelter.de/blog/32-global-ohne-globals-registry-pattern/#comments</comments>
		<pubDate>Thu, 07 Jun 2007 14:10:00 +0000</pubDate>
		<dc:creator>Alexander</dc:creator>
				<category><![CDATA[Pattern]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[globals]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[registry pattern]]></category>

		<guid isPermaLink="false">http://www.alexander-stelter.de/blog/?p=32</guid>
		<description><![CDATA[Wenn man sich eingehender mit MVC beschäftigt stellt man fest, dass es ziemlich schwierig ist die Schichten so anzulegen, dass sie vollständig von einander entkoppelt sind. Ein Knackpunkt dabei ist die Datenübertragung. Wie teilt die Business Logic der Präsentationsschicht Daten mit? Hierbei könnte man natürlich auf das gute alte $GLOBALS-Array zurückgreifen. Dies ist zwar eine [...]]]></description>
			<content:encoded><![CDATA[<p>Wenn man sich eingehender mit <a href="http://de.wikipedia.org/wiki/MVC"  title="MVC">MVC</a> beschäftigt stellt man fest, dass es ziemlich schwierig ist die Schichten so anzulegen, dass sie vollständig von einander entkoppelt sind. Ein Knackpunkt dabei ist die Datenübertragung. Wie teilt die Business Logic der Präsentationsschicht Daten mit? Hierbei könnte man natürlich auf das gute alte $GLOBALS-Array zurückgreifen. Dies ist zwar eine Lösung aber keine gute. Wir sollten vielmehr die Daten aus einer Schnittstelle beziehen und sie auch dorthin speichern, damit andere Klassen über die fixen Schnittstellen direkt wieder darauf zugreifen können.<br />
Dabei hilft uns das Registry Pattern. Es stellt eine Klasse dar, in der Daten abgelegt und wieder ausgelesen werden können. Hierzu wird für den globalen Zugriff das <a href="http://www.alexander-stelter.de/blog/archives/31-Alleinstehend-Das-Singleton-Pattern.html"  title="Singleton Pattern">Singleton Pattern</a> verwendet.<br />
Schauen wir uns nun zunächst das UML-Diagramm an:<span id="more-32"></span><!-- s9ymdb:50 --><img width='450' height='531' style="border: 0px; padding-left: 5px; padding-right: 5px;" src="/blog/uploads/programmierung/registry.jpg" alt="" /></p>
<p>Zur Verdeutlichung sind jetzt auch zwei andere Klassen ins Spiel gekommen Client A und B. Diese verwenden die Registry schreibend und lesend, sodass wir die Verwendung der Registry besser nachvollziehen können. Die Registry besteht neben dem Singleton im Prinzip nur aus Verwaltungsmethoden für das Speichern, Löschen, und Zurückgeben von Daten.</p>
<p>Begeben wir uns nun zunächst an die Definition des Interfaces und die Implementation der Registry, bevor wir die Clients auf unsere Daten los lassen:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Registry
<span style="color: #009900;">&#123;</span>
	<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: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> register <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$value</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> unregister <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$key</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> get <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$key</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;">class</span> DataLayer implements Registry
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> static <span style="color: #000088;">$Instance</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$Registry</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;">protected</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>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</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>
&nbsp;
	<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: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$Instance</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">NULL</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">self</span><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> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><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> register <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$value</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;">Registry</span> <span style="color: #009900;">&#91;</span> <span style="color: #000088;">$key</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</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> unregister <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$key</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;">Registry</span> <span style="color: #009900;">&#91;</span> <span style="color: #000088;">$key</span> <span style="color: #009900;">&#93;</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> get <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$key</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;">Registry</span> <span style="color: #009900;">&#91;</span> <span style="color: #000088;">$key</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Konstruktor und clone sind wieder zu Gunsten von Singleton deaktiviert bzw. protected deklariert. Die Methoden sollten selbsterklärend sein (wie schon so oft vorher <img src='http://www.alexander-stelter.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> ). Jetzt müssen wir noch die Clients implementieren. Diese sind ganz rudimentär, aber erfüllen in diesem Fall den Demozweck:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Client_A
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setData <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$Data</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		DataLayer<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;">-&gt;</span><span style="color: #004000;">register</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'testdata'</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$Data</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> Client_B
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> getData <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> DataLayer<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;">-&gt;</span><span style="color: #004000;">get</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'testdata'</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> printData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getData</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Wie wir sehen wird Client_A Daten in die Registry speichern und Client_B diese wieder auslesen und direkt darstellen. Nun müssen wir nur noch ein Testscript schreiben. In diesem werden $Logic und $Display direkt hintereinander instanziiert, das ist in der Regel nicht so <img src='http://www.alexander-stelter.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Logic</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Client_A<span style="color: #339933;">;</span>
<span style="color: #000088;">$Logic</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setData</span>
<span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span>
	<span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'foo'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'bar'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span>
	<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Display</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Client_B<span style="color: #339933;">;</span>
<span style="color: #000088;">$Display</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Wenn wir dieses Script nun ausführen, sehen wir anhand des Outputs, dass alles korrekt funktioniert:</p>
<blockquote><p>Array<br />
(<br />
    [foo] => 1<br />
    [bar] => 2<br />
)</p></blockquote>
<p>Nun haben wir gesehen wie wir Klassen mit Informationen versorgen können, ohne dass sie einander kennen müssen. Die Datenschicht kann, aufgrund fix definierter Schnittstellen beliebig ausgetauscht werden, solange die neue Schicht die selben Schnittstellen implementiert.</p>
<p>Bei Fragen wie gehabt, einfach einen Kommentar hinterlassen (oder auch wenn&#8217;s gut war oder geholfen hat?) <img src='http://www.alexander-stelter.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" class="tt" href="http://twitter.com/intent/tweet?text=GLOBAL+ohne+%24GLOBALS%3A+Registry+Pattern+http%3A%2F%2Falexander-stelter.de%2Fblog%2F%3Fp%3D32" title="Post to Twitter"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://www.alexander-stelter.de/blog/32-global-ohne-globals-registry-pattern/&amp;title=GLOBAL+ohne+%24GLOBALS%3A+Registry+Pattern" title="Post to Delicious"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious-micro3.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://www.alexander-stelter.de/blog/32-global-ohne-globals-registry-pattern/&amp;title=GLOBAL+ohne+%24GLOBALS%3A+Registry+Pattern" title="Post to Digg"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/digg/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://www.alexander-stelter.de/blog/32-global-ohne-globals-registry-pattern/&amp;t=GLOBAL+ohne+%24GLOBALS%3A+Registry+Pattern" title="Post to Facebook"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook-micro3.png" alt="Post to Facebook" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.alexander-stelter.de/blog/32-global-ohne-globals-registry-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Batch Script mit Command Pattern</title>
		<link>http://www.alexander-stelter.de/blog/30-batch-script-mit-command-pattern/</link>
		<comments>http://www.alexander-stelter.de/blog/30-batch-script-mit-command-pattern/#comments</comments>
		<pubDate>Wed, 06 Jun 2007 19:25:00 +0000</pubDate>
		<dc:creator>Alexander</dc:creator>
				<category><![CDATA[Pattern]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[batch]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[command pattern]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://www.alexander-stelter.de/blog/?p=30</guid>
		<description><![CDATA[Heute widmen wir uns mal der Problemstellung, dass wir ein Script schreiben müssen, welches verschiedene Aktionen nacheinander ausführen muss, sozusagen ein Batch-Script in PHP. Wir nehmen an, dass wir eine Aktion haben, die etwas installiert, und eine, die etwas deinstalliert. Die Aktionen sollen beliebig kombinierbar sein und mit einem Befehl auszuführen sein. In diesem Fall [...]]]></description>
			<content:encoded><![CDATA[<p>Heute widmen wir uns mal der Problemstellung, dass wir ein Script schreiben müssen, welches verschiedene Aktionen nacheinander ausführen muss, sozusagen ein Batch-Script in PHP. Wir nehmen an, dass wir eine Aktion haben, die etwas installiert, und eine, die etwas deinstalliert. Die Aktionen sollen beliebig kombinierbar sein und mit einem Befehl auszuführen sein.<br />
In diesem Fall hilft uns das Command-Pattern. Hier registrieren wir eine bestimmte Menge von Kommandos auf einem Controller (hier Invoker), welcher diese dann ausführt. Bevor wir an die Implementation gehen, schauen wir uns kurz das <a href="http://de.wikipedia.org/wiki/UML"  title="UML">UML</a>-Diagramm an:<span id="more-30"></span><!-- s9ymdb:40 --><img width='450' height='261' style="border: 0px;padding-right: 5px;" src="/blog/uploads/programmierung/command.jpg.jpg" alt="" /></p>
<p>Um die Commands noch ein wenig an einander anzugleichen könnten wir auch zunächst eine Abstract definieren, anstatt eines Interfaces, und so direkt Methoden (Implementationen nicht Schnittstellen!!) weitervererben, die in allen Commands gleich sein soll. In unserem Fall begnügen wir uns mit den Interfaces.</p>
<p>Widmen wir uns zunächst der Implementation der beiden Interfaces für den Invoker und das Command:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Invoker
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> add <span style="color: #009900;">&#40;</span> Command <span style="color: #000088;">$Action</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> remove <span style="color: #009900;">&#40;</span> Command <span style="color: #000088;">$Action</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> run<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;">interface</span> Command
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Die Definition der Schnittstellen ist immer wichtig. Denn nur so können wir sicherstellen, dass Klassen, die in dieser Struktur laufen sollen, später auch wirklich hinein passen.<br />
Wie wir sehen bekommt der Invoker zwei Verwaltungsfunktionen (um Commands hinzuzufügen und zu entfernen) und zusätzlich noch eine Methode um alle Commands auszuführen. Das Command bekommt in unserem Fall nur eine execute-Methode. Intern kann natürlich ein Kommando viele Methoden haben, die für die Ausführung nötig sind, jedoch ist für die Struktur nur wichtig, dass die entsprechenden Command Methoden auf jeden Fall implementiert sind.</p>
<p>In unserem Fall müssen wir noch 3 Klassen implementieren:</p>
<p><strong>- den Invoker (Batch)</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Batch implements Invoker
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$Commands</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> add <span style="color: #009900;">&#40;</span> Command <span style="color: #000088;">$Command</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;">Commands</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$Command</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> remove <span style="color: #009900;">&#40;</span> Command <span style="color: #000088;">$Command</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;">Commands</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_diff</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Commands</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Command</span><span style="color: #009900;">&#41;</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> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Verarbeitung der Anweisungen gestartet ...&lt;br /&gt;&quot;</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>
		<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;">Commands</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$Command</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$Command</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;============================================&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Verarbeitung der Anweisungen beendet ...&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>- ein Command (Install)</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Install implements Command
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$name</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;">$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> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;+&quot;</span> <span style="color: #339933;">.</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: #0000ff;">' wurde installiert&lt;br /&gt;'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>- ein Command (Uninstall)</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> UnInstall implements Command
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$name</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;">$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> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;-&quot;</span> <span style="color: #339933;">.</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: #0000ff;">' wurde deinstalliert&lt;br /&gt;'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ich denke die Methoden sollten selbsterklärend sein. Der Einfachheit halber geben diese Kommandos nur aus was sie tun. Beim Instanziieren geben wir ihnen einen Namen mit wie z.B. Db-Treiber. Die wirkliche Funktionalität kann ja bei Bedarf hinzugefügt werden. Mir geht es hier nur um die Verdeutlichung des Musters.</p>
<p>Nun schreiben wir uns noch ein kleines Testscript:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Batch</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Batch<span style="color: #339933;">;</span>
<span style="color: #000088;">$Batch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span> <span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Uninstall <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Pear::DB'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Batch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span> <span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Install <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Pear::MDB2'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Batch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span> <span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Install <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Pear::XmlTree'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Batch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Hier instaniziieren wir unser Batch-Objekt und übergeben über die add-Methode drei Commands, die dann in die Queue eingereiht werden und dann in dieser Reihenfolge ausgeführt werden.<br />
Wenn wir dieses Script nun laufen lassen, können an der Ausgabe erkennen, dass unsere Batch-Routine erfolgreich durchgelaufen ist:</p>
<blockquote><p>Verarbeitung der Anweisungen gestartet &#8230;<br />
============================================<br />
-Pear::DB wurde deinstalliert<br />
+Pear::MDB2 wurde installiert<br />
+Pear::XmlTree wurde installiert<br />
============================================<br />
Verarbeitung der Anweisungen beendet &#8230;</p></blockquote>
<p>Wie wir nun gesehen haben kann man mit diesem Muster leicht beliebige Befehle kombinieren. Möchte man allerdings, dass die Befehle geschachtelt werden können, also einen Baum darstellen sollen, können wir dazu noch zusätzlich das Composite Pattern einsetzen. Dies wird evtl. nächste Woche in einem gesonderten Eintrag erläutert. Wie man die beiden dann kombiniert ist dann sicherlich aus beiden Einträgen herzuleiten <img src='http://www.alexander-stelter.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>Bei Fragen, Anregungen etc. einfach einen Kommentar hinterlassen <img src='http://www.alexander-stelter.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" class="tt" href="http://twitter.com/intent/tweet?text=Batch+Script+mit+Command+Pattern+http%3A%2F%2Falexander-stelter.de%2Fblog%2F%3Fp%3D30" title="Post to Twitter"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://www.alexander-stelter.de/blog/30-batch-script-mit-command-pattern/&amp;title=Batch+Script+mit+Command+Pattern" title="Post to Delicious"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious-micro3.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://www.alexander-stelter.de/blog/30-batch-script-mit-command-pattern/&amp;title=Batch+Script+mit+Command+Pattern" title="Post to Digg"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/digg/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://www.alexander-stelter.de/blog/30-batch-script-mit-command-pattern/&amp;t=Batch+Script+mit+Command+Pattern" title="Post to Facebook"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook-micro3.png" alt="Post to Facebook" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.alexander-stelter.de/blog/30-batch-script-mit-command-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alleinstehend &#8211; Das Singleton Pattern</title>
		<link>http://www.alexander-stelter.de/blog/31-alleinstehend-das-singleton-pattern/</link>
		<comments>http://www.alexander-stelter.de/blog/31-alleinstehend-das-singleton-pattern/#comments</comments>
		<pubDate>Tue, 05 Jun 2007 13:27:00 +0000</pubDate>
		<dc:creator>Alexander</dc:creator>
				<category><![CDATA[Pattern]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[singleton pattern]]></category>

		<guid isPermaLink="false">http://www.alexander-stelter.de/blog/?p=31</guid>
		<description><![CDATA[Mit dem Singleton Pattern kann man bei der Implementierung von Anwendungen sicher stellen, dass von einer spezifischen Klasse immer nur genau eine Instanz vorhanden ist. Nehmen wir in unserem Beispiel einen Datenbankwrapper. In der Regel arbeiten wir mit _einer_ Datenbank und haben auch nur _eine_ Verbindung zu ihr. Dazu verwenden wir in unserem Beispiel einen [...]]]></description>
			<content:encoded><![CDATA[<p>Mit dem Singleton Pattern kann man bei der Implementierung von Anwendungen sicher stellen, dass von einer spezifischen Klasse immer nur genau eine Instanz vorhanden ist. Nehmen wir in unserem Beispiel einen Datenbankwrapper. In der Regel arbeiten wir mit _einer_ Datenbank und haben auch nur _eine_ Verbindung zu ihr. Dazu verwenden wir in unserem Beispiel einen Wrapper (natürlich ohne Funktionalität). Wir möchten also, dass von diesem Wrapper immer nur eine Instanz zur Verfügung steht, damit wir von jeder Stelle des Codes aus auf die selbe Instanz zugreifen können.<br />
Schauen wir uns dazu einmal das <a href="http://de.wikipedia.org/wiki/UML"  title="UML">UML</a>-Diagramm an:<span id="more-31"></span><!-- s9ymdb:44 --><img width='450' height='227' style="border: 0px; padding-left: 5px; padding-right: 5px;" src="/blog/uploads/programmierung/Singleton.jpg" alt="" /></p>
<p>Wir definieren zunächst ein Interface, welches die Schnittstelle getInstance definiert. Diese gibt dann die aktuelle Instanz zurück. Anders als bei der &#8220;normalen&#8221; Implementation von Klassen ist der Konstruktor protected, kann also nicht von außen aufgerufen werden, das heißt, dass die Klasse nicht mit dem new-Operator instanziiert werden kann. Zusätzlich muss die Methode __clone noch protected deklariert werden, damit man durch den Einsatz des clone-Operators keine neuen Instanzen erzeugen kann.</p>
<p>Die Implementation der Klassse sieht so aus:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> Singleton
<span style="color: #009900;">&#123;</span>
	<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: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DbWrapper implements Singleton
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> static <span style="color: #000088;">$Instance</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;">protected</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>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</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>
&nbsp;
	<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: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$Instance</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">NULL</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">self</span><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> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$Instance</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Wie wir sehen prüft die Methode getInstance beim Aufruf, ob bereits eine Instanz der Klasse in der statischen Klassenvariable $Instance vorhanden ist und legt sie bei Bedarf vor dem zurückgeben an.<br />
Nun wollen wir schauen, ob unser Vorhaben erfolgreich war und rufen folgendes Testscript auf:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$SingleWrapperA</span> <span style="color: #339933;">=</span> DbWrapper<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;">$SingleWrapperB</span> <span style="color: #339933;">=</span> DbWrapper<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>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$SingleWrapperA</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$SingleWrapperB</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Wrapper A und Wrapper B sind gleich&lt;br /&gt;'</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;">'Wrapper A und Wrapper B sind NICHT gleich&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Die Ausgabe im Browser zeigt uns, dass wir alles richtig gemacht haben und das Anlegen von mehreren Instanzen nicht mehr möglich ist:<br />
<blockquote>Wrapper A und Wrapper B sind gleich</p></blockquote>
<p>Jetzt könnte es aber nötig sein verschiedene Datenbanktypen zu verwenden und hier jedoch auch nur jeweils eine Instanz zu haben. Schauen wir uns die Veränderung im UML an:</p>
<p><!-- s9ymdb:45 --><img width='450' height='219' style="border: 0px; padding-left: 5px; padding-right: 5px;" src="/blog/uploads/programmierung/SingletonMulti.jpg" alt="" /></p>
<p>Wie wir sehen übernimmt getInstance nun einen Namen und speichert die Instanz nicht mehr in einer reinen Variable ab, sondern in einem Array, welches die jeweilige Instanz über den Namen zuordnet (alles sehr rudimentär <img src='http://www.alexander-stelter.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ).</p>
<p>Die Implementation dafür sähe folgendermaßen aus:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> SingletonMulti
<span style="color: #009900;">&#123;</span>
	<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: #000088;">$name</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;">class</span>
	DbWrapperMulti
implements
	SingletonMulti
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> static <span style="color: #000088;">$Instance</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;">protected</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</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>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</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>
&nbsp;
	<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: #000088;">$name</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: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$Instance</span> <span style="color: #009900;">&#91;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$Instance</span> <span style="color: #009900;">&#91;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$Instance</span> <span style="color: #009900;">&#91;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setName</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$Instance</span> <span style="color: #009900;">&#91;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#93;</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> setName <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>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Wir speichern also die entsprechenden Instanzen in einem Array und weisen ihnen darüber hinaus einen Namen zu (ohne diesen wären die Instanzen aller Typen gleich, da es sich um leere Instanzen handeln würde).<br />
Folgendes Script verwenden wir nun, um unsere Klasse zu testen:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$SingleWrapperMySql_A</span> <span style="color: #339933;">=</span> DbWrapperMulti<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'mysql'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$SingleWrapperMySql_B</span> <span style="color: #339933;">=</span> DbWrapperMulti<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'mysql'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$SingleWrapperPgSql_A</span> <span style="color: #339933;">=</span> DbWrapperMulti<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'pgsql'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$SingleWrapperPgSql_B</span> <span style="color: #339933;">=</span> DbWrapperMulti<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'pgsql'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$SingleWrapperMySql_A</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$SingleWrapperMySql_B</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'MySql A und MySql B sind gleich&lt;br /&gt;'</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;">'MySql A und MySql B sind NICHT gleich&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$SingleWrapperPgSql_A</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$SingleWrapperPgSql_A</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'PgSql A und PgSql B sind gleich&lt;br /&gt;'</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;">'PgSql A und PgSql B sind NICHT gleich&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$SingleWrapperPgSql_A</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$SingleWrapperMySql_A</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'PgSql und MySql sind gleich&lt;br /&gt;'</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;">'PgSql und MySql sind NICHT gleich&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Die Ausgabe im Browser zeigt uns, dass die beiden MySql und PgSql Wrapper jeweils gleich sind, jedoch der MySql und der PgSql Wrapper unterschiedlich sind. </p>
<blockquote><p>MySql A und MySql B sind gleich<br />
PgSql A und PgSql B sind gleich<br />
PgSql und MySql sind NICHT gleich</p></blockquote>
<p>Somit haben wir unser Ziel erreicht mehrere Typen des Wrappers jeweils einmal zu instanziieren.</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" class="tt" href="http://twitter.com/intent/tweet?text=Alleinstehend+%E2%80%93+Das+Singleton+Pattern+http%3A%2F%2Falexander-stelter.de%2Fblog%2F%3Fp%3D31" title="Post to Twitter"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://www.alexander-stelter.de/blog/31-alleinstehend-das-singleton-pattern/&amp;title=Alleinstehend+%E2%80%93+Das+Singleton+Pattern" title="Post to Delicious"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious-micro3.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://www.alexander-stelter.de/blog/31-alleinstehend-das-singleton-pattern/&amp;title=Alleinstehend+%E2%80%93+Das+Singleton+Pattern" title="Post to Digg"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/digg/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://www.alexander-stelter.de/blog/31-alleinstehend-das-singleton-pattern/&amp;t=Alleinstehend+%E2%80%93+Das+Singleton+Pattern" title="Post to Facebook"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook-micro3.png" alt="Post to Facebook" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.alexander-stelter.de/blog/31-alleinstehend-das-singleton-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Observer Pattern (Beobachter Muster)</title>
		<link>http://www.alexander-stelter.de/blog/29-observer-pattern-beobachter-muster/</link>
		<comments>http://www.alexander-stelter.de/blog/29-observer-pattern-beobachter-muster/#comments</comments>
		<pubDate>Mon, 04 Jun 2007 18:56:00 +0000</pubDate>
		<dc:creator>Alexander</dc:creator>
				<category><![CDATA[Pattern]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[beobachter]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[observer pattern]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://www.alexander-stelter.de/blog/?p=29</guid>
		<description><![CDATA[Da ich mich seit kurzem intensiv mit Entwurfsmustern auseinander setze, möchte ich jetzt eine Beispielimplementation für das Observer Pattern (Beobachter Muster) in PHP vorstellen. Hierfür nehmen wir an, wir haben eine Datenquelle, die ein Array mit Punkteständen aus einem beliebigen Spiel übergeben bekommt. Wir möchten diese nun unabhängig von einander darstellen: Durchschnittliche Punktzahl, Summe aller [...]]]></description>
			<content:encoded><![CDATA[<p>Da ich mich seit kurzem intensiv mit <a href="http://de.wikipedia.org/wiki/Entwurfsmuster"  title="Entwurfsmuster">Entwurfsmustern</a> auseinander setze, möchte ich jetzt eine Beispielimplementation für das Observer Pattern (Beobachter Muster) in <a href="http://de.php.net"  title="PHP">PHP</a> vorstellen.<br />
Hierfür nehmen wir an, wir haben eine Datenquelle, die ein Array mit Punkteständen aus einem beliebigen Spiel übergeben bekommt. Wir möchten diese nun unabhängig von einander darstellen: Durchschnittliche Punktzahl, Summe aller Punkte. Wenn sich die Daten im Objekt der Spielstände ändern, sollen die Darstellungsobjekte automatisch benachrichtigt werden und Daten abrufbereit zur Verfügung stehen.<br />
<span id="more-29"></span>Schauen wir uns zunächst das <a href="http://de.wikipedia.org/wiki/UML"  title="UML">UML</a>-<a href="http://de.wikipedia.org/wiki/UML#Spracheinheit_Klassen"  title="Klassendiagramm">Klassendiagramm</a> an, bevor wir uns an den Quellcode machen:</p>
<p><!-- s9ymdb:41 --><img width='450' height='240' style="border: 0px; padding-left: 5px; padding-right: 5px;" src="/blog/uploads/programmierung/Observer.jpg" alt="" /></p>
<p>Unser Object &#8220;Statistics&#8221; enthält als 1-n &#8220;Objekte&#8221; von Typ &#8220;Average&#8221; und &#8220;Sum&#8221;. Diese werden zu Beginn registriert und werden ab sofort bei jeder Änderung der Daten in &#8220;Statistics&#8221; benachrichtigt. Wir können noch beliebig viele andere Observer-Klassen implementieren, z.B. zum Ausgeben von Diagrammen etc. Dies erfordert nur eine zusätzliche Implementation des Observers.</p>
<p>Legen wir zunächst die Interfaces für unsere Klassen an. Damit stellen wir sicher, dass wir die benötigten Schnittstellen bereitstellen, damit das Muster korrekt arbeiten kann:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><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> detatch <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;">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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printResult <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Gehen wir kurz die Methoden einzeln durch:</p>
<p><strong>Subject::attach</strong><br />
&#8230; bietet die Möglichkeit Observer für die Datenquelle zu registrieren.</p>
<p><strong>Subject::detach</strong><br />
&#8230; bietet die Möglichkeit Observer von der Datenquelle zu entfernen.</p>
<p><strong>Subject::notify</strong><br />
&#8230; benachrichtigt die registrierten Observer über Änderungen auf der Datenquelle.</p>
<p><strong>Observer::update</strong><br />
&#8230; aktualisiert die Daten des Observers bei jedem Update über die Datenquelle</p>
<p><strong>Observer::printResult</strong><br />
&#8230; gehört nicht zur Originalimplementation des Observerpatterns, hier führen wir sie trotzdem auf, da wir von jedem Observer erwarten, dass er seine Ergebnisse ausgeben kann.</p>
<p>Schauen wir uns nun die Implementation des Subjects an. Die Methoden sollten eigentlich für sich sprechen. Die Methode setData() wird verwendet, um Daten des Subjects zu ändern. Im Konstruktor wird der initale Datenbestand übergeben. Dieser wird beim Registrieren eines jeden Observers auf dem Subject an den Observer übergeben:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Statistics implements Subject
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$Data</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;">protected</span> <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;">$Data</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;">setData</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$Data</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> 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;">$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: #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;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> detatch <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: #339933;">=</span> <span style="color: #990000;">array_diff</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: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Observer</span><span style="color: #009900;">&#41;</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> 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>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setData <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$Data</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;">Data</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span><span style="color: #000088;">$Data</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: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getData<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;">Data</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Nachdem wir nun das Subject implementiert haben, müssen wir die jeweiligen Darstellungsformen implementieren. Die update-Methode ist, wie gesagt, dazu bestimmt die Daten des Subjects aufzunehmen und aufzubereiten. Ich denke der Rest muss nicht weiter kommentiert werden</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Sum implements Observer
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$Data</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> update<span style="color: #009900;">&#40;</span>Subject <span style="color: #000088;">$Statistics</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;">Data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$Statistics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getData</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> printResult<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%01.2f</span>&quot;</span> <span style="color: #339933;">,</span> <span style="color: #990000;">array_sum</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Data</span><span style="color: #009900;">&#41;</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>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Average implements Observer
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$Data</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> update<span style="color: #009900;">&#40;</span>Subject <span style="color: #000088;">$Statistics</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;">Data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$Statistics</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getData</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> printResult<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%01.2f</span>&quot;</span> <span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_sum</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Data</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</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>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Nachdem wir auch dies erledigt haben, bauen wir ein kleines Testscript, welches die Funktionalitäten testet. Wir übergeben zunächst ein Array mit Daten an das Subject, registrieren zwei Observer und lassen die Daten ausgeben. Anschließend ändern wir die Daten auf dem Subject und lassen die Daten der Observer erneut ausgeben:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span>
<span style="color: #009900;">&#40;</span>
	<span style="color:#800080;">123.4</span><span style="color: #339933;">,</span>
	<span style="color:#800080;">982.2</span><span style="color: #339933;">,</span>
	<span style="color: #cc66cc;">329</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$Data2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span>
<span style="color: #009900;">&#40;</span>
	<span style="color: #000088;">$Data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
	<span style="color: #000088;">$Data</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$StatisticData</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Statistics <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$Data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$SumPager</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Sum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$AveragePager</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Average<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$StatisticData</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$SumPager</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$StatisticData</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$AveragePager</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$SumPager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$AveragePager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printResult</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;---------Nach Datenaenderung---------&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$StatisticData</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setData</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$Data2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$SumPager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$AveragePager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Folgende Ausgabe wird nun im Browser erzeugt, wie wir sehen, hat alles gut funktioniert und die Daten sind erfolgreich geändert worden:</p>
<blockquote><p>Sum: 1435.60<br />
Average: 358.90<br />
&#8212;&#8212;&#8212;Nach Datenaenderung&#8212;&#8212;&#8212;<br />
Sum: 1311.20<br />
Average: 655.60</p></blockquote>
<p>Dies war also ein kleiner Chrashkurs für das Observer-Pattern in <a href="http://de.php.net"  title="PHP">PHP</a> 5. Bei Fragen, Anregungen etc. einfach einen Kommentar hinterlassen <img src='http://www.alexander-stelter.de/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" class="tt" href="http://twitter.com/intent/tweet?text=Observer+Pattern+%28Beobachter+Muster%29+http%3A%2F%2Falexander-stelter.de%2Fblog%2F%3Fp%3D29" title="Post to Twitter"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter-micro3.png" alt="Post to Twitter" /></a> <a target="_blank" class="tt" href="http://delicious.com/post?url=http://www.alexander-stelter.de/blog/29-observer-pattern-beobachter-muster/&amp;title=Observer+Pattern+%28Beobachter+Muster%29" title="Post to Delicious"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/delicious/tt-delicious-micro3.png" alt="Post to Delicious" /></a> <a target="_blank" class="tt" href="http://digg.com/submit?url=http://www.alexander-stelter.de/blog/29-observer-pattern-beobachter-muster/&amp;title=Observer+Pattern+%28Beobachter+Muster%29" title="Post to Digg"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/digg/tt-digg-micro3.png" alt="Post to Digg" /></a> <a target="_blank" class="tt" href="http://www.facebook.com/share.php?u=http://www.alexander-stelter.de/blog/29-observer-pattern-beobachter-muster/&amp;t=Observer+Pattern+%28Beobachter+Muster%29" title="Post to Facebook"><img class="nothumb" src="http://www.alexander-stelter.de/blog/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook-micro3.png" alt="Post to Facebook" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.alexander-stelter.de/blog/29-observer-pattern-beobachter-muster/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

