<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Was is eine factory class?]]></title><description><![CDATA[<p>Ich belese mich grad etwas in c++, und stoße auf eine factory class!<br />
was genau ist das?</p>
<p>kann mir jemand mal ein kleines beispiel geben?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/137439/was-is-eine-factory-class</link><generator>RSS for Node</generator><lastBuildDate>Sat, 29 Aug 2026 19:42:37 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/137439.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 17 Feb 2006 13:06:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Was is eine factory class? on Fri, 17 Feb 2006 13:06:48 GMT]]></title><description><![CDATA[<p>Ich belese mich grad etwas in c++, und stoße auf eine factory class!<br />
was genau ist das?</p>
<p>kann mir jemand mal ein kleines beispiel geben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/996665</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/996665</guid><dc:creator><![CDATA[Assemblercode??]]></dc:creator><pubDate>Fri, 17 Feb 2006 13:06:48 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Fri, 17 Feb 2006 15:43:39 GMT]]></title><description><![CDATA[<p>Hallo,<br />
eine Factory-Klasse ist eine Klasse deren Hauptaufgabe die Erzeugung von Objekten ist, wobei eine solche Klasse normalerweise Objekte verschiedener Klassen erzeugen kann.</p>
<blockquote>
<p>kann mir jemand mal ein kleines beispiel geben?</p>
</blockquote>
<p>Es gibt unzählige Beispiele und unzählige verschiedene Variationen von Factory-Klassen. Ein typisches Beispiel wo einfache Factory-Klassen zum Einsatz kommen: Objekte sollen nur unter Angabe ihres Klassennames erzeugbar sein. Hier baut man dann eine Factory, die letztlich eine Abbildung zwischen Klassenname und Erzeugungsfunktion ist. Du gibst der Factory einen Klassenname, z.B. &quot;Foo&quot; und sie erzeugt dir eine Instanz der Klasse.</p>
<pre><code class="language-cpp">// Basisklasse aller Klassen, die durch die Factory erzeugt werden können.
class Base { 
    ...
};

class Foo : public Base {...};
class Bar : public Base {...};

class Factory {
public:
    Base* createObject(const char* className);
};

Base* Factory::createObject(const char* className) {
    if (strcmp(className, &quot;Foo&quot;) == 0) {
        return new Foo();
    }
    else if (strcmp(className, &quot;Bar&quot;) == 0) {
        return new Bar();
    }
    ...
    else {
        throw UnknownClassName(className);
    } 
}
</code></pre>
<p>Natürlich ist eine if-else-if-else-Kaskade nicht gerade die sinnvollste Art und Weise sowas zu implementieren, aber deshalb ist das ja auch nur ein Beispiel <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/996825</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/996825</guid><dc:creator><![CDATA[HumeSikkins]]></dc:creator><pubDate>Fri, 17 Feb 2006 15:43:39 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 07:56:30 GMT]]></title><description><![CDATA[<p>ok, hab mal was probiert, brauch aber noch bisschen erklärungen:</p>
<p>probleme stehen im code</p>
<pre><code class="language-cpp">class Base
{
 public:
  Base(int i=0){x=i;}
  void ShowItX(){cout&lt;&lt;&quot;Base x:&quot;&lt;&lt;x&lt;&lt;endl;}
 private:
  int x;
};

class FirstMember:public Base
{
 public:
  FirstMember(int j=0,int i=1):Base(j){y=i;}
  void ShowItY(){cout&lt;&lt;&quot;FirstMember y:&quot;&lt;&lt;y&lt;&lt;endl;}

 private:
  int y;
};

class SecondMember:public Base
{
 public:
  SecondMember(int j=0,int i=1):Base(j){z=i;}
  void ShowItZ(){cout&lt;&lt;&quot;SecondMember z:&quot;&lt;&lt;z&lt;&lt;endl;}
 private:
  int z;
};

class Factory
{
 public:
  Base* createIt(const char* classname)
  {
   if(strcmp(classname,&quot;First&quot;)==0)
    {
     b = new FirstMember();
     return b;                  //so in ordnung?
    }
   if(strcmp(classname,&quot;Second&quot;)==0)
   {
    b = new SecondMember();
    return b;
   }
   else
    throw UnknownClassName(classname);  //wie kann ich das verarbeiten??
  }
 private:
  Base* b;    //das hier richtig? oder ist zeiger falsch?
};

int main(int argc, char* argv[])
{
 Factory F;
 Base* b;            //da ja zeiger zurückgegeben wird
 try
 {
 b=F.createIt(&quot;First&quot;);     //aufruf so richtig?
 b-&gt;ShowItX();              //kann trotzdem nicht auch Methoden von First zugreifen, will ich ja aber,wie mach ich das richtig?
 }
 catch(...)            //wie kann ich die exc hier abfangen?
 {

 }
 getch();       return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/998258</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998258</guid><dc:creator><![CDATA[Assemblercode]]></dc:creator><pubDate>Mon, 20 Feb 2006 07:56:30 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:05:24 GMT]]></title><description><![CDATA[<p>Exceptionverarbeitung:<br />
- geworfen per throw<br />
- gefangen per try/catch<br />
so wie du es machst, fängst du unspezifiziert alle Exceptions, die ankommen. Wenn du das spezifischer machen willst, geht sowas:</p>
<pre><code class="language-cpp">try
{
  b=F.CreateIt(&quot;Third&quot;)
  ...
}
catch(UnknownClassName&amp; ex)
{
  //hier kannst du die Elemente von ex auswerten
}
</code></pre>
<p>Methoden der spezifischen Klassen anwenden: Am besten wäre es, du nutzt die Möglichkeiten von virtuellen Methoden (und überschreibst ShowItX() in den abgeleiteten Klassen). Zur Not könntest du auch einen dynamic_cast verwenden (und dich darauf vorbereiten, eine bad_cast-Exception aufzufangen).</p>
<p>(PS: Das Factory-Objekt braucht das erzeugte Base*-Objekt übrigens nicht unbedingt zwischenzuspeichern)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998262</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998262</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:05:24 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:17:39 GMT]]></title><description><![CDATA[<p>Hey CStoll,</p>
<p>du bist zum Moderator aufgestiegen?</p>
<p>Glückwunsch...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998269</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998269</guid><dc:creator><![CDATA[CStoll-Gratulant]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:17:39 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:30:51 GMT]]></title><description><![CDATA[<p>achso, dachte das UnknownClassName ist eine eigene Exception</p>
<p>also einfachso die funktionen dann aufrufen geht nicht?!</p>
<p>wie meinst das hier mit den virtuellen funktionen?Kannst mir da kurz ein beispiel geben?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998279</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998279</guid><dc:creator><![CDATA[Assemblercode]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:30:51 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:37:36 GMT]]></title><description><![CDATA[<p>wie heisst diese factory? objectfactory od. classfactory?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998283</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998283</guid><dc:creator><![CDATA[netrobot]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:37:36 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:38:50 GMT]]></title><description><![CDATA[<p>Was mir gerade auffällt...</p>
<p>Wirfts du immer Referenzen und keine Zeiger, CStoll?<br />
Hat das einen Vorteil?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998284</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998284</guid><dc:creator><![CDATA[CStoll-Gratulant]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:38:50 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:39:10 GMT]]></title><description><![CDATA[<p>netrobot schrieb:</p>
<blockquote>
<p>wie heisst diese factory? objectfactory od. classfactory?</p>
</blockquote>
<p>deine frage verwirrt mich etwas!<br />
was ist der unterschied?<br />
btw was is eine classfactory?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998285</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998285</guid><dc:creator><![CDATA[Assemblercode]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:39:10 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:41:11 GMT]]></title><description><![CDATA[<p>ist es nicht besser, dass man vor einer factoryClass noch ein Interface bauen?<br />
also die beiden interface kommunizieren miteinander.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998288</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998288</guid><dc:creator><![CDATA[netrobot]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:41:11 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:45:05 GMT]]></title><description><![CDATA[<blockquote>
<p>deine frage verwirrt mich etwas!<br />
was ist der unterschied?<br />
btw was is eine classfactory?</p>
</blockquote>
<p>ich habe irgendwo gelesen, es gibt 2 factories</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998292</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998292</guid><dc:creator><![CDATA[netrobot]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:45:05 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:45:49 GMT]]></title><description><![CDATA[<p>netrobot schrieb:</p>
<blockquote>
<p>ist es nicht besser, dass man vor einer factoryClass noch ein Interface bauen?<br />
also die beiden interface kommunizieren miteinander.</p>
</blockquote>
<p>sorry, aber ich weis nicht was du mir grad damit sagen willst!<br />
wäre um eine erklärung dankbar!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998296</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998296</guid><dc:creator><![CDATA[Assemblercode]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:45:49 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:50:09 GMT]]></title><description><![CDATA[<blockquote>
<p>class Factory<br />
{<br />
public:<br />
Base* createIt(const char* classname)<br />
{<br />
if(strcmp(classname,&quot;First&quot;)==0)<br />
{<br />
b = new FirstMember();<br />
return b; //so in ordnung?<br />
}<br />
if(strcmp(classname,&quot;Second&quot;)==0)<br />
{<br />
b = new SecondMember();<br />
return b;<br />
}<br />
else<br />
throw UnknownClassName(classname); //wie kann ich das verarbeiten??<br />
}<br />
private:<br />
Base* b; //das hier richtig? oder ist zeiger falsch?</p>
</blockquote>
<p>wozu braucht man hier Base* b?, muss man dieses Object intern verwalten?(zwischenspeichern)</p>
<pre><code class="language-cpp">if(strcmp(classname,&quot;Second&quot;)==0)
 {
    return new SecondMember();
 }
</code></pre>
<p>Compiler wird nacher dieses noname Variable automatisch löschen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998300</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998300</guid><dc:creator><![CDATA[netrobot]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:50:09 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 08:51:14 GMT]]></title><description><![CDATA[<p>netrobot schrieb:</p>
<blockquote>
<blockquote>
<p>deine frage verwirrt mich etwas!<br />
was ist der unterschied?<br />
btw was is eine classfactory?</p>
</blockquote>
<p>ich habe irgendwo gelesen, es gibt 2 factories</p>
</blockquote>
<p>Es gibt viel mehr als zwei Factory-Typen. Du meinst wahrscheinlich, neben der hier präsentierten Factory-Variante (die meistens als Object-Factory bezeichnet wird), eine Abstract Factory, so wie sie durch das GoF-Pattern Abstract Factory beschrieben wird. Abstract deshalb, weil du hier zwei Hierarchien hast: Eine Hierarchie von zu erzeugenden Objekten und eine Factory-Hierarchie. Die verschiedenen konkreten Factories erzeugen dann verschiedene Member der Objekt-Hierarchie.<br />
Eine google-Suche nach Abstract Factory sollte dir viele nützliche Informationen liefern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998301</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998301</guid><dc:creator><![CDATA[HumeSikkins]]></dc:creator><pubDate>Mon, 20 Feb 2006 08:51:14 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 09:03:26 GMT]]></title><description><![CDATA[<p>CStoll-Gratulant schrieb:</p>
<blockquote>
<p>Wirfts du immer Referenzen und keine Zeiger, CStoll?<br />
Hat das einen Vorteil?</p>
</blockquote>
<p>Ich werfe - wenn überhaupt - Objekte, da brauche ich mich nicht um die Beseitigung kümmern. Und fange sie dann als Referenz, das spart erstens Konstruktor-Aufrufe und nutzt zweitens die Polymorphie der Exception-Klassen aus (Stichwort: virtuelle Funktionen).</p>
<p>@Assambler: Virtuelle Methoden werden von der Basisklasse definiert (evt. sogar nur die Schnittstelle - dann sind es <strong>pur</strong> virtuelle Methoden) und von abgeleiteten Klassen neu deklariert. Welche Methode letzlich aufgerufen wird, hängt dann vom (dynamischen) Typ des Objekts ab:</p>
<pre><code class="language-cpp">class Base
{
public:
  virtual void ShowIt() { cout&lt;&lt;&quot;Base Object&quot;&lt;&lt;endl; }
};

class Derived
{
public:
  void ShowIt() { cout&lt;&lt;&quot;Derived Object&quot;&lt;&lt;endl; }
};

int main()
{
  Base* b=new Derived();
  b-&gt;ShowIt();
  delete b;
  return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/998310</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998310</guid><dc:creator><![CDATA[CStoll (off)]]></dc:creator><pubDate>Mon, 20 Feb 2006 09:03:26 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 09:09:41 GMT]]></title><description><![CDATA[<p>[quote]<br />
Ich werfe - wenn überhaupt - Objekte, da brauche ich mich nicht um die Beseitigung kümmern. Und fange sie dann als Referenz, das spart erstens Konstruktor-Aufrufe<br />
[quote]<br />
spart Ctor weil es ist eine Referenz oder muss man erst const Referenz benutzen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998320</guid><dc:creator><![CDATA[netrobot]]></dc:creator><pubDate>Mon, 20 Feb 2006 09:09:41 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 09:16:35 GMT]]></title><description><![CDATA[<p>netrobot schrieb:</p>
<blockquote>
<p>spart Ctor weil es ist eine Referenz oder muss man erst const Referenz benutzen?</p>
</blockquote>
<p>Weil es eine Referenz ist <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /> (throw legt ein Objekt an, catch() bekommt entweder eine Kopie (wenn du per Wert fängst) oder einen Verweis (wenn du per Referenz fängst) von diesem Objekt übergeben)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998326</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998326</guid><dc:creator><![CDATA[CStoll (off)]]></dc:creator><pubDate>Mon, 20 Feb 2006 09:16:35 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 09:36:04 GMT]]></title><description><![CDATA[<p>muss man nicht trotzdem const &amp; benutzen? damit man keine Modification mit dem eingeworfenden Object durchführen kann</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998346</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998346</guid><dc:creator><![CDATA[netrobot]]></dc:creator><pubDate>Mon, 20 Feb 2006 09:36:04 GMT</pubDate></item><item><title><![CDATA[Reply to Was is eine factory class? on Mon, 20 Feb 2006 09:42:13 GMT]]></title><description><![CDATA[<p>Muß man nicht - vielleicht WILL man ja das übergebene Objekt verändern <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /> (außerdem - solange du das Exception-Objekt nicht weiterwirfst, wird es am Ende des catch-Blocks sowieso zerstört)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/998350</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/998350</guid><dc:creator><![CDATA[CStoll (off)]]></dc:creator><pubDate>Mon, 20 Feb 2006 09:42:13 GMT</pubDate></item></channel></rss>