473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

design pattern for defining a relational operator

I want the DataAnDRelation is flexible in design so that client code
can define its own relationship according to their own needs.

I am trying to use function pointer, but that needs client code to
declare friendship in the class DataAndRelation, which I feel not
neat. Any design pattern/standard way to solve this problem.

bool MyRelation(double a, double b) {
return a b;
}

bool MyRelationABS(double a, double b) {
return abs(a) abs(b);
}

class DataAndRelation {
public:
DataAndRelation (double a, bool (*ptr )(double, double) ) { data =
a; ptr_relation = ptr; }
~DataAndRelation () {}
bool GreatOrEaqual (const DataAndRelation& other) const {
return (*ptr_relation) (this->data, other.data);
}
private:
double data;
bool (*ptr_relation )(double, double);
}

int main() {
DataAndRelation example(1.0, MyRelation);
DataAndRelation example2(2.0, MyRelationABS);
...
...
}

Jun 4 '07 #1
2 1270
On Jun 5, 5:31 am, newbie <mitbb...@yahoo.comwrote:
I want the DataAnDRelation is flexible in design so that client code
can define its own relationship according to their own needs.

I am trying to use function pointer, but that needs client code to
declare friendship in the class DataAndRelation, which I feel not
neat. Any design pattern/standard way to solve this problem.

bool MyRelation(double a, double b) {
return a b;

}

bool MyRelationABS(double a, double b) {
return abs(a) abs(b);

}

class DataAndRelation {
public:
DataAndRelation (double a, bool (*ptr )(double, double) ) { data =
a; ptr_relation = ptr; }
~DataAndRelation () {}
bool GreatOrEaqual (const DataAndRelation& other) const {
return (*ptr_relation) (this->data, other.data);
}
private:
double data;
bool (*ptr_relation )(double, double);

}

int main() {
DataAndRelation example(1.0, MyRelation);
DataAndRelation example2(2.0, MyRelationABS);
...
...

}- Hide quoted text -

- Show quoted text -
Very good. Thank you.

Jun 5 '07 #2

"newbie" <mi******@yahoo.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
I want the DataAnDRelation is flexible in design so that client code
can define its own relationship according to their own needs.

I am trying to use function pointer, but that needs client code to
declare friendship in the class DataAndRelation, which I feel not
neat. Any design pattern/standard way to solve this problem.

bool MyRelation(double a, double b) {
return a b;
}

bool MyRelationABS(double a, double b) {
return abs(a) abs(b);
}

class DataAndRelation {
public:
DataAndRelation (double a, bool (*ptr )(double, double) ) { data =
a; ptr_relation = ptr; }
~DataAndRelation () {}
bool GreatOrEaqual (const DataAndRelation& other) const {
return (*ptr_relation) (this->data, other.data);
}
private:
double data;
bool (*ptr_relation )(double, double);
}

int main() {
DataAndRelation example(1.0, MyRelation);
DataAndRelation example2(2.0, MyRelationABS);
...
...
}

What about something like this? Basically I've replaced function pointers
with real objects or functors.
This is based upon some pattern, "strategy" perhaps ?

dave

#include "math.h"

class Relation
{
public:
virtual bool compare( double a, double b) const = 0;
virtual ~Relation();
};

class LessThan : public Relation
{
public:
virtual bool compare( double a, double b) const
{
return a < b;
}
};
class LessAbs : public Relation
{
public:
virtual bool compare( double a, double b) const
{
return fabs(a) < fabs(b);
}
};


class DataAndRelation
{
public:
DataAndRelation (double a, Relation* relation )
:_data(a), _relation(relation)
{
}
bool GreatOrEaqual (const DataAndRelation& other) const
{

return _relation->compare( _data, other.data() );
}
double data() const { return _data ;}
private:
double _data;
Relation* _relation;
};

int main()
{
LessThan lessThan;
LessAbs lessAbs;
DataAndRelation example(1.0, &lessThan);
DataAndRelation example2(2.0, &lessAbs);

return 0;
}

Jun 5 '07 #3

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

Similar topics

5
by: Coder-X | last post by:
Hi, i have a few questions i would like to ask : 1 - Where can i find good design patterns resources for .NET ? 2 - What's the best design pattern for a windows database application (...
4
by: scottrm | last post by:
I am fairly new to oo design and I am looking at developing an object oriented asp.net application which will be built on top of a relational database. I have read quite a bit of the theory but...
4
by: joh12005 | last post by:
Hello, i posted for suggestions a little idea even if it still needs further thoughts but as i'm sure you could help :) if would like to implement some kind of Condition class which i coud...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
3
by: John S | last post by:
I've got to produce a console app that requests a dataset from a web service in our personnel system, manipulates the data and then updates each record in the Active directory. Not being the...
9
by: Jens Jensen | last post by:
Hello all, I need some design advice for my web service. I' have written a web service that exposes a function that takes some parameters and return an xml.
2
by: =?Utf-8?B?QmVu?= | last post by:
I have a Customer table in the database that relates to a CustomerType table (I have several other table combinations in the database like Document and DocumentType). As far as I can tell, I...
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
3
by: aaragon | last post by:
Hello everyone, I've been trying to work with the visitor design pattern, and it works fine except for the following. Let's suppose that we have a fixed hierarchy of classes (many of them)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.