C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

design Q: override | strategy pattern | function object

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Marlene Miller
Guest





PostPosted: Thu Mar 17, 2005 8:19 am    Post subject: design Q: override | strategy pattern | function object Reply with quote



Hello.

I want to allow variations of a class, so that my event objects can use
different algorithms to select the next server. I am new to OO. I've read
about various ways to do things. They all work. But I don't know how to
choose.

Question: Would you please offer some guidelines or insights or reasons on
how you would choose?

1. Create a derived class and override the select function.

class E1 {
public:
void f() { S* s = select(); ... }
protected:
virtual S* select();
};

class B : public E1 {
S* select();
};

2. The strategy pattern - a family of algorithms with a common interface.

class C { //strategy interface
public:
virtual S* select();
};

class D : public C { //a special algorithm
public:
S* select();
};

class E2 {
C* c;
public:
E2(C* cc) : c(cc) {}
void f() { S* s = c->select(); ... }
};

3. The strategy pattern using function objects.

class SelectS {
public:
virtual S* operator()();
};

class FixedS : public SelectS {
public:
S* operator()();
};

class E3 {
SelectS* ss;
public:
E3(SelectS* p) : ss(p) {}
void f() { S* s = (*ss)(); ... }
};

Thank you. - Marlene



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.