473,774 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strategy pattern

I think there are 2 options for how to implement the Strategy pattern.
One is to use polymorphism; derived classes have the same interface
and can be plugged in. The other is to use templates: Instantiate the
class, passing in a functor (algorithm). Code samples below.

The problem with the first way is that the interface must be the same
(parameters passed in) for all strategies. The problem with the
second way is that there is no common base class, and that means that
I can't put these items on an STL queue (which requires that all its
types be homogeneous).

In my case, I want to make a general Task class, and put them on a
queue. Later, they will be taken off and executed by calling their
function do(). But in cases where you want to pass a few things in,
how do you do it? You could use Windows old trick of passing in 2
general parameters, one being a pointer to whatever you wish. But you
still need type information to interpret it.

Treat this as semi-pseudocode: You don't need this code to answer the
question.

// -------------------------
//First way: polymorphism
class base_strategy (string filename, int offset)
{
string filename, int offset;
base_strategy (string filename, int offset)
: filename(filena me),offset(offs et) {}
virtual void do() =0;
};

class strategy1 (string filename, int offset)
{
string filename, int offset;
strategy1 (string filename, int offset)
: filename(filena me),offset(offs et) {}
virtual void do() {//print contents of file starting at offset};
};

// But what if I want a strategy that also inputs another number?
// It's too late to change the interface.
// And isn't it illegal to have a container of pointers? Some STL
rule?

main()
{
queue<base_stra tegy *> q;
base_strategy *p = new strategy1 ("a.cpp", 234);
q.insert (p);
}

// ------------------------
//Second way: functors (function object)
class strategy1
{
int a,b;
strategy1 (int a, int b) : a(a), b(b) {};
void operator()(int i) {output a*b+2*i;}
}

for_each(q.begi n(), q.end(), strategy1(3,4)) ;

// That would work fine, but this doesn't help me with containers.

Sorry for the long message.
Jul 22 '05 #1
3 3256
syncman wrote:
I think there are 2 options for how to implement the Strategy pattern.
One is to use polymorphism; derived classes have the same interface
and can be plugged in. The other is to use templates: Instantiate the
class, passing in a functor (algorithm). Code samples below.

The problem with the first way is that the interface must be the same
(parameters passed in) for all strategies. The problem with the
second way is that there is no common base class, and that means that
I can't put these items on an STL queue (which requires that all its
types be homogeneous).

In my case, I want to make a general Task class, and put them on a
queue. Later, they will be taken off and executed by calling their
function do(). But in cases where you want to pass a few things in,
how do you do it? You could use Windows old trick of passing in 2
general parameters, one being a pointer to whatever you wish. But you
still need type information to interpret it.


What do you think of using templates and polymorphism like this:

// Interface => abstract class
class StrategyInterfa ce
{
public:
StrategyInterfa ce();

virtual void do() = 0;
};

template<class T>
class Strategy : StrategyInterfa ce
{
public:
virtual void do() {...};
}

and you benefit from template specialization, and can use it:

main()
{
queue<StrategyI nterface *> q;
StrategyInterfa ce *p = new Strategy<float> ();
q.insert(p);
}

Yannick

Jul 22 '05 #2

"syncman" <hs***@hotmail. com> wrote in message
news:cc******** *************** ***@posting.goo gle.com...
I think there are 2 options for how to implement the Strategy pattern.
One is to use polymorphism; derived classes have the same interface
and can be plugged in. The other is to use templates: Instantiate the
class, passing in a functor (algorithm). Code samples below.
[SNIP]
Treat this as semi-pseudocode: You don't need this code to answer the
question.
I just wonder, if you take the time to type in this "pseudo" code, why not
do it correct with all the semicolons and so on?

// -------------------------
//First way: polymorphism
class base_strategy (string filename, int offset)
{
string filename, int offset;
base_strategy (string filename, int offset)
: filename(filena me),offset(offs et) {}
virtual void do() =0;
};

class strategy1 (string filename, int offset)
{
string filename, int offset;
strategy1 (string filename, int offset)
: filename(filena me),offset(offs et) {}
virtual void do() {//print contents of file starting at offset};
};

// But what if I want a strategy that also inputs another number?
// It's too late to change the interface.
// And isn't it illegal to have a container of pointers? Some STL
rule?


No it is not illegal to have a container of pointers. The requirements of
the standard containers can be found in the C++ standard, and there it says:
23.1

-3- The type of objects stored in these components must meet the
requirements of CopyConstructib le types (lib.copyconstr uctible), and the
additional requirements of Assignable types.

Thus, having, e.g., a vector of pointers is perfectly okay.

[SNIP]

IMHO having a common interface of all strategies (at least to the outside
world) is not a drawback but a good thing. A common way to implement a
strategy pattern is to have an object that defines the interface and this
object obtains a pointer to the actual implementation.

For example: (Of course the actual integration objects like Simpson, Gauss
and so on should be singletons, but I will skip this here).

class CIntAlgorithm {
// some common base stuff
};

class CSimpsonInt : public CIntAlgorithm {
public:
double Integrate( double Min, double Max, double NrOfSteps ) { ....};
};

class CGaussInt : public CIntAlgorithm {
public:
double Integrate( double Min, double Max, double NrOfSteps, int
AdditionalParam ) { ....};
};

class CIntegrator {
public:
CIntegrator( CIntAlgorithm* pAlgo ) : m_pAlgo( pAlgo ) {};

double Integrate( double Min, double Max, double NrOfSteps ) {

// If you need to differentiate in the use of Simpson & Gauss you
could use dynamic_cast for example.
};

protected:
CIntAlgorithm* m_pAlgo;
};

HTH
Chris
Jul 22 '05 #3
On Wed, 14 Jan 2004 14:00:28 -0800, syncman wrote:
I think there are 2 options for how to implement the Strategy pattern.
One is to use polymorphism; derived classes have the same interface
and can be plugged in. The other is to use templates: Instantiate the
class, passing in a functor (algorithm). Code samples below.

The problem with the first way is that the interface must be the same
(parameters passed in) for all strategies. The problem with the
second way is that there is no common base class, and that means that
I can't put these items on an STL queue (which requires that all its
types be homogeneous).

In my case, I want to make a general Task class, and put them on a
queue. Later, they will be taken off and executed by calling their
function do(). But in cases where you want to pass a few things in,
how do you do it? You could use Windows old trick of passing in 2
general parameters, one being a pointer to whatever you wish. But you
still need type information to interpret it.


Combine the two aproaches. Your functor approach is fine, just make them
inherit from a common baseclass. (Don't forget the virtual destructor,
your stategy_base class should have had one).

Now put pointers to these object in a container. Containers of pointers
are fine, and in fact very often used. It's containers of std::auto_ptr
that cannot be done. You may want to use boost::shared_p tr for this, or
just delete every pointer in the queue manually once you're done with them.

This does not allow one to use for_each and related algorithms easily (but
look up std::mem_fun), but that is a small price to pay. I'ld advice to do
it the way I told you above, but...

.... you could implement a functor that holds a pointer to some functor
implementation baseclass. You'll need to implement copy construction and
assignment of this functor in an inteligent way (use boost::shared_p tr to
make it trivial). Then make operator() of the functor call operator() on
this baseclass and you're done!

I would advice you to walk, no run, to your nearest bookstore and get
"Modern C++ Design" by Alexandrescu. It goes into great depth on this, and
other, subjects. A must have for every advanced C++ programmer. Also have
a look at boost::bind, it is very useful in situations like this.

HTH,
M4

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1932
by: Ben Jeurissen | last post by:
Hello, I have to deal with the following issue in C++: Two threads are started from the main thread, both capturing images from a different firewire camera. Both threads take shots of 460800 bytes at a rate of 30 frames per second. This works fine, without frame loss, and I can display the two framestreams on screen. Now I would like to write these frames to my disk too (2 10000rpm in
2
1797
by: philippe sillon | last post by:
Hi, I have a problem to implemente the strategy pattern. the problem come from that a method take different arguments type (object in interface and String / Int in implemented class). When price3 instance call getPrice, the method invoked is the one of PriceRule abstract class. But, I want to invoke the method of PriceruleForRegion. So I think the solution is somewhere on casting to original type (PriceRuleForRegion) but I don't how I...
6
4706
by: Daniel Santa Cruz | last post by:
Hello all, I've been trying to go over my OO Patterns book, and I decided to try to implement them in Python this time around. I figured this would help me learn the language better. Well, I've gotten stuck with my first go at OO patterns with Python. I guess it goes without say that some of the stuff that are taken for granted in most of the books (ie. Interfaces, Abstract classes) don't really apply to Python per say, but the idea...
5
2069
by: pythoncurious | last post by:
Hi python experts In C++ I can do something like this: class Base { public: void f() { this->f_(); } private: virtual void f_() = 0; };
0
1360
by: ltruett | last post by:
I'm almost done my series of design patterns using PHP 5. Today's pattern is the Strategy Pattern. http://www.fluffycat.com/PHP-Design-Patterns/Strategy/ In the Stratedy Pattern a "family of algorythms" is used interchangably by calling clients. This is the second pattern in as many days as I've used my older work with patterns in Java, and again I liked the old example when I first
1
2491
by: Nick | last post by:
Hi, I read somewhere recently that the strategy pattern could be used instead of using switch command on an enum (for example). How would this work? A small example would be great. Thanks, Nick
8
2154
by: CK | last post by:
Good Morning All, I am writing an app to calculate the cost of a hotel room. I am new to c++. I have decided to try using the Strategy Design Pattern. I want a superclass HotelRoom, and then make 3 classes that inherit from it. SuiteRoom, StandardRoom, and DeluxeRoom. The price of the room is based on 3 factors, if there is are more then 2 people in the room, there is a $10 charge per persons over 2, if they add an extra bed, and the room...
3
1475
by: Mike TI | last post by:
Aug 2, 2007 12:00pm Hi all I have to design and program an application which will be used in three countries. It would be a fairly large application with lot of data entry. The database hence has to be consolidated and at one location. Now the internet is excellent in one country and reasonable in the other
8
2777
by: Slaunger | last post by:
Hi all, I am a Python novice, and I have run into a problem in a project I am working on, which boils down to identifying the patterns in a sequence of integers, for example ..... 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0 3 3 0 3 3 0 10 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 6 6 1 9 3 3 0 3 3 0...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.