<?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>build broken &#187; Plug-In</title>
	<atom:link href="http://blog.aztec-project.org/tag/plug-in/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.aztec-project.org</link>
	<description>but we can fix it! Sometimes :)</description>
	<lastBuildDate>Fri, 11 Jun 2010 12:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Umsetzung des Plug-In-Patterns</title>
		<link>http://blog.aztec-project.org/2009/06/05/umsetzung-des-plug-in-patterns/</link>
		<comments>http://blog.aztec-project.org/2009/06/05/umsetzung-des-plug-in-patterns/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 19:45:57 +0000</pubDate>
		<dc:creator>Thomas Christian</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[Architektur]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Plug-In]]></category>

		<guid isPermaLink="false">http://blog.aztec-project.org/?p=5</guid>
		<description><![CDATA[In meinem ersten Blog-Post möchte ich euch in vereinfachter Form meine Umsetzung des Plug-In-Patterns vorstellen. Voraussetzungen für dieses Pattern sind: eine Host-Applikation die das Plug-In laden möchte ein Plug-In welches es zu laden gilt und die Schnittstellen Plugin und IHost Zu allererst brauchen wir eine gemeinsame Schnittstelle, die sowohl vom Plug-In als auch vom Host [...]]]></description>
			<content:encoded><![CDATA[<p>In meinem ersten Blog-Post möchte ich euch in vereinfachter Form meine Umsetzung des Plug-In-Patterns vorstellen.<br />
Voraussetzungen für dieses Pattern sind:</p>
<ul>
<li> eine Host-Applikation die das Plug-In laden möchte</li>
<li> ein Plug-In welches es zu laden gilt</li>
<li> und die Schnittstellen Plugin und IHost</li>
</ul>
<p>Zu allererst brauchen wir eine gemeinsame Schnittstelle, die sowohl vom Plug-In als auch vom Host (die Applikation welche das Plug-In verwenden soll) benutzt werden soll. Über diese Schnittstelle kommuniziert der Host mit dem Plug-In.<br />
Es würde sich das Interface IPlugIn anbieten. In meinem Fall verwende ich allerdings kein Interface sondern eine abstrakte Klasse, da ich bereits Logik direkt in die Schnittstelle implementieren möchte. In meinem Fall heißt die abstrakte Klasse einfach nur Plugin. Im weiteren Verlauf werde ich zu meiner abstrakten Klasse Schnittstelle sagen.</p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: #2b91af;">    1</span> <span style="color: blue;">public</span> <span style="color: blue;">abstract</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Plugin</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    2</span>     <span style="color: blue;">private</span> IHost m_host;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    3</span>     <span style="color: blue;">public</span> Plugin(<span style="color: blue;">string</span> name) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    4</span>         Name = name;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    5</span>     }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    6</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    7</span>     <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    8</span>     <span style="color: blue;">public</span> <span style="color: blue;">string</span> Author { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    9</span>     <span style="color: blue;">public</span> <span style="color: blue;">string</span> Version { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   10</span>     <span style="color: blue;">public</span> <span style="color: blue;">bool</span> IsRegistered { <span style="color: blue;">get</span>; <span style="color: blue;">private</span> <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   11</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   12</span>     <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   13</span>     <span style="color: gray;">///</span><span style="color: green;"> Setzt oder gibt die Host-Application.</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   14</span>     <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   15</span>     <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;value&gt;</span><span style="color: green;">Host-Application.</span><span style="color: gray;">&lt;/value&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   16</span>     <span style="color: blue;">public</span> IHost Host {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   17</span>         <span style="color: blue;">get</span> { <span style="color: blue;">return</span> m_host; }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   18</span>         <span style="color: blue;">set</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   19</span>             <span style="color: blue;">if</span> (<span style="color: blue;">value</span> != <span style="color: blue;">null</span>) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   20</span>                 <span style="color: blue;">if</span> (m_host == <span style="color: blue;">null</span>) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   21</span>                     m_host = <span style="color: blue;">value</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   22</span>                     <span style="color: blue;">if</span> (m_host.Register(<span style="color: blue;">this</span>)) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   23</span>                         IsRegistered = <span style="color: blue;">true</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   24</span>                     }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   25</span>                 }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   26</span>             } <span style="color: blue;">else</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   27</span>                 <span style="color: blue;">if</span> (m_host.Unregister(<span style="color: blue;">this</span>)) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   28</span>                     m_host = <span style="color: blue;">value</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   29</span>                     IsRegistered = <span style="color: blue;">false</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   30</span>                 }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   31</span>             }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   32</span>         }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   33</span>     }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   34</span> }</pre>
</div>
<p>Ich glaube, der Aufbau der Schnittstelle Plugin sollte bis auf die Property “Host” klar sein. Wie man sieht, benötigt man bei diesem Pattern zusätzlich zur Schnittstelle Plugin noch das IHost-Interface.<br />
Dieses Interface ist direkter Bestandteil der Schnittstelle Plugin. Nun kann man sich natürlich die Frage stellen, warum das Plug-In den Host kennen muss und somit von diesem abhängig ist.</p>
<p>Zum einen ist es in der Regel so, dass ein Plug-In für nur einen Host entwickelt wird und zum anderen wollte ich dem Host gewisse Richtlinien zum Registrieren und Lösen des Plug-Ins vorgeben.</p>
<p>Das Interface IHost sieht folgendermaßen aus:</p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: #2b91af;">    1</span> <span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: #2b91af;">IHost</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    2</span>     <span style="color: blue;">bool</span> Register(Plugin plugin);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    3</span>     <span style="color: blue;">bool</span> Unregister(Plugin plugin);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    4</span> }</pre>
</div>
<p>Dieses Interface muss von der Host-Applikation implementiert werden, damit sich das Plug-In am Host registrieren kann. Im letzten Satz habe ich es schon angedeutet. Nicht der Host registriert das Plug-In bei sich, sondern das Plug-In registriert sich am Host. Durch das setzen der Property Plugin.Host wird die Methode Register oder Unregister vom Plug-In aufgerufen, welche der Host implementiert.</p>
<p>Eine vereinfachte Darstellung der Implementierung des IHost -Interfaces sieht folgendermaßen aus:</p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: #2b91af;">    1</span> <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">HostApplication</span> : IHost {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    2</span>     <span style="color: blue;">private</span> List&lt;Plugin&gt; m_pluginList;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    3</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    4</span>     <span style="color: green;">//Konstruktor</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    5</span>     <span style="color: blue;">public</span> HostApplication() {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    6</span>         m_pluginList = <span style="color: blue;">new</span> List&lt;Plugin&gt;();</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    7</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    8</span>         <span style="color: green;">//Lädt alle verfügbaren Plug-Ins zb. aus einem Verzeichnis</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">    9</span>         List&lt;Plugin&gt; plugins = GetPlugins();</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   10</span>         <span style="color: blue;">foreach</span> (Plugin plugin <span style="color: blue;">in</span> plugins) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   11</span>             <span style="color: green;">//ruft implizit die IHost.Register-Methode auf</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   12</span>             plugin.Host = <span style="color: blue;">this</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   13</span>         }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   14</span>     }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   15</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   16</span>     <span style="color: blue;">public</span> <span style="color: blue;">void</span> ShowPlugins() {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   17</span>         <span style="color: blue;">foreach</span> (Plugin plugin <span style="color: blue;">in</span> m_pluginList) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   18</span>             <span style="color: green;">//Ausgabe der Namen aller am Host registrierten Plug-Ins</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   19</span>             Console.WriteLine(plugin.Name);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   20</span>         }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   21</span>     }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   22</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   23</span>     <span style="color: blue;">public</span> <span style="color: blue;">void</span> UnloadPlugins() {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   24</span>         <span style="color: blue;">foreach</span> (Plugin plugin <span style="color: blue;">in</span> m_pluginList) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   25</span>             <span style="color: green;">//ruft implizit die IHost.Unregister-Methode auf</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   26</span>             plugin.Host = <span style="color: blue;">null</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   27</span>         }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   28</span>     }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   29</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   30</span> <span style="color: blue;">    #region</span> IHost-Implementierung</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   31</span>     <span style="color: blue;">public</span> <span style="color: blue;">bool</span> Register(Plugin plugin) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   32</span>         <span style="color: blue;">if</span> (!m_pluginList.Contains(plugin)) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   33</span>             m_pluginList.Add(plugin);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   34</span>             <span style="color: blue;">return</span> <span style="color: blue;">true</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   35</span>         }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   36</span>         <span style="color: blue;">return</span> <span style="color: blue;">false</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   37</span>     }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   38</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   39</span>     <span style="color: blue;">public</span> <span style="color: blue;">bool</span> Unregister(Plugin plugin) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   40</span>         <span style="color: blue;">if</span> (m_pluginList.Contains(plugin)) {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   41</span>             m_pluginList.Remove(plugin);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   42</span>             <span style="color: blue;">return</span> <span style="color: blue;">true</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   43</span>         }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   44</span>         <span style="color: blue;">return</span> <span style="color: blue;">false</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   45</span>     }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   46</span> <span style="color: blue;">    #endregion</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;">   47</span> }</pre>
</div>
<p>Das Registrieren und Lösen der Plug-Ins könnte man nun noch in einen Plug-In-Manager auslagern, worauf ich in diesem Blog allerdings verzichten möchte.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.aztec-project.org%2F2009%2F06%2F05%2Fumsetzung-des-plug-in-patterns%2F&amp;title=Umsetzung%20des%20Plug-In-Patterns"><img src="http://blog.aztec-project.org/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.aztec-project.org/2009/06/05/umsetzung-des-plug-in-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

