473,671 Members | 2,586 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 3251
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
1925
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
1792
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
4700
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
2064
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
1357
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
2487
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
2149
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
1468
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
2759
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
8483
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
8825
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8676
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
7445
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...
1
6237
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
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
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.