 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Marlene Miller Guest
|
Posted: Thu Mar 17, 2005 8:19 am Post subject: design Q: override | strategy pattern | function object |
|
|
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 |
|
 |
|
|
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
|
|