473,835 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(doub le a, double b) {
return a b;
}

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

class DataAndRelation {
public:
DataAndRelation (double a, bool (*ptr )(double, double) ) { data =
a; ptr_relation = ptr; }
~DataAndRelatio n () {}
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 1290
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(doub le a, double b) {
return a b;

}

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

}

class DataAndRelation {
public:
DataAndRelation (double a, bool (*ptr )(double, double) ) { data =
a; ptr_relation = ptr; }
~DataAndRelatio n () {}
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.goo glegroups.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(doub le a, double b) {
return a b;
}

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

class DataAndRelation {
public:
DataAndRelation (double a, bool (*ptr )(double, double) ) { data =
a; ptr_relation = ptr; }
~DataAndRelatio n () {}
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(relat ion)
{
}
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
1864
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 ( multiuser ) ? 3 - I'm trying to develop a simple database application . I'm thinking of defining a class for every table in the database. Is this a good design practice ?
4
1791
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 find it hard to put it into practice. In particular I am confused in terms of interacting with the database. It seems to me classes map quite closely to database tables and I end up with a bunch of methods in each class which simply call stored...
4
1901
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 use to build bricks of more complex condition, conditions are based on fields by using regexp class Condition:
22
4753
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. Also describes implementation via ChangeManager (Mediator + Singleton) 2) Pattern hatching by John Vlissides Describes Observer's implementation via Visitor Design Pattern. 3) Design Patterns Explained by Alan Shalloway and James Trott
3
1377
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 most brilliant OOP programmer I'm having problems trying to design suitable objects. Would anyone like to suggest how they would design this type of app? Thanks
9
1701
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
1217
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 have several ways of defining the Customer class: 1) Use inheritance, where I will have an abstract base class CustomerBase that other, concrete customer class can subclass, i.e. Private, Company etc. 2) Use composition, so the Customer class would...
10
3687
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 classes talk to front-end directly. Do I need to use any design pattern in this? or what kind of design pattern is this?
3
2316
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) which I cannot modify. I decided to use the visitor design pattern depending on the actual type of the classes because those classes already support the loki visitor. #include <Loki/Visitor.h>
0
9810
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
9653
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,...
0
10524
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...
1
10562
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
10236
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
9348
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
6968
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
5639
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...
3
3092
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.