<?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[Strategy Pattern Problem beim Interface]]></title><description><![CDATA[<p>Ich habe ein sehr einfaches Strategy Pattern in c++ passend zu diesem Diagramm</p>
<p><a href="http://www.philipphauer.de/study/se/design-pattern/strategy/strategy-gr.png" rel="nofollow">http://www.philipphauer.de/study/se/design-pattern/strategy/strategy-gr.png</a></p>
<p>Context.cpp</p>
<pre><code>#include &quot;stdafx.h&quot;
#include &quot;Context.h&quot;
#include &quot;Strategy.h&quot;
#include &lt;iostream&gt;

using namespace std;

Context::Context( void ) {
	Strategy_ = new ConcreteStrategyA();
}

void Context::execute() {
	Strategy_-&gt;executeAlgorithm();
}

void Context::setStrategy(Strategy *Strategy) {
	cout &lt;&lt; &quot;Neue Strategie&quot; &lt;&lt; endl;
	Strategy_ = Strategy;
}
</code></pre>
<p>Context.h</p>
<pre><code>#pragma once
#ifndef _CONTEXT_H_
#define _CONTEXT_H_

#include &quot;stdafx.h&quot;
#include &quot;Strategy.h&quot;

class Context {
public:
	Strategy *Strategy_;

	Context( void );
	void execute();
	void setStrategy(Strategy *Strategy);
};

#endif // _CONTEXT_H_
</code></pre>
<p>Strategy.cpp</p>
<pre><code>#include &quot;stdafx.h&quot;
#include &quot;Strategy.h&quot;
#include &lt;iostream&gt;

using namespace std;

void ConcreteStrategyA::executeAlgorithm() {
	cout &lt;&lt; &quot;Strategie A&quot; &lt;&lt; endl; 
}

void ConcreteStrategyB::executeAlgorithm() {
	cout &lt;&lt; &quot;Strategie B&quot; &lt;&lt; endl; 
}
</code></pre>
<p>Strategy.h</p>
<pre><code>#pragma once
#ifndef _STRATEGY_H_
#define _STRATEGY_H_

class Strategy {
public:
	virtual void executeAlgorithm() = 0;
};

class ConcreteStrategyA : public Strategy {
public:
	void executeAlgorithm();
};

class ConcreteStrategyB : public Strategy {
public:
	void executeAlgorithm();
};

#endif // _STRATEGY_H_
</code></pre>
<p>Client</p>
<pre><code>#include &quot;stdafx.h&quot;
#include &quot;Context.h&quot;
#include &quot;Strategy.h&quot;

using namespace System;

int main( void ) {

	//Default Verhalten
	Context *ContextObj = new Context();
	ContextObj-&gt;execute();

	//Verhalten ändern
	ContextObj-&gt;setStrategy(new ConcreteStrategyB());
	ContextObj-&gt;execute();

	return 0;
}
</code></pre>
<p>Ich möchte nun Parameter über ein struct einbinden und in den konkrekten Strategien ausgeben und entsprechende setter und getter vorsehen:</p>
<pre><code>struct Parameter {
    int A;
    int B;
    int C;
};

Parameter Parameter_;

void setParameter( Parameter &amp;Parameter ) {
    Parameter_ = Parameter;
}

void getParameter( Parameter &amp;Parameter ) {
    Parameter = Parameter_;
}
</code></pre>
<p>Die execute Methode der konkreten Strategien würde dann beispielsweise so aussehen:</p>
<pre><code>void ConcreteStrategyA::executeAlgorithm() {
    cout &lt;&lt; &quot;Strategie A&quot; &lt;&lt; endl;
    cout &lt;&lt; Parameter_.A &lt;&lt; endl;
    cout &lt;&lt; Parameter_.B &lt;&lt; endl;
    cout &lt;&lt; Parameter_.C &lt;&lt; endl;
}
</code></pre>
<p>Ich kam auf die Idee die Drei im Strategy &quot;Interface&quot; zu implementieren, da die konkreten Strategien vom Interface erben und so direkt auf das Parameter_ zugreifen können.</p>
<p>Das hat jedoch zur folge, dass ich auch im Context das gleiche struct definieren muss und dann in etwa so &quot;durchreiche&quot;:</p>
<pre><code>Context::setParameter( Parameter &amp;Parameter ) {
    Strategy::Parameter StrategyParameter;
    StrategyParameter.A = Parameter.A;
    StrategyParameter.B = Parameter.B;
    StrategyParameter.C = Parameter.C;
    Strategy_.-&gt;setParameter( StrategyParameter );
}
</code></pre>
<p><strong>Nun zu meiner Frage:</strong> Gibt es eine Möglichkeit das struct Parameter in Context anzulegen und die konkreten Strategien darauf zugreifen zu lassen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/307018/strategy-pattern-problem-beim-interface</link><generator>RSS for Node</generator><lastBuildDate>Sat, 04 Apr 2026 12:46:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/307018.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 14 Aug 2012 10:05:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Strategy Pattern Problem beim Interface on Tue, 14 Aug 2012 10:05:58 GMT]]></title><description><![CDATA[<p>Ich habe ein sehr einfaches Strategy Pattern in c++ passend zu diesem Diagramm</p>
<p><a href="http://www.philipphauer.de/study/se/design-pattern/strategy/strategy-gr.png" rel="nofollow">http://www.philipphauer.de/study/se/design-pattern/strategy/strategy-gr.png</a></p>
<p>Context.cpp</p>
<pre><code>#include &quot;stdafx.h&quot;
#include &quot;Context.h&quot;
#include &quot;Strategy.h&quot;
#include &lt;iostream&gt;

using namespace std;

Context::Context( void ) {
	Strategy_ = new ConcreteStrategyA();
}

void Context::execute() {
	Strategy_-&gt;executeAlgorithm();
}

void Context::setStrategy(Strategy *Strategy) {
	cout &lt;&lt; &quot;Neue Strategie&quot; &lt;&lt; endl;
	Strategy_ = Strategy;
}
</code></pre>
<p>Context.h</p>
<pre><code>#pragma once
#ifndef _CONTEXT_H_
#define _CONTEXT_H_

#include &quot;stdafx.h&quot;
#include &quot;Strategy.h&quot;

class Context {
public:
	Strategy *Strategy_;

	Context( void );
	void execute();
	void setStrategy(Strategy *Strategy);
};

#endif // _CONTEXT_H_
</code></pre>
<p>Strategy.cpp</p>
<pre><code>#include &quot;stdafx.h&quot;
#include &quot;Strategy.h&quot;
#include &lt;iostream&gt;

using namespace std;

void ConcreteStrategyA::executeAlgorithm() {
	cout &lt;&lt; &quot;Strategie A&quot; &lt;&lt; endl; 
}

void ConcreteStrategyB::executeAlgorithm() {
	cout &lt;&lt; &quot;Strategie B&quot; &lt;&lt; endl; 
}
</code></pre>
<p>Strategy.h</p>
<pre><code>#pragma once
#ifndef _STRATEGY_H_
#define _STRATEGY_H_

class Strategy {
public:
	virtual void executeAlgorithm() = 0;
};

class ConcreteStrategyA : public Strategy {
public:
	void executeAlgorithm();
};

class ConcreteStrategyB : public Strategy {
public:
	void executeAlgorithm();
};

#endif // _STRATEGY_H_
</code></pre>
<p>Client</p>
<pre><code>#include &quot;stdafx.h&quot;
#include &quot;Context.h&quot;
#include &quot;Strategy.h&quot;

using namespace System;

int main( void ) {

	//Default Verhalten
	Context *ContextObj = new Context();
	ContextObj-&gt;execute();

	//Verhalten ändern
	ContextObj-&gt;setStrategy(new ConcreteStrategyB());
	ContextObj-&gt;execute();

	return 0;
}
</code></pre>
<p>Ich möchte nun Parameter über ein struct einbinden und in den konkrekten Strategien ausgeben und entsprechende setter und getter vorsehen:</p>
<pre><code>struct Parameter {
    int A;
    int B;
    int C;
};

Parameter Parameter_;

void setParameter( Parameter &amp;Parameter ) {
    Parameter_ = Parameter;
}

void getParameter( Parameter &amp;Parameter ) {
    Parameter = Parameter_;
}
</code></pre>
<p>Die execute Methode der konkreten Strategien würde dann beispielsweise so aussehen:</p>
<pre><code>void ConcreteStrategyA::executeAlgorithm() {
    cout &lt;&lt; &quot;Strategie A&quot; &lt;&lt; endl;
    cout &lt;&lt; Parameter_.A &lt;&lt; endl;
    cout &lt;&lt; Parameter_.B &lt;&lt; endl;
    cout &lt;&lt; Parameter_.C &lt;&lt; endl;
}
</code></pre>
<p>Ich kam auf die Idee die Drei im Strategy &quot;Interface&quot; zu implementieren, da die konkreten Strategien vom Interface erben und so direkt auf das Parameter_ zugreifen können.</p>
<p>Das hat jedoch zur folge, dass ich auch im Context das gleiche struct definieren muss und dann in etwa so &quot;durchreiche&quot;:</p>
<pre><code>Context::setParameter( Parameter &amp;Parameter ) {
    Strategy::Parameter StrategyParameter;
    StrategyParameter.A = Parameter.A;
    StrategyParameter.B = Parameter.B;
    StrategyParameter.C = Parameter.C;
    Strategy_.-&gt;setParameter( StrategyParameter );
}
</code></pre>
<p><strong>Nun zu meiner Frage:</strong> Gibt es eine Möglichkeit das struct Parameter in Context anzulegen und die konkreten Strategien darauf zugreifen zu lassen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2241740</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241740</guid><dc:creator><![CDATA[Zisko]]></dc:creator><pubDate>Tue, 14 Aug 2012 10:05:58 GMT</pubDate></item><item><title><![CDATA[Reply to Strategy Pattern Problem beim Interface on Tue, 14 Aug 2012 10:10:22 GMT]]></title><description><![CDATA[<p>Zisko schrieb:</p>
<blockquote>
<p><strong>Nun zu meiner Frage:</strong> Gibt es eine Möglichkeit das struct Parameter in Context anzulegen und die konkreten Strategien darauf zugreifen zu lassen?</p>
</blockquote>
<p>erstelle das struct in einer eigenen Header-Datei, die du überall dort einbindest, wo du sie brauchst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2241743</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241743</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Tue, 14 Aug 2012 10:10:22 GMT</pubDate></item><item><title><![CDATA[Reply to Strategy Pattern Problem beim Interface on Tue, 14 Aug 2012 10:20:43 GMT]]></title><description><![CDATA[<p>Und wo packe ich die Instanz Parameter_ (nicht public) des structs hin?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2241748</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241748</guid><dc:creator><![CDATA[Zisko]]></dc:creator><pubDate>Tue, 14 Aug 2012 10:20:43 GMT</pubDate></item><item><title><![CDATA[Reply to Strategy Pattern Problem beim Interface on Tue, 14 Aug 2012 10:26:00 GMT]]></title><description><![CDATA[<p>Zisko schrieb:</p>
<blockquote>
<p>Und wo packe ich die Instanz Parameter_ (nicht public) des structs hin?</p>
</blockquote>
<p>in die Strategy-Basisklasse</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2241750</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241750</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Tue, 14 Aug 2012 10:26:00 GMT</pubDate></item><item><title><![CDATA[Reply to Strategy Pattern Problem beim Interface on Tue, 14 Aug 2012 11:21:13 GMT]]></title><description><![CDATA[<p>Einige deiner new-Aufrufe sind überflüssig, außerdem hast du tonnenweise Speicherlecks. C++ ist nicht Java, also solltest du C++ auch nicht wie Java behandeln...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2241769</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241769</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Tue, 14 Aug 2012 11:21:13 GMT</pubDate></item><item><title><![CDATA[Reply to Strategy Pattern Problem beim Interface on Tue, 14 Aug 2012 13:38:00 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Einige deiner new-Aufrufe sind überflüssig, außerdem hast du tonnenweise Speicherlecks. C++ ist nicht Java, also solltest du C++ auch nicht wie Java behandeln...</p>
</blockquote>
<p>Dazu habe ich im anderem Thread( <a href="http://www.c-plusplus.net/forum/p2241803#2241803" rel="nofollow">http://www.c-plusplus.net/forum/p2241803#2241803</a> ) einiges geschrieben. (Nur so zur Sicherheit, damit sich hier nicht nochmal jemand unnötig die gleiche Mühe macht. ;))</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2241808</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241808</guid><dc:creator><![CDATA[Dobi]]></dc:creator><pubDate>Tue, 14 Aug 2012 13:38:00 GMT</pubDate></item><item><title><![CDATA[Reply to Strategy Pattern Problem beim Interface on Tue, 14 Aug 2012 13:53:27 GMT]]></title><description><![CDATA[<p>Danke für den Hinweis, aber keine Sorge die Speicherlecks sind mir bewusst und werden im &quot;Original&quot; entsprechend behandelt. Das hier ist auch nur ein quick'n'dirty Beispiel um mein Problem besser zu beschreiben und da die Programmierung bei mir nur eine Nebenbeschäftigung ist, bin ich nicht so firm, dass ich bei ersten heruntertippen alles berücksichtige.</p>
<p>Ich würde gerne wissen welche meiner Aufrufe überflüssig sind?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2241814</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241814</guid><dc:creator><![CDATA[Zisko]]></dc:creator><pubDate>Tue, 14 Aug 2012 13:53:27 GMT</pubDate></item><item><title><![CDATA[Reply to Strategy Pattern Problem beim Interface on Tue, 14 Aug 2012 14:09:15 GMT]]></title><description><![CDATA[<p>Zisko schrieb:</p>
<blockquote>
<p>Danke für den Hinweis, aber keine Sorge die Speicherlecks sind mir bewusst und werden im &quot;Original&quot; entsprechend behandelt. Das hier ist auch nur ein quick'n'dirty Beispiel um mein Problem besser zu beschreiben und da die Programmierung bei mir nur eine Nebenbeschäftigung ist, bin ich nicht so firm, dass ich bei ersten heruntertippen alles berücksichtige.</p>
</blockquote>
<p>Aus dem Grund solltest du auch ein reduziertes Original posten, damit man sich um die eigentliche Problematik kümmern kann und die nicht hinter dem &quot;dirty&quot;-Teil (d.h. den ganzen Fehlern) untergeht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2241831</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241831</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Tue, 14 Aug 2012 14:09:15 GMT</pubDate></item></channel></rss>