473,383 Members | 1,919 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,383 software developers and data experts.

design pattern .. factory i suspect

I'm going through modern C++ design looking for tips and while hi-tech
I suspect one solution to my issue would involve the factory design
pattern.

// algorithms.h
class Algorithms {
protected:
typedef std::deque<double> DDEQUE; // need to make this even more
generic to support floats .. i.e float and double
public:
virtual ~Algorithms() {};
virtual double mean1( const DDEQUE& queue );
virtual double mean2( const DDEQUE& queue );
// FFT and all sorts of funny math _stuff_
};

/////////// algorithms.hpp
#include "algorithms.hpp"
#include <iostream>
#include <numeric>

#ifndef NDEBUG
# define PRINT_CALLING_FUN() std::cout << __func__ << "() was called"
<< std::endl
#else
# define PRINT_CALLING_FUN()
#endif

double Algorithms::mean1( const DDEQUE& queue )
{
double val = std::accumulate ( queue.begin(), queue.end(), 0. ) /
queue.size();
PRINT_CALLING_FUN();
return val;
}

double Algorithms::mean2( const DDEQUE& queue )
{
double val = std::accumulate ( queue.begin(), queue.end(), 0. ) /
queue.size();
PRINT_CALLING_FUN();
return val;
}

I realize, there's no difference between mean1 and mean2 but they're
there for illustration purposes.

------------------------------------------------------------------------------------------------------
I've got two classes Ying and Yang.

Yang will use/is only interested in mean2 from Algorithm and Ying will
use/is only interested in mean1 from Algorithm. The current approach:

class Algorithms_Ying : public Algorithms {
public:
double mean1( const DDEQUE& queue ) {}
};

class Algorithms_Yang : public Algorithms {
public:
double mean2( const DDEQUE& queue ) {}
};

class Yang {
private:
typedef std::deque<double> DDEQUE;
Algorithms *alg;
public:
Yang(Algorithms *a) {alg = a;};
virtual ~Yang() {};
void test(const DDEQUE& q) { alg->mean2(q); }
};
class Ying {
private:
typedef std::deque<double> DDEQUE;
Algorithms *alg;
public:
Ying(Algorithms *a) {alg = a;};
virtual ~Ying() {};
void test(const DDEQUE& q) { alg->mean1(q); }
};

int main{}
{
Algorithms_Ying a1;
Algorithms_Yang a2;

Ying ying(&a1);
Yang yang(&a2);
}
That today seems silly and _appears_ like a maintenance nightmare.
What I'm trying to determine is a way to have (may not be saying this
right) a factory of algorithms, then have Ying/Yang or whomever go
inside and literally pull out what they want. You see, I receive from
the _outside_ world composite type (literally) YingStuff and
YangStuff.

enum ALGO { mean1, mean2, FFT, IFFT }; // etc etc
struct YingToDo { ALGO enum; };
struct YangToDo { ALGO enum; };

struct YingStuff { YingToDo todo[ 5 ] ; } ;
struct YangStuff { YingToDo todo[ 5 ] ; } ;

The tricky piece to this though surrounds the fact that I can't do -
for example - a mean1 on Yang. The point being the outside world will
need to ensure the YingStuff and YangStuff composites are filled out
properly. Simply put the factory I suspect could return an object that
contains the necessary functions and if I'm asked to do something I'm
can't do I wont do it.

The question. How would I achieve this? Source snippet or pseudo code
would help me better envision the design.
Thanks

Apr 2 '06 #1
0 1511

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

Similar topics

3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
2
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site...
0
by: ma740988 | last post by:
I'm going through modern C++ design looking for tips and while hi-tech I suspect one solution to my issue would involve the factory design pattern. // algorithms.h class Algorithms {...
2
by: Mike | last post by:
Hello NG, i am just learning various Design Patterns and now i am not sure, if this design is correct (Builder) or if i should use an other pattern. I have various classes (here ChildA and...
2
by: Chris | last post by:
Hi, I have been stuck trying to come up with a design for days. I am working on a small project regarding barcode and I want to implement a factory design. I am now confused. I decided factory...
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
2
by: Duy Lam | last post by:
Hi everyone, Sorry, I don't know what group to post this problem, I think may be this group is suitable. I'm styduing DAO (Data Access Object) pattern in this link...
4
by: Pallav singh | last post by:
Hi , when should i select Factory Method / Prototype Design Pattern during my design phase ?? as both look similar to me Thanks in Advance Thanks Pallav
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.