473,503 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1520

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

Similar topics

3
3116
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
4285
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
1558
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
311
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
2273
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
1387
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
2325
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
2553
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
4996
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
0
7342
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...
1
6998
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...
0
7464
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...
0
5586
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,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4680
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...
0
3171
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...
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.